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:
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:
This selector is used to restrict some particular elements, then it needs to specify that element before the attribute selector.
Example:
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:
Multiple elements can be selected using the 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 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: 
[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:
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: 
[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: 
[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: 
[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:

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Jul, 2023
Like Article
Save Article