In CSS, selectors are used to select elements by class name, id, tag, etc. There are also some wildcard selectors available in CSS that we can use to define queries that select HTML elements.
Wildcard selectors allow us to select HTML elements that contain a specific substring in the value of a specific attribute (such as class or id). In this tutorial, we will learn to use *, ^ and $ wildcard selectors to represent classes and ids.
The Contains (*=) wildcard selector allows developers to find all HTML elements whose attribute value contains "string" as a substring. For example, using the "*" wildcard selector on a class finds all HTML elements whose class name contains that string.
Users can use wildcard selectors containing (*) for classes according to the following syntax.
[class*="string"] { }
The above syntax selects all HTML elements that contain "string" as a substring in the class name.
In the example below, we have created four different div elements and added text in them based on their class names. In CSS, we use the "contains" wildcard selector to select all div elements whose class name contains "test" as a substring.
In the output, the user can observe that the text color of the first two div elements is red because it contains the class name with the "test" substring.
<html> <head> <style> [class*="test"] { color: red; font-size: 2rem; } </style> </head> <body> <h2> Using the <i> contains (*=) </i> wildcard CSS selector in CSS. </h2> <div class = "test1"> This is a text with the class name test1.</div> <div class = "sampletest"> This is a text with the class name sampletest. </div> <div class = "demo"> This is a text with the class name demo test. </div> <div class = "element"> This is a text with the class name element.</div> </body> </html>
(^=) wildcard selector allows us to select all HTML elements whose attribute value starts with a specific substring.
Users can use selectors starting with wildcard characters for classes according to the following syntax.
[class^="string"] { }
The above syntax selects all HTML elements whose class name starts with "string".
In the example below, we use wildcard CSS selectors starting with (^=) and classes to select elements based on class names.
In the output, the user can observe that the text of the first and third div elements turns blue because it contains the "test" string at the beginning. The second div element contains "test" in the class name, but it is at the end of the class name and therefore is not selected by the leading (^=) wildcard selector.
<html> <head> <style> [class^="test"] { color: blue; font-weight: bold; } </style> </head> <body> <h2> Using the <i> starts with (^=) </i> wildcard CSS selector in CSS </h2> <div class = "test1"> This is a text with the class name test1.</div> <div class = "sampletest"> This is a text with the class name sampletest. </div> <div class = "testdemo"> This is a text with the class name testdemo test. </div> <div class = "element"> This is a text with the class name element. </div> </body> </html>
A wildcard selector ending in ($=) selects all HTML elements if a specific attribute value contains a substring at the end. For example, if the class names of two different elements are "onestart" and "lastone", and the substring is "one", it will select an HTML element with only the class name "lastone" because it contains the first End of substring.
Users can use wildcard CSS selectors ending with ($=) on classes according to the following syntax.
[class$="string"] { }
The above syntax selects all HTML elements whose class name ends with the "string" substring.
In the example below, the second nd and fourth div elements contain the "test" substring at the end of the class name value. We use a CSS selector with a ($=) wildcard at the end to select two div elements and apply borders, margins, and padding to them.
<html> <head> <style> [class$="test"] { border: 2px solid pink; padding: 5px 10px; margin: 5px; } </style> </head> <body> <h2> Using the <i> ends with ($=) </i> wildcard CSS selector in CSS.</h2> <div class = "test1"> This is a text with the class name test1.</div> <div class = "sampletest"> This is a text with the class name sampletest. </div> <div class = "testdemo"> This is a text with the class name testdemo test. </div> <div class = "elementtest"> This is a text with the class name elementtest. </div> </body> </html>
In the example below, we use a CSS selector ending in id instead of using a class as an attribute. It demonstrates how to select HTML elements using other properties and wildcard CSS selectors.
Here we select all HTML elements whose id value contains "name" at the end and change the font style and color.
<html> <head> <style> [id$="name"] { color: lightskyblue; font-style: italic; font-size: 1.5rem; } </style> </head> <body> <h2> Using the <i> ends with ($=) </i> wildcard CSS selector in CSS.</h2> <div id = "firstname"> id == firstname </div> <div id = "lastname"> id == lastname </div> <div id = "age"> id == age </div> <div id = "namestart"> id == namestart </div> </body> </html>
Users learned how to use wildcard CSS selectors with classes. Users can use the contains (*=) CSS selector to get elements whose attribute value contains a substring in the middle, and use the (^=) CSS selector to get elements whose attribute value contains a substring at the beginning and ends with ($). =) The end.
Additionally, users learned how to use wildcard CSS selectors with other properties, such as id in the previous example.
The above is the detailed content of Wildcard selectors (*, ^ and $) in CSS for classes. For more information, please follow other related articles on the PHP Chinese website!