close
The Wayback Machine - https://web.archive.org/web/20240601163609/https://www.geeksforgeeks.org/css-attribute-selector/
Open In App

CSS Attribute Selector

Last Updated : 23 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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



Previous Article
Next Article

Similar Reads

CSS [attribute$=value] Selector
The [attribute$=”value”] selector is used to select those elements whose attribute value ends with a specified value "value". The value need not to be present as separate word. It may be a part of another word or expression but it needs to be present at the end. Syntax: [attribute$="value"] { // CSS property } Example 1: In this example, we are usi
1 min read
CSS [attribute=value] Selector
The [attribute=value] selector in CSS is used to select those elements whose attribute value is equal to "value". Syntax: element [attribute = "value"] { // CSS Property } Note: &lt;!DOCTYPE&gt; must be declared for IE8 and earlier versions. Example 1: In this example, The selector h1[id="geeks"] targets the h1 element with id="geeks" and applies a
1 min read
CSS [attribute*=value] Selector
The [attribute*="str"] selector is used to select those elements whose attribute value contains the specified substring str. This sub-string could be at the start, end, or in middle of the class. Syntax: element [attribute*="str"] { // CSS Property } Example: In this example, the selected &lt;p&gt; elements with the class containing "for" will have
1 min read
CSS [attribute|=value] Selector
The [attribute|=value] selector is used to select those elements whose attribute value is equal to "value" or whose attribute value started with "value" immediately followed by a hyphen (-). Note: Use &lt;!DOCTYPE&gt; to run [attribute|=value] selector in IE8 or earlier versions. Syntax: [attributeType|=value] { // CSS Property } Example 1: In this
2 min read
CSS [attribute~=value] Selector
The [attribute~="value"] selector is used to select those elements whose attribute value contains a specified word. The "value" must be present in the attribute as a separate word and not as part of the other word i.e. if [title~=Geeks] is specified then all elements with Geeks title get selected. Syntax: [attribute~=value] { // CSS property } Exam
2 min read
CSS [attribute^=value] Selector
The [attribute^=value] selector is used to select those elements whose attribute value begins with a given attribute. Syntax: [attribute^=value] { // CSS Property } Example: In this example, The CSS selector p[class^="for"] targets &lt;p&gt; elements with a class attribute that starts with "for" and applies a green background color and white text c
1 min read
How to use a "not equal" CSS attribute selector ?
Cascading Style Sheets(CSS) fondly referred to as CSS, is a simply designed language intended to simplify the process of making web pages presentable. CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this independent of the HTML that makes up each web page. CSS is easy to learn and understand, but it provides pow
2 min read
jQuery [attribute!=value] Selector
The jQuery [attribute!=value] selector in jquery is used to select each element that doesn't have the specified attribute and value. Syntax: $("[attribute!='value']")Parameter: attribute: This parameter is required to specify the attribute to be searched.value: This parameter is required to specify the value to be searched.Example 1: C/C++ Code
1 min read
jQuery [attribute*=value] Selector
jQuery ("[attribute*=value]") Selector is used to select all elements with attribute specified by attribute parameter that contains the word specified by the value parameter. Syntax: $("[attribute*='value']")Parameters: This selector has two parameters. attribute: attribute specifies the attributes that need to find.value: value is the string that
2 min read
jQuery [attribute$=value] Selector
The jQuery [attribute$=value] selector is used to select each element with a specific attribute, with a specific ending string. Syntax: $("[attribute$='value']")Parameter: attribute: This parameter is required to specify the attribute to find.value: This parameter is required to specify the string i.e. the value should end with.Example 1: C/C++ Cod
1 min read