CSS Pseudo-classes


What are Pseudo-classes?

A pseudo-class is a way to describe a unique condition or situation of an element.

For example, it can be used to:

  • Change how something looks when a person moves their mouse over it.
  • Make sure that the links on your webpage have different styles for when someone has already visited them and when they haven't visited them yet.
  • Change how something looks when you click on it.

Mouse Over Me

Anchor Pseudo-classes

You can show links in various styles:


Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective! a:active MUST come after a:hover in the CSS definition in order to be effective! Pseudo-class names are not case-sensitive.


Pseudo-classes and HTML Classes

You can use pseudo-classes together with HTML classes.

When you move your mouse pointer over the link in the example, the color of the link will change:


Hover on <div>

An example of using the :hover pseudo-class on a <div> element:


Simple Tooltip Hover

Hover over a <div> element to show a <p> element (like a tooltip):

Hover over me to show the <p> element.

Tada! Here I am!


CSS - The :first-child Pseudo-class

The ":first-child" pseudo-class is used to select an element that happens to be the very first child of another element.

Match the first <p> element

In this example, the selector finds the very first

element within any other element.


Match the first <i> element in all <p> elements

In this example, we're selecting the very first <i> element inside every <p> element.


Match all <i> elements in all first child <p> elements

In this example, the selector is looking for all <i> elements inside <p> elements that are the first child of some other element.


CSS - The :lang Pseudo-class

The ":lang" pseudo-class helps you create specific rules for various languages in HTML.

In this example, the :lang code specifies how quotation marks should appear around elements that have the attribute lang="no":