CSS Pseudo-classes

What are pseudo-classes?

Pseudo-classes are used to define the special state of elements.

For example, it can be used for:

  • Set the style when the mouse hovers over the element
  • Set different styles for visited and unvisited links
  • Set the style when the element gets focused
Hover over me

Syntax

Syntax of pseudo-classes:

selector:pseudo-class {
  property: value;
}

Anchor pseudo-class

Links can be displayed in different ways:

Examples

/* Unvisited link */
a:link {
  color: #FF0000;
}
/* Visited link */
a:visited {
  color: #00FF00;
}
/* Hovering over the link */
a:hover {
  color: #FF00FF;
}
/* Selected link */
a:active {
  color: #0000FF;
}

Try it yourself

Note:a:hover to take effect! The pseudo-class name is case-insensitive. a:link and a:visited to take effect!a:active to take effect! The pseudo-class name is case-insensitive. a:hover must be defined after the CSS properties,

Pseudo-classes and CSS classes

Pseudo-classes can be used with CSS classes:

When you hover over the link in the example, it changes color:

Examples

a.highlight:hover {
  color: #ff0000;
}

Try it yourself

Hover over <div>

Used on <div> element :hover Instance of pseudo-class:

Examples

div:hover {
  background-color: blue;
}

Try it yourself

Simple tooltip hover

Hover over the <div> element to display the <p> element (similar to a tooltip effect):

Hover over me to display the <p> element.

Haha, I'm here!

Examples

p {
  display: none;
  background-color: yellow;
  padding: 20px;
}
div:hover p {
  display: block;
}

Try it yourself

CSS - :first-child pseudo-class

:first-child Pseudo-classes match the specified element: the element is the first child of another element.

Match the first <p> element

In the following example, the selector matches any <p> element that is the first child of any element:

Examples

p:first-child {
  color: blue;
}

Try it yourself

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

In the following example, the selector matches the first <i> element within all <p> elements:

Examples

p i:first-child {
  color: blue;
}

Try it yourself

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

In the following example, the selector matches all <i> elements within the <p> element that is the first child of another element:

Examples

p:first-child i {
  color: blue;
}

Try it yourself

CSS - :lang pseudo-class

:lang Pseudo-classes allow you to define special rules for different languages.

In the following example,:lang Define quotes for <q> elements with the attribute lang="en":

Examples

<html>
<head>
<style>
q:lang(en) {
  quotes: "~" "~";
}
</style>
</head>
<body>
<p>Some text <q lang="no">A quote in a paragraph</q> Some text.</p>
</body>
</html>

Try it yourself

More examples

Add different styles to hyperlinks
This example demonstrates how to add other styles to hyperlinks.
Using :focus
This example demonstrates how to use the :focus pseudo-class.

All CSS pseudo-classes

Selector Example Example Description
:active a:active Select active links.
:checked input:checked Select each checked <input> element.
:disabled input:disabled Select each disabled <input> element.
:empty p:empty Select each <p> element that does not have child elements.
:enabled input:enabled Select each enabled <input> element.
:first-child p:first-child Select each <p> element that is the first child of its parent element.
:first-of-type p:first-of-type Select each <p> element that is the first child of its parent.
:focus input:focus Select the <input> element that is focused.
:hover a:hover Select the link that the mouse is hovering over.
:in-range input:in-range Select <input> elements with values within a specified range.
:invalid input:invalid Select all <input> elements with invalid values.
:lang(language) p:lang(it) Select each <p> element whose lang attribute value starts with "it".
:last-child p:last-child Select each <p> element that is the last child of its parent element.
:last-of-type p:last-of-type Select each <p> element that is the last child of its parent.
:link a:link Select all unvisited links.
:not(selector) :not(p) Select each element that is not a <p> element.
:nth-child(n) p:nth-child(2) Select each <p> element that is the second child of its parent.
:nth-last-child(n) p:nth-last-child(2) Select each <p> element that is the second child of its parent element, counting from the last child.
:nth-last-of-type(n) p:nth-last-of-type(2) Select each <p> element that is the second child of its parent element, counting from the last child.
:nth-of-type(n) p:nth-of-type(2) Select each <p> element that is the second child of its parent.
:only-of-type p:only-of-type Select each <p> element that is the only child of its parent element.
:only-child p:only-child Select the <p> element that is the only child of its parent.
:optional input:optional Select <input> elements that do not have the "required" attribute.
:out-of-range input:out-of-range Select <input> elements with values out of the specified range.
:read-only input:read-only Select <input> elements that have specified the "readonly" attribute.
:read-write input:read-write Select <input> elements that do not have the "readonly" attribute.
:required input:required Select <input> elements that have specified the "required" attribute.
:root root Select the root element of an element.
:target #news:target Select the current active #news element (click on the URL containing the anchor name).
:valid input:valid Select all <input> elements with valid values.
:visited a:visited Select all visited links.

All CSS Pseudo-elements

Selector Example Example Description
::after p::after Insert content after each <p> element.
::before p::before Insert content before each <p> element.
::first-letter p::first-letter Select the first letter of each <p> element.
::first-line p::first-line Select the first line of each <p> element.
::selection p::selection Select the part of the element chosen by the user.