layer
An at-rule that decides which styles win by the order of layers rather than by specificity. Replaces the !important on utility classes and the extra-specific selectors used to override library CSS.
Modern approach
Declare the order of the layers up front with a single @layer statement. A later layer wins regardless of specificity, so !important is no longer needed.
@layer components, utilities;
@layer components {
.card .card__title {
margin-top: 1em;
}
}
@layer utilities {
.u-mt-0 {
margin-top: 0;
}
}Legacy approach
Component selectors tend to have high specificity, so utility classes applied on top of them lose. To make sure they won, utility classes were given !important.
.card .card__title {
margin-top: 1em;
}
.u-mt-0 {
margin-top: 0 !important;
}Things to watch when replacing
- Styles in a layer lose to styles outside any layer, even when their specificity is higher. If you move only part of your CSS into layers, its priority relative to the CSS you haven’t moved can flip.
- For declarations with
!important, the layer order is reversed and the layer declared first wins. If you keep!importantwhile moving rules into layers, the opposite declaration from the one you intended can win. - Layer order is set by the order in which layers first appear, and can’t be changed later. Even when each file writes its own layers, declare the order at the top of the CSS that loads first.
Putting library CSS in a layer
Library CSS can also be put in a layer with layer() on @import. If you write @import inside a CSS file, the library doesn’t start loading until that file has finished loading, so write it in a <style> element in the HTML.
<style>
@import url("https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css") layer(vendor);
</style>
<link rel="stylesheet" href="style.css" />
Declare vendor first at the top of style.css, and the library CSS becomes the weakest layer. Whether you put your own CSS in components or utilities or leave it outside any layer, it beats the library CSS regardless of specificity, so you no longer need to add parent classes to raise specificity.
/* style.css */
@layer vendor, components, utilities;
- Once the library is in a layer, declarations it marks
!important(such as Bootstrap’s.mt-0and other utility classes) can no longer be overridden by your own CSS, even with!important. @importhas nointegrityattribute, so you lose the file check thatintegrity(SRI) on<link>provides. To keep that check, host the library CSS on your own server, or bundle it with your own CSS using a build tool.
Fallback
Browsers that don’t know @layer ignore @layer blocks along with everything inside them. layer() on @import is read as a media query that doesn’t match, so the library CSS isn’t applied. If you also have to support older browsers, use the PostCSS plugin @csstools/postcss-cascade-layers to convert layers into specificity adjustments. When you use layer() on @import, combine the CSS into one file with postcss-import first.
Browser Support
Chrome
v99+2022-03-01Edge
v99+2022-03-03Safari
v15.4+2022-03-14Safari (iOS)
v15.4+2022-03-14Firefox
v97+2022-02-08