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.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>CSS Attribute Selectors</title>
- <body>
- <div>
- <p title="List of famous programming books in my opinion">List of famous programming books in my opinion</p>
- <ul>
- <li>
- <a href="https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882"
- title="Clean Code: A Handbook of Agile Software Craftsmanship" target="__blank">Clean Code</a>
- </li>
- <li>
- <a href="https://www.amazon.com/Pragmatic-Programmer-Journeyman-Master/dp/020161622X"
- title="The Pragmatic Programmer: From Journeyman to Master" target="__blank">The Pragmatic
- Programmer</a>
- </li>
- <li>
- <a href="https://www.amazon.com/Refactoring-Improving-Existing-Addison-Wesley-Signature-ebook/dp/B07LCM8RG2/"
- title="Refactoring: Improving the Design of Existing Code (2nd Edition) (Addison-Wesley Signature Series (Fowler))"
- target="__blank">Refactoring</a>
- </li>
- <li>
- <a href="https://www.amazon.com/Self-Taught-Programmer-Definitive-Programming-Professionally-ebook/dp/B01M01YDQA/"
- title="The Self-Taught Programmer: The Definitive Guide to Programming Professionally"
- target="__blank" lang="en-US">The Self-Taught Programmer</a></li>
- </ul>
- </div>
- </body>
- </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.
- [title]{
- color:#36454f;
- font-size:12px;
- font-weight: bold;
- }
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.
- /*
- * Exact attribute value selector
- */
- a[title='Clean Code: A Handbook of Agile Software Craftsmanship']{
- color:#36454f;
- font-size:20px; /*Let's make the font-size a bit larger so it will stand-out*/
- font-weight: bold;
- font-style:italic;
- }
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.
- /*
- * Partial attribute value selector
- */
- a[title~='Programmer:']{
- color:#36454f;
- font-size:20px; /*Let's make the font-size a bit larger so it will stand-out*/
- font-weight: bold;
- font-style:italic;
- }






Roshan RathodPosted Jul 23, 2020, 12:50 AM
Nice ..keep it up sir