Table of Contents
CSS contains (*=) wildcard selector
grammar
Example
Using the contains (*=) wildcard CSS selector in CSS.
CSS starts with (^=) wildcard selector
Using the starts with (^=) wildcard CSS selector in CSS
Ending with ($=) wildcard selector in CSS
Using the ends with ($=) wildcard CSS selector in CSS.
Home Web Front-end CSS Tutorial Wildcard selectors (*, ^ and $) in CSS for classes

Wildcard selectors (*, ^ and $) in CSS for classes

Sep 04, 2023 am 09:57 AM

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.

1

2

[class*="string"] {

}

Copy after login

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<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>

Copy after login

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.

1

2

[class^="string"] {

}

Copy after login

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<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>

Copy after login

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.

1

2

[class$="string"] {

}

Copy after login

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<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>

Copy after login

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<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>

Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

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

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;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.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

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&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

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.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

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

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

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

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

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

See all articles