Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Structural and Functional Pseudo-Classes | Box Model and Layout
CSS Fundamentals
course content

Kursinhalt

CSS Fundamentals

CSS Fundamentals

1. Introduction to CSS
2. Text Styles
3. Box Model and Layout
4. Flexbox
5. Decorative Effects

book
Structural and Functional Pseudo-Classes

Pseudo-classes allow you to target elements precisely based on their order within a container. Let's explore some of the most commonly used ones.

:first-child

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

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

:last-child

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

:nth-child

The :nth-child pseudo-class allows you to select elements based on their position within a list of siblings. You can start with simple targeting by specifying a number. For example:

html

index.html

css

index.css

copy

This will style only the third element in the list.

Advanced :nth-child

For more complex scenarios, you can use the formula an+b to select multiple elements based on their position. Here's how the formula works:

  • a determines the repetition pattern (e.g., every 2nd, 3rd child, etc.);
  • b sets the starting point or offset for the selection;
  • n acts as the counter that increments with each iteration, starting at 0.

Let's consider some typical selectors.

Note

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

:not()

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

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?

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

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

Wählen Sie die richtige Antwort aus

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

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

Wählen Sie die richtige Antwort aus

How does the `last-child` pseudo-class work?

How does the last-child pseudo-class work?

Wählen Sie die richtige Antwort aus

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 5
We're sorry to hear that something went wrong. What happened?
some-alt