CSS Attribute Selectors

Introduction

 
You probably have dealt with a lot of CSS selectors already, such as class, ids and etc. However, we can’t ignore attribute selectors. Honestly, attribute selectors are my favorite because they are much easier to work with.
 
In this post, we are going to tackle the different types of CSS attribute selectors. Will explore from the simple attribute selectors up to the sophisticated ones. OK, then let’s get started.
 

What is an Attribute Selector?

 
Attribute selectors were introduced in CSS2, and, from the name itself, you’ll be able to specify rules that match elements based on their attributes – such as href or title – and of course the values of those attributes.
 
Attribute selectors defined in CSS2
  • Simple Attribute Selector
  • Exact Attribute Value Selector
  • Partial Attribute Value Selector
  • Language Attribute Selector
Attribute Selectors in CSS3
  • Beginning Sub-string Attribute Value Selector
  • Ending Sub-string Attribute Value Selector
  • Arbitrary Sub-string Attribute Value Selector
Before starting with the examples, will be using this HTML below as the main source that will be used with entire CSS examples.
  1. <!DOCTYPE html>    
  2. <html lang="en">    
  3. <head>    
  4. <meta charset="UTF-8">    
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">    
  6. <title>CSS Attribute Selectors</title>    
  7. <body>    
  8. <div>    
  9.     <p title="List of famous programming books in my opinion">List of famous programming books in my opinion</p>    
  10.     <ul>    
  11.         <li>    
  12.             <a href="https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882"    
  13.                 title="Clean Code: A Handbook of Agile Software Craftsmanship" target="__blank">Clean Code</a>    
  14.         </li>    
  15.         <li>    
  16.             <a href="https://www.amazon.com/Pragmatic-Programmer-Journeyman-Master/dp/020161622X"    
  17.                 title="The Pragmatic Programmer: From Journeyman to Master" target="__blank">The Pragmatic    
  18.                 Programmer</a>    
  19.         </li>    
  20.         <li>    
  21.             <a href="https://www.amazon.com/Refactoring-Improving-Existing-Addison-Wesley-Signature-ebook/dp/B07LCM8RG2/"    
  22.                 title="Refactoring: Improving the Design of Existing Code (2nd Edition) (Addison-Wesley Signature Series (Fowler))"    
  23.                 target="__blank">Refactoring</a>    
  24.         </li>    
  25.         <li>    
  26.             <a href="https://www.amazon.com/Self-Taught-Programmer-Definitive-Programming-Professionally-ebook/dp/B01M01YDQA/"    
  27.                 title="The Self-Taught Programmer: The Definitive Guide to Programming Professionally"    
  28.                 target="__blank" lang="en-US">The Self-Taught Programmer</a></li>    
  29.     </ul>    
  30. </div>    
  31. </body>    
  32. </html>     
HTML Output
 

Simple Attribute Selector

 
From the word itself, simple attribute selectors apply rules to elements that have the specified attribute, regardless of that attribute's value. Let's see an example. 
  1. [title]{  
  2.       
  3.     color:#36454f;  
  4.     font-size:12px;  
  5.     font-weightbold;  
  6. }  
With the example above, all of the elements that have a title attribute despite different values will apply the given styles. 
 
Output
 

Exact Attribute Value Selector

 
If you want to be more specific, we can use this exact attribute value selector. You just need to know what value you need to apply with the styles. Let's see an example. 
  1. /*  
  2.  * Exact attribute value selector 
  3.  */  
  4. a[title='Clean Code: A Handbook of Agile Software Craftsmanship']{  
  5.     color:#36454f;  
  6.     font-size:20px/*Let's make the font-size a bit larger so it will stand-out*/  
  7.     font-weightbold;  
  8.     font-style:italic;  
  9. }   
Output
 

Partial Attribute Value Selector

 
If you want to select elements that contain a specific word we can use this partial attribute value selector. Let's see an example below. 
  1. /*  
  2.  * Partial attribute value selector 
  3.  */  
  4. a[title~='Programmer:']{  
  5.     color:#36454f;  
  6.     font-size:20px/*Let's make the font-size a bit larger so it will stand-out*/  
  7.     font-weightbold;  
  8.     font-style:italic;  
  9. }  
Output
 

Language Attribute Selector

 
This selector applies rules to elements that have an attribute matching the first argument in the selector. Let's see an example below. 
  1. /*  
  2.  * Language attribute value selector 
  3.  */  
  4. a[lang|='en']{  
  5.     color:#36454f;  
  6.     font-size:20px/*Let's make the font-size a bit larger so it will stand-out*/  
  7.     font-weightbold;  
  8.     font-style:italic;  
  9. }  
Output
 

Beginning Sub-string Attribute Value Selector

 
This selector finds the elements whose chosen attribute begins/starts with the string passed to it as an argument. Let's see an example below. 
  1. /*  
  2.  * Begin substring attribute value selector 
  3.  */  
  4. a[href^='http']{  
  5.     color:#36454f;  
  6.     background-imageurl("../images/link-24.png");  
  7.     background-repeatno-repeat;  
  8.     background-positionright;  
  9.     displayblock;  
  10.     width:250px;  
  11.     height30px;  
  12. }  
In the example shown, we have a 24px image in height so, in order to fit in the list, we have changed the display value to block and set the height to 30px so the image will fit in the ordered-list. 
 
Output
 

Ending Sub-string Attribute Value Selector

 
This selector is exactly the opposite of the beginning substring attribute value selector. This means that you can use it to select attributes that end with a supplied value. Let's see an example below:
  1. a[href^='http']{  
  2.      
  3.     text-decorationnone;  
  4. }  
  5.   
  6. /* 
  7.  * Ending Substring Attribute Value Selector 
  8.  */  
  9.  a[title$='Master']{  
  10.     color:#36454f;  
  11.     border1px solid green;  
  12.     outline1px solid black;  
  13.  }  
Output

Arbitrary Sub-string Attribute Value Selector

 
This attribute selector works in the same way as the beginning and ending substring attribute selector. However, it searches the value anywhere in the provided attribute string. Let's see an example below:
  1. /* Arbitrary Substring Attribute value selector 
  2. as long as the string contains a certain value the  
  3. styles will be applied. 
  4. */  
  5. a[href*='book']{  
  6.   
  7.     color : #6e6456;  
  8.     font-weightbold;  
  9.     text-decorationnone;  
  10.     font-styleitalic;  
  11.     displayblock;  
  12.     padding:10px;  
  13. }  
Output

Summary

 
In this article, we have discussed the following:
  • Attribute selectors defined in CSS2:

    • Simple Attribute Selector
    • Exact Attribute Value Selector
    • Partial Attribute Value Selector
    • Language Attribute Selector

  • Attribute Selectors in CSS3

    • Beginning Sub-string Attribute Value Selector
    • Ending Sub-string Attribute Value Selector
    • Arbitrary Sub-string Attribute Value Selector
I hope you have enjoyed this article, as I have enjoyed writing it. Until next time, happy programming!
 
References