WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

CSS Pseudo-classes


What are Pseudo-classes?

The CSS pseudo-classes allow us to style elements, or parts of elements, that exist in the document tree without using JavaScript or any other scripts


Syntax

The syntax of pseudo-classes:

selector:pseudo-class {
    property:value;
}

Anchor Pseudo-classes

Links can be displayed in different ways:

Example

/* unvisited link */
a:link {
    color: hotpink;
}

/* visited link */
a:visited {
    color: green;
}

/* mouse over link */
a:hover {
    color:yellow;
}

/* selected link */
a:active {
    color: red;
}
Try it yourself »

Pseudo-classes and CSS Classes

Pseudo-classes can be combined with CSS classes:

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

Example

a.highlight:hover {
    color: hotpink;
}
Try it yourself »

Hover on <div>

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

Example

div:hover {
    background-color: blue;
}
Try it yourself »

CSS - The :first-child Pseudo-class

The :first-child pseudo-class matches a specified element that 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:

Example

p:first-child {
    color: blue;
}
Try it yourself »

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

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

Example

p i:first-child {
    color: blue;
}
Try it yourself »

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

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

Example

p:first-child i {
    color: blue;
}
Try it yourself »

CSS - The :lang Pseudo-class

The :lang pseudo-class allows you to define special rules for different languages.

In the example below, :lang defines the quotation marks for <q> elements with lang="no":

Example

<html>
<head>
<style>
q:lang(no) {
    quotes: "~" "~";

}
</style>
</head>

<body>
<p>Some text <q lang="no">A quote in a paragraph</q> Some text.</p>
</body>
</html>
Try it yourself »