Loading...

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

🔥 Quickfire # CSS
Ravi Baghel  ·   13 Oct 2022
 Wildcard Selectors (*, ^ and $) in CSS for classes
service-banner

In basic CSS learning, we learn about the various types of selectors available. Most of us are always using CSS to create queries and apply styles to HTML elements. But some of them are special for developing fields. Do you know about each and every one? If not, don't worry; here we introduce you to some useful wild selectors that you can use in your development.

Using CSS not only improves your designs and layouts, but you have to use it in the right way to achieve the desired results. Sometimes you realise that a good understanding of CSS foundations may have easily fixed the issue in website development. Hence, in this blog, we understand a some important attributes known as selectors.

How the Wildcard Selectors (*, ^ and $) Work in CSS

CSS allows the developer to arrange the designs and create visually appealing website elements and layouts. When developers use a selector, it is easy to select a proper element to design over code. With the help of selectors, developers can apply rules for applying styles to an element based on its attributes, class, or ID. However, there are times when developers need to apply styles to multiple elements at once or select elements with specific attribute values.

The wildcard selector (*) allowed developers to choose any element on a webpage, independent of type or attribute. This is important when applying a consistent style to all elements or making global modifications to a website.

On the other hand, the caret (^) and dollar sign ($) selectors are used to select elements based on their attribute values. The caret selector selects elements that have an attribute value starting with a specific string, while the dollar sign selector selects elements that have an attribute value ending on a specific string.

Using wildcard selectors carefully saves time and energy when styling multiple elements together. It improves website development efficiency by maintaining consistent design throughout the site.

attribute*=”txt”] Selector

The [attribute*=" txt"] selector selects elements whose attribute value contains the specified substring txt. In this example, you can use wildcardLooking to snatch up all those div elements with a specific class. Here's how to do it! with "txt." The class name can appear at the class name's beginning, end, or middle.

  Syntax:

[attribute*="value"] {
   // CSS property
}

Example: Implementation of [attribute*=”txt”] Wildcard Selector

<!DOCTYPE html>
<html>
   <head>
       <style> 
           [class*="txt"] {  /* WE USE * HERE */
               background: green;
               color: white;
           }
           body {
               text-align:center;
               width:60%;
           }
       </style>
   </head>
   <body>
       <!-- Check Apply text -->
       <div class="first_txt">The first div element "txt" Apply.</div>
       <div class="second">The second div element not apply.</div>
       <div class="my-txtt">The third div element my-"txt"t Apply.</div>
       <p class="mytxt">Paragraph Text my"txt" Apply</p>
   </body>
</html>