bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/CSS/CSS Foundations
CSS•CSS Foundations

CSS Structural Pseudo-classes

Concept visual

CSS Structural Pseudo-classes

Graph traversalgraph
ABCDE
current
queued
1
4

Start from A

Structural Pseudo-classes

Formula

Structural pseudo - classes select elements based on their position in the document tree.

The CSS :first-child Pseudo-class

The

Formula

:first - child pseudo - class matches a specific element that is the first child of another element.
Style all first child < p > elements
In the following example, the selector matches all < p > elements that is the first child of any element:

Example p:first-child

{
color: blue;
}

Formula

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

Example p em:first-child

{
color: blue;
}

Formula

Style all < em > elements in all first child < p > elements

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

Example p:first-child em

{
color: blue;
}

The CSS :lang() Pseudo-class

The

:lang()

Formula

pseudo - class is used to select elements with a lang attribute with the specified value.

Example

Formula

Define the quotation marks for < q > elements with lang ="no":

<html> <head> <style>

q:lang(no) {
quotes: "~" "~";
}

</style> </head> <body>

Formula

< p > Some text < q lang ="no"> A quote in a paragraph </q >

Some text.</p> </body> </html>

Other Structural Pseudo-classes

Formula

There are many more structural pseudo - classes available:
:last - child
  • Matches the last child of a parent

Formula

:nth - child(n)
  • Matches the nth child of a parent

Formula

:nth - last - child(n)
  • Matches the nth child from the end

Formula

:only - child
  • Matches an element that is the only child

Formula

:first - of - type
  • Matches the first element of its type

Formula

:last - of - type
  • Matches the last element of its type

CSS Pseudo-classes Reference

Formula

For a complete list of all CSS Pseudo - classes, visit our
CSS Pseudo - classes Reference.

Previous

CSS Interactive Pseudo-classes

Next

CSS Pseudo-elements