Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Pseudo-classes in CSS?

Pseudo-classes are a powerful concept in CSS that allows you to apply styles to elements under specific circumstances or states. In this post, we will explore what pseudo-classes are and how to use them effectively in your web development projects.

What are Pseudo-classes?

To understand pseudo-classes, let's first discuss what classes are in CSS. Classes are a way of grouping elements together to apply the same style rules. You can define a class in your CSS file and then assign it to one or more HTML elements. This way, all elements with the same class will have the same style properties.

A pseudo-class, on the other hand, is a way to target elements based on their state or relationship with other elements, rather than adding a class attribute to the HTML element. You can think of a pseudo-class as a special type of class that is automatically assigned to an element under certain conditions.

One analogy for pseudo-classes is to think of them as "filters" or "modifiers" that you can apply to your CSS selectors. They help you target elements more specifically without having to manually add or remove class attributes in your HTML code.

Basic Pseudo-classes

Let's go through some basic pseudo-classes to understand how they work.

:hover

The :hover pseudo-class is used to apply styles to an element when the user hovers their mouse over it. This is a common way to provide visual feedback to users, such as changing the background color of a button or the text color of a link.

Here's an example of how to use the :hover pseudo-class:

button:hover {
  background-color: lightblue;
}

In this example, when a user hovers their mouse over any <button> element, the background color will change to light blue.

:active

The :active pseudo-class is used to style an element when it is being actively interacted with, such as when a user clicks on a button or a link. This pseudo-class can be useful for providing additional visual feedback to users during interactions.

Here's an example of using the :active pseudo-class:

button:active {
  background-color: darkblue;
}

In this example, when a user clicks on a <button> element, the background color will change to dark blue.

:visited

The :visited pseudo-class is used to style links that have been visited by the user. This can be helpful for providing visual cues to users about which links they have already clicked on and which ones they haven't.

Here's an example of using the :visited pseudo-class:

a:visited {
  color: purple;
}

In this example, any visited <a> (anchor) elements will have their text color set to purple.

Advanced Pseudo-classes

Now that we've covered some basic pseudo-classes, let's dive into some more advanced ones.

:first-child and :last-child

The :first-child and :last-child pseudo-classes are used to target the first and last child elements of a parent element, respectively. This can be useful for styling elements in a list or a group without having to add extra classes to the HTML.

Here's an example of using both the :first-child and :last-child pseudo-classes:

ul > li:first-child {
  font-weight: bold;
}

ul > li:last-child {
  font-style: italic;
}

In this example, the first <li> element within a <ul> element will have its text bolded, and the last <li> element will have its text in italics.

:nth-child

The :nth-child pseudo-class allows you to target elements based on their position in a group of siblings. This is a powerful way to apply styles to elements in a repeating pattern or to target specific elements in a list.

Here's an example of using the :nth-child pseudo-class to apply a striped background to a table:

tr:nth-child(odd) {
  background-color: lightgrey;
}

tr:nth-child(even) {
  background-color: white;
}

In this example, odd-numbered <tr> (table row) elements will have a light grey background, while even-numbered <tr> elements will have a white background.

:not

The :not pseudo-class allows you to exclude elements that match a certain selector from being targeted. This is useful for applying styles to elements that don't meet specific criteria.

Here's an example of using the :not pseudo-class to style all buttons except those with a .disabled class:

button:not(.disabled) {
  background-color: blue;
}

button.disabled {
  background-color: grey;
}

In this example, all <button> elements without the .disabled class will have a blue background, while those with the .disabled class will have a grey background.

Conclusion

Pseudo-classes are a powerful way to apply styles to elements based on their state or relationships with other elements. They can help you write more efficient and maintainable CSS by targeting elements more specifically without having to manually add or remove class attributes in your HTML code.

In this post, we've covered some of the most commonly used pseudo-classes, such as :hover, :active, :visited, :first-child, :last-child, :nth-child, and :not. By understanding and utilizing these pseudo-classes, you can create more dynamic and interactive styles for your web projects.