CSS | Attribute Selector

03 Dec, 2018
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:<!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:This selector is used to restrict some particular elements, then it needs to specify that element before the attribute selector.Example:<!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:Multiple elements can be selected using comma operatorh2, p[style] {
    background-color: #00b93e;
}
Example:
<!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:
This selector is used to restrict some particular elements, then it needs to specify that element before the attribute selector.Example:
<!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:
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:<!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:
Example:
<!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:
[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:<!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:
Example:
<!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:
[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:<!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:
Example:
<!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:
[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:<!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:
Example:
<!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:
[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:<!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:
Example:
<!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:
[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:<!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:
Example:
<!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-Basics
Picked
CSS
HTML
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.