Skip to content

Commit

Permalink
Move specificity under cascade
Browse files Browse the repository at this point in the history
  • Loading branch information
estelle committed Feb 12, 2025
1 parent 32a1f3f commit e272cdb
Show file tree
Hide file tree
Showing 49 changed files with 116 additions and 115 deletions.
3 changes: 2 additions & 1 deletion files/en-us/_redirects.txt
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@
/en-US/docs/CSS/Scaling_background_images /en-US/docs/Web/CSS/CSS_backgrounds_and_borders/Resizing_background_images
/en-US/docs/CSS/Scaling_of_SVG_backgrounds /en-US/docs/Web/CSS/Scaling_of_SVG_backgrounds
/en-US/docs/CSS/Shorthand_properties /en-US/docs/Web/CSS/Shorthand_properties
/en-US/docs/CSS/Specificity /en-US/docs/Web/CSS/Specificity
/en-US/docs/CSS/Specificity /en-US/docs/Web/CSS/CSS_cascade/Specificity
/en-US/docs/CSS/Syntax /en-US/docs/Web/CSS/Syntax
/en-US/docs/CSS/Text-overflow_TEST /en-US/docs/Web/CSS/text-overflow
/en-US/docs/CSS/Tutorial/Using_CSS_flexible_boxes /en-US/docs/Web/CSS/CSS_flexible_box_layout/Basic_concepts_of_flexbox
Expand Down Expand Up @@ -11958,6 +11958,7 @@
/en-US/docs/Web/CSS/Reference/object-position /en-US/docs/Web/CSS/object-position
/en-US/docs/Web/CSS/Selector_lists /en-US/docs/Web/CSS/Selector_list
/en-US/docs/Web/CSS/Selectors /en-US/docs/Web/CSS/CSS_Selectors
/en-US/docs/Web/CSS/Specificity /en-US/docs/Web/CSS/CSS_cascade/Specificity
/en-US/docs/Web/CSS/Text-overflow_TEST /en-US/docs/Web/CSS/text-overflow
/en-US/docs/Web/CSS/Tools /en-US/docs/Web/CSS
/en-US/docs/Web/CSS/Tools/Border-image_generator /en-US/docs/Web/CSS/CSS_backgrounds_and_borders/Border-image_generator
Expand Down
106 changes: 53 additions & 53 deletions files/en-us/_wikihistory.json
Original file line number Diff line number Diff line change
Expand Up @@ -76239,6 +76239,59 @@
"Fredchat"
]
},
"Web/CSS/CSS_cascade/Specificity": {
"modified": "2020-11-14T16:01:00.890Z",
"contributors": [
"mfuji09",
"An-Error94",
"ExE-Boss",
"elias6",
"Sajag",
"Aerodium",
"CodeLight",
"estelle",
"tommygebru",
"tama200",
"mfluehr",
"sekkuar",
"rileym7",
"Anawriter1",
"dustinryerson",
"yannicka",
"Artoria2e5",
"RobinHu",
"david_ross",
"chrisprobably",
"jnachtigall",
"fcard",
"jsx",
"rolfedh",
"PushpitaPikuDey",
"Tigt",
"kmhrussell17",
"kustolovic",
"SeriouslyAwesome",
"mina86",
"diraniyoussef",
"rralian",
"mflodin",
"ahmedsai001",
"dawsonda",
"mkollra01",
"applescr",
"Sebastianz",
"SphinxKnight",
"kscarfone",
"pablofiumara",
"FredB",
"teoli",
"ethertank",
"Sheppy",
"MattBrubeck",
"fryn",
"jwalker"
]
},
"Web/CSS/CSS_cascading_variables": {
"modified": "2020-07-07T12:03:44.891Z",
"contributors": ["wbamberg", "mfluehr"]
Expand Down Expand Up @@ -78426,59 +78479,6 @@
"alecananian"
]
},
"Web/CSS/Specificity": {
"modified": "2020-11-14T16:01:00.890Z",
"contributors": [
"mfuji09",
"An-Error94",
"ExE-Boss",
"elias6",
"Sajag",
"Aerodium",
"CodeLight",
"estelle",
"tommygebru",
"tama200",
"mfluehr",
"sekkuar",
"rileym7",
"Anawriter1",
"dustinryerson",
"yannicka",
"Artoria2e5",
"RobinHu",
"david_ross",
"chrisprobably",
"jnachtigall",
"fcard",
"jsx",
"rolfedh",
"PushpitaPikuDey",
"Tigt",
"kmhrussell17",
"kustolovic",
"SeriouslyAwesome",
"mina86",
"diraniyoussef",
"rralian",
"mflodin",
"ahmedsai001",
"dawsonda",
"mkollra01",
"applescr",
"Sebastianz",
"SphinxKnight",
"kscarfone",
"pablofiumara",
"FredB",
"teoli",
"ethertank",
"Sheppy",
"MattBrubeck",
"fryn",
"jwalker"
]
},
"Web/CSS/Subsequent-sibling_combinator": {
"modified": "2020-10-15T21:04:16.746Z",
"contributors": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ page-type: learn-module-chapter

{{LearnSidebar}}

This lesson aims to introduce you to [cascade layers](/en-US/docs/Web/CSS/@layer), a more advanced feature that builds on the fundamental concepts of the [CSS cascade](/en-US/docs/Web/CSS/Cascade) and [CSS specificity](/en-US/docs/Web/CSS/Specificity).
This lesson aims to introduce you to [cascade layers](/en-US/docs/Web/CSS/@layer), a more advanced feature that builds on the fundamental concepts of the [CSS cascade](/en-US/docs/Web/CSS/Cascade) and [CSS specificity](/en-US/docs/Web/CSS/CSS_cascade/Specificity).

If you are new to CSS, working through this lesson may seem less relevant immediately and a little more academic than some other parts of the course. However, knowing the basics of what cascade layers are should you encounter them in your projects is helpful. The more you work with CSS, understanding cascade layers and knowing how to leverage their power will save you from a lot of pain managing a code base with CSS from different parties, plugins, and development teams.

Expand Down Expand Up @@ -45,7 +45,7 @@ The 'C' in CSS stands for "Cascading". It is the method by which styles cascade
2. **Importance:** Sort rules based on whether they are normal or important. Important styles are those that have the [`!important`](/en-US/docs/Web/CSS/important) flag set.
3. **Origin:** Within each of the two importance buckets, sort rules by author, user, or user-agent origin.
4. **Cascade layers:** Within each of the six origin importance buckets, sort by cascade layer. The layer order for normal declarations is from the first layer created to the last, followed by unlayered normal styles. This order is inverted for important styles, with unlayered important styles having the lowest precedence.
5. **Specificity:** For competing styles in the origin layer with precedence, sort declarations by [specificity](/en-US/docs/Web/CSS/Specificity).
5. **Specificity:** For competing styles in the origin layer with precedence, sort declarations by [specificity](/en-US/docs/Web/CSS/CSS_cascade/Specificity).
6. **Scoping proximity**: When two selectors in the origin layer with precedence have the same specificity, the property value within scoped rules with the smallest number of hops up the DOM hierarchy to the scope root wins. See [How `@scope` conflicts are resolved](/en-US/docs/Web/CSS/@scope#how_scope_conflicts_are_resolved) for more details and an example.
7. **Order of appearance:** When two selectors in the origin layer with precedence have the same specificity and scope proximity, the property value from the last declared selector with the highest specificity wins.

Expand All @@ -68,9 +68,9 @@ The "user-agent" is the browser. The "user" is the site visitor. The "author" is

### Origin and specificity

For each property, the declaration that "wins" is the one from the origin with precedence based on the weight (normal or important). Ignoring layers for the moment, the value from the origin with the highest precedence gets applied. If the winning origin has more than one property declaration for an element, the [specificity](/en-US/docs/Web/CSS/Specificity) of the selectors for those competing property values are compared. Specificity is never compared between selectors from different origins.
For each property, the declaration that "wins" is the one from the origin with precedence based on the weight (normal or important). Ignoring layers for the moment, the value from the origin with the highest precedence gets applied. If the winning origin has more than one property declaration for an element, the [specificity](/en-US/docs/Web/CSS/CSS_cascade/Specificity) of the selectors for those competing property values are compared. Specificity is never compared between selectors from different origins.

In the example below, there are two links. The first has no author styles applied, so only user-agent styles are applied (and your personal user styles, if any). The second has [`text-decoration`](/en-US/docs/Web/CSS/text-decoration) and [`color`](/en-US/docs/Web/CSS/color) set by author styles even though the selector in the author stylesheet has a specificity of [`0-0-0`](/en-US/docs/Web/CSS/Specificity#selector_weight_categories). The reason why author styles "win" is because when there are conflicting styles from different origins, the rules from the origin with precedence are applied, irrespective of the specificity in the origin that doesn't have precedence.
In the example below, there are two links. The first has no author styles applied, so only user-agent styles are applied (and your personal user styles, if any). The second has [`text-decoration`](/en-US/docs/Web/CSS/text-decoration) and [`color`](/en-US/docs/Web/CSS/color) set by author styles even though the selector in the author stylesheet has a specificity of [`0-0-0`](/en-US/docs/Web/CSS/CSS_cascade/Specificity#selector_weight_categories). The reason why author styles "win" is because when there are conflicting styles from different origins, the rules from the origin with precedence are applied, irrespective of the specificity in the origin that doesn't have precedence.

```html live-sample___basic-cascade
<p><a href="https://example.org">User agent styles</a></p>
Expand All @@ -86,7 +86,7 @@ In the example below, there are two links. The first has no author styles applie

{{EmbedLiveSample("basic-cascade")}}

The "competing" selector in the user-agent stylesheet at the time of this writing is `a:any-link`, which has a specificity weight of `0-1-1`. While this is greater than the `0-0-0` selector in the author stylesheet, even if the selector in your current user agent is different, it doesn't matter: the specificity weights from author and user-agent origins are never compared. Learn more about [how specificity weight is calculated](/en-US/docs/Web/CSS/Specificity#how_is_specificity_calculated).
The "competing" selector in the user-agent stylesheet at the time of this writing is `a:any-link`, which has a specificity weight of `0-1-1`. While this is greater than the `0-0-0` selector in the author stylesheet, even if the selector in your current user agent is different, it doesn't matter: the specificity weights from author and user-agent origins are never compared. Learn more about [how specificity weight is calculated](/en-US/docs/Web/CSS/CSS_cascade/Specificity#how_is_specificity_calculated).

Origin precedence always wins over selector specificity. If an element property is styled with a normal style declaration in multiple origins, the author style sheet will always override the redundant normal properties declared in a user or user-agent stylesheet. If the style is important, the user-agent stylesheet will always win over author and user styles. Cascade origin precedence ensures specificity conflicts between origins never happen.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ While working through this lesson may seem less relevant immediately and a littl

CSS stands for **Cascading Style Sheets**, and that first word _cascading_ is incredibly important to understand — the way that the cascade behaves is key to understanding CSS.

At some point, you will be working on a project and you will find that the CSS you thought should be applied to an element is not working. Often, the problem is that you create two rules that apply different values of the same property to the same element. [**Cascade**](/en-US/docs/Web/CSS/Cascade) and the closely-related concept of [**specificity**](/en-US/docs/Web/CSS/Specificity) are mechanisms that control which rule applies when there is such a conflict. The rule that's styling your element may not be the one you expect, so you need to understand how these mechanisms work.
At some point, you will be working on a project and you will find that the CSS you thought should be applied to an element is not working. Often, the problem is that you create two rules that apply different values of the same property to the same element. [**Cascade**](/en-US/docs/Web/CSS/Cascade) and the closely-related concept of [**specificity**](/en-US/docs/Web/CSS/CSS_cascade/Specificity) are mechanisms that control which rule applies when there is such a conflict. The rule that's styling your element may not be the one you expect, so you need to understand how these mechanisms work.

Also significant here is the concept of [**inheritance**](/en-US/docs/Web/CSS/Inheritance), which means that some CSS properties by default inherit values set on the current element's parent element and some don't. This can also cause some behavior that you might not expect.

Expand Down Expand Up @@ -70,7 +70,7 @@ h1 {

### Specificity

[Specificity](/en-US/docs/Web/CSS/Specificity) is the algorithm that the browser uses to decide which property value is applied to an element. If multiple style blocks have different selectors that configure the same property with different values and target the same element, specificity decides the property value that gets applied to the element. Specificity is basically a measure of how specific a selector's selection will be:
[Specificity](/en-US/docs/Web/CSS/CSS_cascade/Specificity) is the algorithm that the browser uses to decide which property value is applied to an element. If multiple style blocks have different selectors that configure the same property with different values and target the same element, specificity decides the property value that gets applied to the element. Specificity is basically a measure of how specific a selector's selection will be:

- An element selector is less specific; it will select all elements of that type that appear on a page, so it has less weight. Pseudo-element selectors have the same specificity as regular element selectors.
- A class selector is more specific; it will select only the elements on a page that have a specific `class` attribute value, so it has more weight. Attribute selectors and pseudo-classes have the same weight as a class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ These tutorials are not part of the learning pathway, but they are interesting n
- [Advanced styling effects](/en-US/docs/Learn_web_development/Core/Styling_basics/Advanced_styling_effects)
- : This article will give you guidance on how to go about debugging a CSS problem, and show you how the DevTools included in all modern browsers can help you to find out what is going on.
- [Cascade layers](/en-US/docs/Learn_web_development/Core/Styling_basics/Cascade_layers)
- : This lesson aims to introduce you to [cascade layers](/en-US/docs/Web/CSS/@layer), a more advanced feature that builds on the fundamental concepts of the [CSS cascade](/en-US/docs/Web/CSS/Cascade) and [CSS specificity](/en-US/docs/Web/CSS/Specificity).
- : This lesson aims to introduce you to [cascade layers](/en-US/docs/Web/CSS/@layer), a more advanced feature that builds on the fundamental concepts of the [CSS cascade](/en-US/docs/Web/CSS/Cascade) and [CSS specificity](/en-US/docs/Web/CSS/CSS_cascade/Specificity).
- [Handling different text directions](/en-US/docs/Learn_web_development/Core/Styling_basics/Handling_different_text_directions)
- : In recent years, CSS has evolved in order to better support different directionality of content, including right-to-left but also top-to-bottom content (such as Japanese) — these different directionalities are called writing modes. As you progress in your study and begin to work with layout, an understanding of writing modes will be very helpful to you, therefore we will introduce them in this article.
- [Organizing CSS](/en-US/docs/Learn_web_development/Core/Styling_basics/Organizing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ div {
<div id="orange" class="green" style="color: red;">This is red</div>
```

The rules are more complicated when the selector has multiple parts. A more detailed explanation about how selector specificity is calculated can be found in the [CSS specificity documentation](/en-US/docs/Web/CSS/Specificity).
The rules are more complicated when the selector has multiple parts. A more detailed explanation about how selector specificity is calculated can be found in the [CSS specificity documentation](/en-US/docs/Web/CSS/CSS_cascade/Specificity).

## What do the -moz-\*, -ms-\*, -webkit-\*, -o-\* and -khtml-\* properties do?

Expand Down
2 changes: 1 addition & 1 deletion files/en-us/mdn/tutorials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ These tutorials are created by forward-thinking companies and web developers who
- : CSS (Cascading Style Sheets) is used to style and lay out web pages — for example, to alter the font, color, size, and spacing of your content, split it into multiple columns, or add animations and other decorative features. This module provides a gentle beginning to your path toward CSS mastery with the basics of how it works, what the syntax looks like, and how you can start using it to add styling to HTML.
- [Selectors](/en-US/docs/Learn_web_development/Core/Styling_basics/Basic_selectors)
- : Target HTML elements, including based on element state, with CSS.
- [Specificity](/en-US/docs/Web/CSS/Specificity)
- [Specificity](/en-US/docs/Web/CSS/CSS_cascade/Specificity)
- : Understanding the browser algorithm to determine which CSS declarations get applied to an element when there are competing declarations, with a [specificity quiz](https://estelle.github.io/CSS/selectors/exercises/specificity.html).
- [Handling conflicts](/en-US/docs/Learn_web_development/Core/Styling_basics/Handling_conflicts)
- : The cascade, specificity, and inheritance control how CSS is applied to HTML and how conflicts between style declarations are resolved.
Expand Down
2 changes: 1 addition & 1 deletion files/en-us/web/css/@media/prefers-reduced-motion/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ For Firefox, the `reduce` request is honoured if:

## Examples

This example uses a scaling animation for the purpose of demonstrating `prefers-reduced-motion`. If you enable the setting for reducing motion in the accessibility preferences on your device, the `prefers-reduced-motion` media query will detect your preference and the CSS within the reduced motion rules, with the same [specificity](/en-US/docs/Web/CSS/Specificity) but coming later in the [CSS source order](/en-US/docs/Learn_web_development/Core/Styling_basics/Handling_conflicts#source_order), will take precedence. As a result, the [animation](/en-US/docs/Web/CSS/CSS_animations/Using_CSS_animations) on the box will tone down to the `dissolve` animation, which is a more muted animation that is not a vestibular motion trigger.
This example uses a scaling animation for the purpose of demonstrating `prefers-reduced-motion`. If you enable the setting for reducing motion in the accessibility preferences on your device, the `prefers-reduced-motion` media query will detect your preference and the CSS within the reduced motion rules, with the same [specificity](/en-US/docs/Web/CSS/CSS_cascade/Specificity) but coming later in the [CSS source order](/en-US/docs/Learn_web_development/Core/Styling_basics/Handling_conflicts#source_order), will take precedence. As a result, the [animation](/en-US/docs/Web/CSS/CSS_animations/Using_CSS_animations) on the box will tone down to the `dissolve` animation, which is a more muted animation that is not a vestibular motion trigger.

### Toning down the animation scaling

Expand Down
2 changes: 1 addition & 1 deletion files/en-us/web/css/@scope/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ To fix this, you can use `@scope` as follows:
Now the innermost paragraph is correctly colored black. This is because it is only one DOM tree hierarchy level away from the `.light-theme` scope root, but two levels away from the `.dark-theme` scope root. Therefore, the light style wins.

> [!NOTE]
> Scoping proximity overrules source order but is itself overridden by other, higher-priority criteria such as [importance](/en-US/docs/Web/CSS/important), [layers](/en-US/docs/Learn_web_development/Core/Styling_basics/Cascade_layers), and [specificity](/en-US/docs/Web/CSS/Specificity).
> Scoping proximity overrules source order but is itself overridden by other, higher-priority criteria such as [importance](/en-US/docs/Web/CSS/important), [layers](/en-US/docs/Learn_web_development/Core/Styling_basics/Cascade_layers), and [specificity](/en-US/docs/Web/CSS/CSS_cascade/Specificity).

## Formal syntax

Expand Down
Loading

0 comments on commit e272cdb

Please sign in to comment.