CSS Attribute Selector

The CSS Attribute Selector is a very useful approach that targets elements based on their specific attributes or attribute values. This allows for precise styling of HTML elements that share common attributes, enhancing the consistency and efficiency of your CSS code. Lets see how to work with CSS Attribute Selector.

Syntax:

[attribute]{
/* Styles*/
}
// OR
element [attribute] {
/* Styles*/
}
// OR
[attribute ="value "]
{
/* Styles*/
}

There are several types of attribute selectors which are discussed below:

CSS [attribute] Selector

The [attribute] Selector is used to select all the elements that have the specified attribute and applies the CSS property to that attribute. For example, the selector [class] will select all the elements with the style attribute. 

Example 1: This example applies style to all elements with class attributes. 

HTML
<!DOCTYPE html>
<html>
  
<head>
    <title>Attributes selector</title>
    <style>
        [class] {
            text-align: center;
            Color: green;
        }

        .gfg {
            font-size: 40px;
            font-weight: bold;
            margin-bottom: -20px;
        }
    </style>
</head>

<body>
    <div class="gfg">w3wiki</div>
    <p class="Beginner">
        A computer science portal for Beginner
    </p>
</body>
  
</html>

Output:  

This selector can be used to restrict some particular elements, then it needs to specify that element before the attribute selector.

Example 2: This example apply CSS to the div with style attribute only

HTML
<!DOCTYPE html>
<html>
  
<head>
    <title>Attribute selector</title>
    <style>
        div[style] {
            text-align: center;
            color: green;
            font-size: 40px;
            font-weight: bold;
            margin-bottom: -20px;
        }

        p {
            text-align: center;
            font-size: 17px;
        }
    </style>
</head>

<body>
    <div style="color:green">w3wiki</div>
    <p>
        A computer science portal for Beginner
    </p>
</body>
  
</html>

Output: 

Multiple elements can be selected using the comma operator

h2, p[style] {
background-color: #00b93e;
}

CSS [attribute = “value”] Selector

The [attribute = “value”] Selector selector is used to select all the elements whose attribute has the value exactly the same as the specified value. 

Example: 

HTML
<!DOCTYPE html>
<html>
  
<head>
    <title>Attribute selector</title>
    <style>
        [title="gfg"] {
            color: green;
            font-size: 40px;
            font-weight: bold;
            text-align: center;
        }

        [title="Beginner"] {
            font-size: 17px;
            text-align: center;
            margin-top: 0px;
        }
    </style>
</head>

<body>
    <div title="gfg">w3wiki</div>
    <p title="Beginner">
        A computer science portal for Beginner
    </p>
</body>
  
</html>


Output: 

CSS [attribute~=”value”] Selector

The [attribute~=”value”] Selector selector is used to select all the elements whose attribute value is a list of space-separated values, one of which is exactly equal to the specified value. 

Example: 

HTML
<!DOCTYPE html>
<html>
  
<head>
    <title>Attribute selector</title>
    <style>
        [class~="gfg"] {
            color: green;
            font-size: 40px;
            font-weight: bold;
            text-align: center;
        }

        [class~="Beginner"] {
            font-size: 17px;
            text-align: center;
            margin-top: 0px;
        }
    </style>
</head>

<body>
    <div class="gfg">w3wiki</div>
    <div Class="Beginner">A computer science portal for Beginner
    </div>
    <div class="Beginner ide">
        w3wiki is coding platform
    </div>
</body>
  
</html>


Output: 

CSS [attribute|=”value”] Selector

The [attribute|=”value”] Selector This selector is used to select all the elements whose attribute has a hyphen-separated list of values beginning with the specified value. The value has to be a whole word either alone or followed by a hyphen. 

Example: 

HTML
<!DOCTYPE html>
<html>
  
<head>
    <title>Attribute selector</title>
    <style>
        [class|="gfg"] {
            color: green;
            font-size: 40px;
            font-weight: bold;
            text-align: center;
        }

        [class|="Beginner"] {
            font-size: 17px;
            text-align: center;
            margin-top: 0px;
        }
    </style>
</head>

<body>
    <div class="gfg">w3wiki</div>
    <div Class="Beginner-ide">
        A computer science portal for Beginner
    </div>
    <div class="Beginner-ide1">
        w3wiki is coding platform
    </div>
</body>
  
</html>

Output: 

CSS [attribute^=”value”] Selector

The [attribute^=”value”] Selector This selector is used to select all the elements whose attribute value begins with the specified value. The value doesn’t need to be a whole word. 

Example: 

HTML
<!DOCTYPE html>
<html>
  
<head>
    <title>Attribute selector</title>
    <style>
        [class^="gfg"] {
            color: green;
            font-size: 40px;
            font-weight: bold;
            text-align: center;
        }

        [class^="Beginner"] {
            font-size: 17px;
            text-align: center;
            margin-top: 0px;
        }
    </style>
</head>

<body>
    <div class="gfg">w3wiki</div>
    <div Class="Beginner">
        A computer science portal for Beginner
    </div>
    <div class="Beginneride">
        w3wiki is coding platform
    </div>
</body>
  
</html>


Output: 

CSS [attribute$=”value”] Selector

The [attribute$=”value”] Selector This selector is used to select all the elements whose attribute value ends with the specified value. The value doesn’t need to be a whole word. 

Example: 

HTML
<!DOCTYPE html>
<html>
  
<head>
    <title>Attribute selector</title>
    <style>
        [class$="gfg"] {
            color: green;
            font-size: 40px;
            font-weight: bold;
            text-align: center;
        }

        [class$="Beginner"] {
            font-size: 17px;
            text-align: center;
            margin-top: 0px;
        }
    </style>
</head>

<body>
    <div class="gfg">w3wiki</div>
    <div Class="w3wiki">
        A computer science portal for Beginner
    </div>
    <div class="Beginner">
        w3wiki is coding platform
    </div>
</body>
  
</html>

Output: 

CSS [attribute*=”value”] Selector

The [attribute*=”value”] Selector This selector selects all the elements whose attribute value contains the specified value present anywhere. The value doesn’t need to be a whole word. 

Example: 

HTML
<!DOCTYPE html>
<html>
  
<head>
    <title>Attribute selector</title>
    <style>
        [class*="gfg"] {
            color: green;
            font-size: 40px;
            font-weight: bold;
            text-align: center;
        }

        [class*="for"] {
            font-size: 17px;
            text-align: center;
            margin-top: 0px;
        }
    </style>
</head>

<body>
    <div class="gfg">w3wiki</div>
    <div Class="w3wiki">
        A computer science portal for Beginner
    </div>
    <div class="Beginner for">
        w3wiki is coding platform
    </div>
</body>
  
</html>


Output: 



Contact Us