Sub topic 9
Containing third-party and framework CSS
Third-party CSS is hard to override because it was written for someone else's page. Its selectors are often specific, and some use !important. This chapter shows how to put that CSS in a layer so your own rules win without specificity fights.
This chapter assumes you know selector specificity and source order, and that you have used @layer blocks, the layer order statement, and @import ... layer().
The problem with vendor CSS
Suppose a framework ships this rule:
.framework .panel .btn.primary {
background: steelblue;
}Its specificity is four classes. To override it with a plain .btn rule you would normally have to raise your own specificity, add !important, or rely on load order. Each of these makes your stylesheet harder to maintain.
Layers remove the need for any of that. Layer order is compared before specificity, so a rule in a later layer wins even if its selector is weaker.
Put the vendor CSS in the lowest layer
The approach has two steps:
- Declare the layer order first, with vendor CSS earliest.
- Load the vendor stylesheet into the vendor layer.
@layer vendor, site;
@import url("vendor.css") layer(vendor);
@layer site {
.btn { padding: 0.5rem 1rem; }
}The @layer vendor, site; statement fixes the priority: site is declared after vendor, so it wins. Every rule inside vendor.css now belongs to the vendor layer, whatever its selectors look like. A .btn rule in site beats the vendor's four-class selector for any property both set.
The @layer statement may come before @import. Other rules cannot, so keep the order statement at the very top.
How the pieces interact
This diagram applies to normal declarations. Unlayered styles beat every layer, which is useful if you have a few one-off page rules that must always win.
Several vendors, one order
When a project uses more than one library, give each its own layer. You then control which library wins when two of them conflict.
@layer reset, framework, plugins, site;
@import url("framework.css") layer(framework);
@import url("datepicker.css") layer(plugins);
@layer site {
.btn { border-radius: 4px; }
}Here plugins outranks framework, and site outranks both. Changing the first line is enough to change priorities. You do not need to reorder files.
The `!important` gotcha
Layering does not tame vendor !important declarations. For !important, layer order is reversed, so an earlier layer wins. A vendor !important in the lowest layer therefore beats !important in your later layers.
@layer vendor, site;
@layer vendor {
.modal { display: none !important; }
}
@layer site {
.modal { display: block !important; }
}The modal stays hidden, because the vendor layer comes first and wins for important declarations. To beat vendor !important, you would need your own !important rule in a layer declared before vendor. Do not add one casually. Check whether the vendor rule is meant to be a hard override first.
Limits
- Only the order of layers matters between layers. Specificity still decides conflicts inside the same layer.
- Browsers that do not support
@layerignore the layered rules, so check your support targets. - A vendor file that already declares its own layers keeps them. They are nested under the layer you import it into.
Exercise
You import bootstrap.css with @import url("bootstrap.css") layer(vendor); and declare @layer vendor, site; first. Bootstrap sets .btn { color: white; } and your unlayered stylesheet sets button { color: black; }. Which color does a <button class="btn"> get, and why?
Show answer
Black. Unlayered normal declarations beat layered ones, so the unlayered button rule wins even though .btn has higher specificity and vendor is also layered.
Exercise
A vendor stylesheet in the vendor layer contains .alert { margin: 0 !important; }. Your later site layer sets .alert { margin: 1rem !important; }. Which margin applies?
Show answer
0. For !important declarations the layer order is reversed, so the earlier vendor layer wins over the later site layer.