search
HomeWeb Front-endCSS TutorialWildcard selectors (*, ^ and $) in CSS for classes

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!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

See all articles

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool