

In that case, the !important styles invert their specificity. In a layer, declarations with the !important rule are more specific than unlayered styles. While layered styles are less specific than unlayered styles in general, using !important reverses this.
# Rule 3: !important inverts cascade specificity Using a reset.css file, for example, is a good starting point and use case for cascade layers. This was an intentional decision to make it easier to introduce layers in a more sensible way to work with your existing codebase. It’s important to note that a layered CSS file will not override non-layered CSS. The CSS then adds three layers: base, typography, and utilities as follows: base # Rule 2: cascade layers are ordered behind non-layered CSS This example showcases the power of cascade layers, using There are several links shown: some without any additional class names applied, one with a. Layered styles create new cascade "planes." # in action See demo on Codepen. If trying to style a link, inside a card, within a post you will find that the more specific selector will be applied.īy using you can be more explicit about the style-specificity of each, and make sure that your card link’s styles override the post link’s styles, even though the specificity might numerically be lower if all your CSS was on the same plane. With layered styles, the precedence of a layer always beats the specificity of a selector.įor example, the selector. They introduce a new layer to the CSS cascade. An illustrated example of BEM naming from .Ĭascade layers aim to solve this problem.
CASCADEA FOR CHROME CODE
However, it’s not always possible to maintain such organized styles, especially when working with third-party code and design systems. By giving everything a single classname, everything is placed on the same specificity plane. This is an elemental part of learning CSS.įolks turn to CSS naming conventions like BEM to prevent overriding specificity unintentionally. For example, elements are less specific than classes or attributes, which are in turn less specific than IDs. The different selectors you can use determine the specificity of any style rule. # CSS specificity and the cascadeĬSS specificity is how CSS decides which styles to apply to which elements.

Layering your CSS in a clear way prevents unexpected style overrides and promotes better CSS architecture. This is particularly useful for large codebases, design systems, and when managing third party styles in applications. They enable more explicit control of your CSS files to prevent style-specificity conflicts. Cascade layers (the CSS rule) are coming to Chromium 99, Firefox 97, and Safari 15.4 Beta.
