Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Combinators | Selecting and Targeting Elements
CSS Foundations

bookCombinators

index.html

index.html

style.css

style.css

copy

Combinators allow you to select elements based on their relationships with other elements in the HTML structure. There are several types of combinators in CSS, each serving a different purpose when targeting elements.

The descendant combinator (a single space) selects all elements that are descendants of a specified ancestor, no matter how deep they are nested. For instance, .container p targets every p element inside .container, whether the p is a direct child or nested further within other elements.

The child combinator (>) is more specific. It selects only those elements that are direct children of a specified parent. For example, ul > li will select only the li elements that are immediate children of a ul, and will not select nested li elements further down the tree.

The adjacent sibling combinator (+) selects an element that is immediately after another element, sharing the same parent. For instance, ul + p targets a p element that comes directly after a ul in the HTML.

Use the descendant combinator when you want to style all matching elements inside a parent, regardless of how deeply they are nested. Use the child combinator when you need to limit your selection to only direct children. The adjacent sibling combinator is useful when you want to style an element that appears immediately after another specific element.

question mark

Which CSS combinator selects only direct children of a specified parent?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4

Spørg AI

expand

Spørg AI

ChatGPT

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

Suggested prompts:

Can you give examples of when to use each combinator in real-world scenarios?

What other types of combinators are there in CSS?

Can you explain the difference between adjacent sibling and general sibling combinators?

Awesome!

Completion rate improved to 6.67

bookCombinators

Stryg for at vise menuen

index.html

index.html

style.css

style.css

copy

Combinators allow you to select elements based on their relationships with other elements in the HTML structure. There are several types of combinators in CSS, each serving a different purpose when targeting elements.

The descendant combinator (a single space) selects all elements that are descendants of a specified ancestor, no matter how deep they are nested. For instance, .container p targets every p element inside .container, whether the p is a direct child or nested further within other elements.

The child combinator (>) is more specific. It selects only those elements that are direct children of a specified parent. For example, ul > li will select only the li elements that are immediate children of a ul, and will not select nested li elements further down the tree.

The adjacent sibling combinator (+) selects an element that is immediately after another element, sharing the same parent. For instance, ul + p targets a p element that comes directly after a ul in the HTML.

Use the descendant combinator when you want to style all matching elements inside a parent, regardless of how deeply they are nested. Use the child combinator when you need to limit your selection to only direct children. The adjacent sibling combinator is useful when you want to style an element that appears immediately after another specific element.

question mark

Which CSS combinator selects only direct children of a specified parent?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4
some-alt