CSS | Attribute Selector

The CSS Attribute Selector is used to select 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.
There are several types of attribute selectors which are discussed below:

  • [attribute] Selector: This type of 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:
    attribute selector



    This selector is used to restrict some particular elements, then it needs to specify that element before the attribute selector.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:
    attribute selector

    Multiple elements can be selected using comma operator

    h2, p[style] {
        background-color: #00b93e;
    }
  • [attribute = “value”] Selector: This selector is used to select all the elements whose attribute has the value exactly same as the specified value.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:
    attribute selector

  • [attribute~=”value”] Selector: This 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:
    attribute selector

  • [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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:
    attribute selector

  • [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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:
    attribute selector

  • [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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:
    attribute selector

  • [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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:
    attribute selector



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.