· 2 min read
Global styles with cascade layers
Created src/lib/styles/global.css with @layer reset, base, components;.
What we did
- Created
src/lib/styles/global.csswith@layer reset, base, components;. reset: box-sizing, margins, media elements, form fonts,overflow-wrap.base:bodyfont and colors, heading sizes and link color, all from tokens.- Moved the
:global(body)and:global(h1)rules out of the layout.
What I learned
- Layers are checked before specificity. In the order list, the later layer wins, however specific the earlier rule is.
- Unlayered styles beat all layered ones. Svelte component
<style>blocks aren't in a layer, so they override the globals. resetgoes first so mybasedefaults aren't wiped out by it.- A layer's position is set by where its name first appears, so the order statement goes at the top. The minifier rewrote it, and the order still held.
var()is looked up when styles are computed for an element, after all stylesheets are loaded. So the order of thetokens.cssandglobal.cssimports doesn't matter.- Source order only decides between rules that set the same property on the same element, with the same specificity, in the same layer.
- Logical properties:
max-inline-sizeandmin-block-sizeinstead ofmax-widthandmin-height.
Links
Next
- Dark mode.