Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre 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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 5

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain more about CSS specificity and how it's calculated?

What happens if two selectors have the same specificity and are declared in different stylesheets?

Can you give more examples of how the cascade affects styling in complex layouts?

Awesome!

Completion rate improved to 6.67

bookThe Cascade

Glissez pour afficher le menu

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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 5
some-alt