Open In App

CSS Attribute Selector

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The CSS Attribute Selector selects an element with some specific attribute or attribute value. It is an excellent way to style the HTML elements by grouping them based on some specific attributes and the attribute selector will select those elements with similar attributes.

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">GeeksforGeeks</div>
    <p class="geeks">
        A computer science portal for geeks
    </p>
</body>
  
</html>

Output: CSS [attribute] Selector Example 

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">GeeksforGeeks</div>
    <p>
        A computer science portal for geeks
    </p>
</body>
  
</html>

Output: CSS [attribute] Selector Example

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="geeks"] {
            font-size: 17px;
            text-align: center;
            margin-top: 0px;
        }
    </style>
</head>

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


Output: CSS [attribute = "value"] Selector Example

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~="geeks"] {
            font-size: 17px;
            text-align: center;
            margin-top: 0px;
        }
    </style>
</head>

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


Output: CSS [attribute~="value"] Selector Example

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|="geeks"] {
            font-size: 17px;
            text-align: center;
            margin-top: 0px;
        }
    </style>
</head>

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

Output: CSS [attribute|="value"] Selector Example

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^="geeks"] {
            font-size: 17px;
            text-align: center;
            margin-top: 0px;
        }
    </style>
</head>

<body>
    <div class="gfg">GeeksforGeeks</div>
    <div Class="geeks">
        A computer science portal for geeks
    </div>
    <div class="geekside">
        GeeksforGeeks is coding platform
    </div>
</body>
  
</html>


Output: CSS [attribute^="value"] Selector Example

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$="geeks"] {
            font-size: 17px;
            text-align: center;
            margin-top: 0px;
        }
    </style>
</head>

<body>
    <div class="gfg">GeeksforGeeks</div>
    <div Class="geeksforgeeks">
        A computer science portal for geeks
    </div>
    <div class="geeks">
        GeeksforGeeks is coding platform
    </div>
</body>
  
</html>

Output: 

CSS [attribute$="value"] Selector Example

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">GeeksforGeeks</div>
    <div Class="geeksforgeeks">
        A computer science portal for geeks
    </div>
    <div class="geeks for">
        GeeksforGeeks is coding platform
    </div>
</body>
  
</html>


Output: 

CSS [attribute*="value"] Selector Example



Last Updated : 14 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads