Wildcard selectors (*, ^ and $) in CSS for classes
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.
CSS contains (*=) wildcard selector
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.
grammar
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.
Example
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 id="Using-the-i-contains-i-wildcard-CSS-selector-in-CSS"> 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>
CSS starts with (^=) wildcard selector
Starting with(^=) wildcard selector allows us to select all HTML elements whose attribute value starts with a specific substring.
grammar
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".
Example
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 id="Using-the-i-starts-with-i-wildcard-CSS-selector-in-CSS"> 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>
Ending with ($=) wildcard selector in CSS
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.
grammar
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.
Example
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 id="Using-the-i-ends-with-i-wildcard-CSS-selector-in-CSS"> 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>
Example
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 id="Using-the-i-ends-with-i-wildcard-CSS-selector-in-CSS"> 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and
