How to select elements by data attribute using CSS?

CSS allows to select HTML elements that have specific attributes or attribute values. Element can be selected in number of ways. Some examples are given below:

  • [attribute]: It selects the element with specified attribute.
  • [attribute=”value”]: It selects the elements with a specified attribute and value.
  • [attribute~=”value”]: It selects the elements with an attribute value which contains a specified word.
  • [attribute|=”value”]: It selects the elements with the specified attribute which starts with the specified value.
  • [attribute^=”value”]: It selects the elements in which attribute value begins with a specified value.
  • [attribute$=”value”]: It selects the elements in which attribute value ends with a specified value.
  • [attribute*=”value”]: It selects the elements in which attribute value contains a specified value.

Example 1: This example changes the background-color of <a> element by selecting the element [target] using CSS.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
  
<head
    <title
        Attribute selector in CSS
    </title>
      
    <style>
        a[target] {
            background-color: yellow;
        }
        a {
            font-size: 20px;
        }
    </style>
</head
      
<body style = "text-align:center;"
  
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1
      
    <a href="https://www.geeksforgeeks.org" target="_blank">
        geeksforgeeks.org
    </a>
      
    <br><br>
      
    <a href="https://www.google.com" >
        google.com
    </a
</body
  
</html>                    

chevron_right


Output:



Example 2: This example changes the background-color and text-color of the <a> element by selecting element having [target = “_top”] using CSS.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
  
<head
    <title
        Attribute selector in CSS
    </title>
      
    <style>
        a[target=_top] {
            background-color: green;
            color: white;
        }
        a {
            font-size: 20px;
        }
    </style>
</head
          
<body style = "text-align:center;"
      
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1
      
    <a href="https://www.geeksforgeeks.org" target="_top" >
        geeksforgeeks.org
    </a>
      
    <br><br>
      
    <a href="https://www.google.com" >
        google.com
    </a
</body
  
</html>                    

chevron_right


Output:

Example 3: This example changes the background-color and text-color of the <p> element by selecting element having [class^=”top”] using CSS.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
  
<head
    <title
        Attribute selector in CSS
    </title>
      
    <style>
        [class^="top"] {
            background-color: green;
            color: white;
        }
        p {
            font-size: 20px;
        }
    </style>
</head>     
  
<body style = "text-align:center;"
  
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1
      
    <p class="top-p">A computer science portal</p>
    <p class="topPara">Attribute Selector Example</p>
    <p class="Para">CSS does not applies here</p
</body
  
</html>                    

chevron_right


Output:



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.