CSS Selectors

CSS selectors are the foundational tools that allow web developers to target and style specific HTML elements. This blog will dive deep into the CSS selectors, from the most basic tag and class selectors to the more complex attribute, pseudo-class, and pseudo-element selectors. Understanding how to use these selectors is key to writing cleaner, more maintainable, and highly specific stylesheets, ultimately paving the way for better web design and development.

We can divide CSS selectors into 5 categories:

  1. Simple Selectors
  2. Combinator Selectors
  3. Pseudo-class Selectors
  4. Pseudo-element Selectors
  5. Attribute selectors

1 – Simple Selectors

Simple selectors select elements based on name, id, and class.

2 – Combinator Selectors

A combinator is something that explains the relationship between the selectors. There are 4 different combinators in CSS.

2.1 – descendant selector (space)

The CSS descendant combinator (a single space) selects elements inside elements. The following example selects all <p> elements inside <div> elements.

2.2 – child selector (>)

The child combinator selects all elements that are direct children of a specified element. The following example selects all <p> elements that are direct children of <div>.

2.3 – adjacent sibling selector (+)

adjacent sibling selector select an element that is directly after a specific element. The following example selects the first <p> element that immediately follows a <div>.

2.4 – general sibling selector (~)

general sibling selector selects all elements that are next siblings of a specified element. The following example selects all <p> elements that are next siblings of <div>.

3 – Pseudo-class Selectors

A Pseudo-class defines a style for a special state of an element. We can use pseudo-class in many ways:

  • Style an element when a user mouses over it
  • Style visited and unvisited links differently
  • Style an element when it gets focus

Here is an example of using the :hover pseudo-class on a <div> element:

Here is an example of using the :focus pseudo-class to style an <input> field when it gets focus:

4 – Pseudo-element Selectors

A pseudo-element is used to style specified parts of an element. It can be used in many ways.

  • Style the first letter, or line of an element
  • Insert content before or after of an element

The ::first-letter pseudo-element is used to add a special style to the first letter of a text.

The ::before and ::after pseudo-elements are used to insert some content before or after the content of a specified element.

5 – Attribute Selectors

CSS attribute selectors are used to select and style HTML elements with a specific attribute or attribute value, or both.

The following example selects all <a> elements with a target attribute:

The following example selects all <a> elements with a target=”_blank” attribute:

The following example selects all elements with a class attribute that contains a space-separated list of words, one of which is “text”

The [attribute|="value"] selector selects elements with the specific attribute, whose value can be exactly the specific value, or start with the specific value followed by a hyphen (-).

The [attribute^="value"] selector selects elements with the specific attribute, whose value starts with a specific value. The following example selects all elements with a class attribute value that starts with “top”.

The [attribute$="value"] selector selects elements whose attribute value ends with a specific value. The following example selects all elements with a class attribute value that ends with “test”.

The [attribute*="value"] selector selects elements whose attribute value contains a specific value. The following example selects all elements with a class attribute value that contains “te”.

CSS Selectors are the fundamental tool for applying styles. You use them to target a specific element, a group of elements, or elements based on their attributes, allowing you to precisely control styling across your web page. Below is a full example illustrating the most common selector types. Feel free to use this example to experiment and gain a clearer understanding of how different selectors work.

See the Pen CSS Selectors by Code Hacks (@code-hacks) on CodePen.

Leave a Comment