Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Cascade in CSS | Selectors and Cascade
Introduction to CSS Part I
course content

Зміст курсу

Introduction to CSS Part I

Introduction to CSS Part I

1. Basic Introduction
2. Selectors and Cascade
3. Box Model

bookCascade in CSS

CSS has multiple aspects of how the styles get applied, and one of these is the cascade, which refers to how style rules are applied to elements on a web page. It determines the priority of different style rules and how they should be applied when multiple rules apply to the same element. Here's how the cascade works in CSS:

Specificity: When multiple style rules apply to the same element, the rule with the highest specificity will take precedence. Specificity is calculated based on the number and type of selectors used in the rule. For example, an id selector has a higher specificity than a class selector, which has a higher specificity than an element selector.

Origin: When multiple style rules have the same specificity, the rule that comes from the highest origin will take precedence. There are three origins in CSS:

  • Author: These are styles defined by the website developer in the CSS file.
  • User: These are styles defined by the user in their browser settings (e.g., a custom stylesheet).
  • User agent: These are styles defined by the browser itself (e.g., default styles for HTML elements).

The author styles have the highest precedence, followed by user styles and user agent styles.

Order: When multiple style rules have the same specificity and origin, the last one defined will take precedence. This means that the later a rule appears in the CSS file, the higher its priority will be.

Here is an example of how the cascade works in CSS:

In this example, three style rules apply to h1 elements:

  1. The first rule is a user agent style that sets the font size to 24px.
  2. The second rule is an author style that targets h1 elements within an element with the id attribute of the header. It sets the font size to 36px and the color to red. This rule has a higher specificity than the first rule, so it takes precedence.
  3. The third rule is also an author style that targets all h1 elements. It sets the font weight to bold and the text-decoration to underline. This rule has the same specificity as the first rule, but it appears later in the CSS file, so it takes precedence.

As a result, the h1 elements on the page will have a font size of 36px, a color of red, a font weight of bold, and a text-decoration of underline.

html

index

css

index

js

index

copy

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 4
We're sorry to hear that something went wrong. What happened?
some-alt