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.
<!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> geeksforgeeks.org </a> <br><br> google.com </a> </body> </html> |
Output:

Example 2: This example changes the background-color and text-color of the <a> element by selecting element having [target = “_top”] using CSS.
<!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> geeksforgeeks.org </a> <br><br> google.com </a> </body> </html> |
Output:

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

Recommended Posts:
- How to select elements by attribute in jQuery ?
- HTML | <select> name Attribute
- HTML | <select> required Attribute
- HTML | <select> multiple Attribute
- HTML | <select> autofocus Attribute
- HTML | <select> size Attribute
- HTML | <select> form Attribute
- HTML <select> disabled Attribute
- jQuery UI | Spinner to select numeric data
- How to select all child elements recursively using CSS?
- HTML | data Attribute
- HTML | <object> data Attribute
- D3.js | d3.select() Function
- HTML | <select> Tag
- How to remove items from a select box ?
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.



