Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Structural and Functional Pseudo-Classes | Box Model and Element Spacing
CSS Fundamentals

book
Structural and Functional Pseudo-Classes

These pseudo-classes pertain to the position of an element in the document's hierarchical structure.

first-child pseudo-class

The :first-child pseudo-class targets an element that is the first child of its parent, regardless of its tag or class name. Let's consider the following example to clarify. We have a set of elements, and for only the first element (the first <li> element), we want to set its color property to blue.

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<ul>
<li class="item">Lorem ipsum dolor sit amet.</li>
<li class="item">Lorem, ipsum.</li>
<li class="item">Lorem ipsum dolor sit.</li>
</ul>
<ul>
<li class="item">Lorem ipsum dolor sit amet.</li>
<li class="item">Lorem, ipsum.</li>
<li class="item">Lorem, ipsum dolor.</li>
</ul>
</body>
</html>

We see that the first element was selected, and only for it we change the color property.

last-child pseudo-class

The :last-child pseudo-class targets the last child of its parent, allowing us to modify any of its properties. Let's consider an example to illustrate how we can use this pseudo-class effectively.

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<ul>
<li class="item">Lorem ipsum dolor sit amet.</li>
<li class="item">Lorem ipsum dolor sit.</li>
<li class="item">Lorem, ipsum dolor.</li>
</ul>
<ul>
<li class="item">Lorem ipsum dolor sit amet.</li>
<li class="item">Lorem ipsum dolor sit.</li>
<li class="item">Lorem, ipsum dolor.</li>
</ul>
</body>
</html>

nth-child pseudo-class

The :nth-child pseudo-class targets an element based on its position in the hierarchy. It takes an argument in the form of an expression, usually in the form of an+b, which helps determine the specific child elements to select. Let's break down the components of this expression:

  • a represents the loop period;

  • n is the loop counter, starting at 0 and increasing by 1 with each iteration;

  • b is the offset, influencing the starting point of the selection.

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<ul>
<li class="item">Lorem ipsum dolor sit amet.</li>
<li class="item">Lorem, ipsum.</li>
<li class="item">Lorem ipsum dolor sit.</li>
<li class="item">Lorem, ipsum dolor.</li>
</ul>
</body>
</html>

Let's consider some typical selectors.

css
/* Selects every second element */
.item:nth-child(2n) {
/* Styles for every second element */
}

/* Selects the first three elements */
.item:nth-child(-n + 3) {
/* Styles for the first three elements */
}

/* Selects all odd elements */
.item:nth-child(odd) {
/* Styles for all odd elements */
}

/* Selects the last three elements */
.item:nth-last-child(-n + 3) {
/* Styles for the last three elements */
}

Note

We don't need to remember all the selectors. We can always search for it on Google.

not() pseudo-class

The :not() pseudo-class targets elements that do not match a specified selector. For instance, :not(p) would select all elements except for <p> elements. Let's explore some examples:

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<ul>
<li>Lorem, ipsum.</li>
<li>Lorem ipsum dolor sit.</li>
<li>Lorem, ipsum dolor.</li>
<li>Lorem ipsum dolor sit amet.</li>
<li>Lorem.</li>
</ul>
</body>
</html>

1. What pseudo class can be used to select first element in the set of elements?

2. What is the nth-child pseudo-class used for?

3. How does the last-child pseudo-class work?

question mark

What pseudo class can be used to select first element in the set of elements?

Select the correct answer

question mark

What is the nth-child pseudo-class used for?

Select the correct answer

question mark

How does the last-child pseudo-class work?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 5

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt