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

bookThe Cascade

When you write CSS, you often create multiple rules that could apply to the same element. The cascade is the process by which the browser decides which CSS rule to use when there is a conflict. The browser follows a specific order to resolve these conflicts and apply the correct styles.

First, the browser collects all the rules that target an element. Then, it considers the source of the styles: browser defaults, external stylesheets, internal styles, and inline styles. Inline styles have the highest priority, followed by internal styles, then external styles, and finally browser defaults.

Next, the browser checks the specificity of each rule. Specificity is determined by how the selector targets the element. For example, an ID selector is more specific than a class selector, and a class selector is more specific than a type selector (like h1 or p). If two rules have the same specificity, the rule that appears last in the CSS file takes precedence.

index.html

index.html

style.css

style.css

copy

In the example above, the <h1> element has three rules that could set its color: h1 { color: blue; }, .highlight { color: green; }, and #main-title { color: red; }. The browser compares the specificity of each selector. The ID selector #main-title is the most specific, so its rule is applied and the heading appears red. The class selector .highlight is less specific, so it is ignored for the color property on the heading, but it still applies to the paragraph, which turns green.

Understanding the cascade helps you write CSS that behaves as you expect and makes it easier to troubleshoot when styles do not appear as intended.

question mark

Which CSS rule will determine the color of the <h1> element with both a class of "highlight" and an ID of "main-title"?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 5

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 6.67

bookThe Cascade

Sveip for å vise menyen

When you write CSS, you often create multiple rules that could apply to the same element. The cascade is the process by which the browser decides which CSS rule to use when there is a conflict. The browser follows a specific order to resolve these conflicts and apply the correct styles.

First, the browser collects all the rules that target an element. Then, it considers the source of the styles: browser defaults, external stylesheets, internal styles, and inline styles. Inline styles have the highest priority, followed by internal styles, then external styles, and finally browser defaults.

Next, the browser checks the specificity of each rule. Specificity is determined by how the selector targets the element. For example, an ID selector is more specific than a class selector, and a class selector is more specific than a type selector (like h1 or p). If two rules have the same specificity, the rule that appears last in the CSS file takes precedence.

index.html

index.html

style.css

style.css

copy

In the example above, the <h1> element has three rules that could set its color: h1 { color: blue; }, .highlight { color: green; }, and #main-title { color: red; }. The browser compares the specificity of each selector. The ID selector #main-title is the most specific, so its rule is applied and the heading appears red. The class selector .highlight is less specific, so it is ignored for the color property on the heading, but it still applies to the paragraph, which turns green.

Understanding the cascade helps you write CSS that behaves as you expect and makes it easier to troubleshoot when styles do not appear as intended.

question mark

Which CSS rule will determine the color of the <h1> element with both a class of "highlight" and an ID of "main-title"?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 5
some-alt