In a web build where each component imports its own stylesheet, why can CSS rules apply in a different order in production than in development?
answer
- the cascade's last tiebreaker
- injected tags versus extracted files
- one stylesheet per chunk
- lazy chunks insert later
- declared layer order
basics
~20 sProduction builds extract imported CSS into one file per chunk, and a lazy chunk's file is inserted when that chunk loads. At equal specificity the later stylesheet wins, so the winner depends on load and navigation order rather than on development's order.
solid answer
~40 sAn `import './SpecimenCard.css'` is a side-effect import: the bundler collects the stylesheet into the graph. In development, many dev servers inject each stylesheet as a `<style>` element when its module evaluates. A production build instead extracts the CSS into files, usually one per chunk: the entry chunk's file is linked from the HTML, and a lazily loaded chunk's file is added when that chunk is fetched. The cascade breaks ties between equally specific rules by order of appearance, so when two lazy routes both style `.card-title`, whichever stylesheet arrived last wins, and that can depend on which route the visitor opened first. Fix it by not relying on cross-file order: scope class names per component, or declare cascade layers once in the entry stylesheet so priority comes from layer order, not file order.
code
css · 12 lines/* entry stylesheet, always loaded first: fixes priority by layer */
@layer base, components, overrides;
/* SpecimenCard.css, extracted into the gallery chunk */
@layer components {
.card-title { color: var(--ink); }
}
/* ExhibitBanner.css, extracted into another lazy chunk */
@layer overrides {
.card-title { color: var(--accent); }
}go deeper
Recall that at equal specificity the later rule wins, and that a production build splits CSS into separate files that may load in a different order.
Explain injected <style> elements in development against per-chunk extracted files in production, and why a lazy chunk's stylesheet is inserted later.
Diagnose a navigation-dependent style bug from the styles panel, and choose between scoping, cascade layers and a single stylesheet by what each costs.
Set a team rule that component styles never depend on cross-file order, using declared layers or scoping, so code splitting can change freely without visual regressions.
## How a stylesheet becomes part of the build `import './SpecimenCard.css'` imports nothing by name. It is a **side-effect import**: it tells the bundler that this module needs that stylesheet. The bundler adds the stylesheet to the module graph next to the JavaScript that imported it, processes its `url()` references and `@import` rules, and then has to decide how the CSS reaches the page. One build-side trap to rule out first: a stylesheet imported only for its side effect can be dropped entirely when the package is marked as free of side effects. That marking is a separate subject; here the CSS does arrive, but in a different order. ## Development: injected as modules run In development, many dev servers turn each stylesheet into a small JavaScript module that inserts a `<style>` element when it evaluates, which also makes swapping the CSS on edit easy. The order of those elements follows the order in which modules **evaluate**, which usually mirrors import order and, for lazy routes, the order in which you visit them. ## Production: extracted per chunk A production build typically **extracts** CSS into real `.css` files, because a linked stylesheet is cached separately and can download in parallel with the script. With CSS split by chunk: - CSS reached from the entry chunk goes into a stylesheet linked from the HTML; - CSS reached only from a **lazily loaded chunk** goes into that chunk's own stylesheet, which is added to the document when the chunk is requested; bundlers usually hold the chunk's code until its CSS has loaded, so the content does not flash unstyled; - CSS imported by modules shared between chunks may land in a shared chunk's stylesheet, inserted whenever that chunk first loads. Inside one extracted file, rules follow the order in which the bundler reached the modules. Across files, order is the order of the `<link>` and `<style>` elements in the document. Grouping changes the sequence too: a stylesheet that development inserted after another route's may, in production, sit inside a shared or entry file that was inserted earlier. | | Development | Production | |---|---|---| | Delivery | `<style>` elements inserted by modules | Extracted `.css` files, often one per chunk | | Order within a chunk | Module evaluation order | Order the build reached the modules | | Order across chunks | Evaluation and visit order | Order the chunks' stylesheets were inserted | | Shared component CSS | Inserted once, where first evaluated | May sit in a shared chunk's file | ## Why the winner changes Among the cascade's criteria, origin and importance come first, then cascade layers, then specificity, and finally **order of appearance**: the later declaration wins. Component stylesheets usually have equal specificity, single class selectors, so order of appearance decides. Consider the museum viewer: 1. The gallery route's stylesheet sets `.card-title { color: var(--ink); }`. 2. The exhibit-banner route's stylesheet sets `.card-title { color: var(--accent); }`. 3. A visitor who opens the gallery first and the banner second gets the accent colour; a visitor who arrives the other way round gets ink on the same screen. Nothing in either file changed; only the insertion order did. Development seemed fine because the developer always clicked through the same path, or because modules evaluated in a different sequence. ## Diagnosing an order bug 1. In the browser's styles panel, find the losing and winning rules and note which stylesheet file each came from. 2. Compare their specificity; if it is equal, the order of the two files in the document decides. 3. Reproduce with a different navigation path or a hard reload on the second route, to confirm the result depends on load order. ## Fixes, and what each costs - **Stop sharing selectors across files.** Locally scoped class names per component make the collision impossible; how scoping works is its own subject. - **Declare cascade layers once, first.** An `@layer base, components, overrides;` statement in the entry stylesheet fixes priority by layer order, wherever each file loads. Remember that unlayered rules beat layered ones for normal declarations. - **Keep global and base CSS in the entry**, so it is always inserted before any route's stylesheet. - **Extract everything into one stylesheet.** Order becomes fixed by the build, at the cost of downloading every route's CSS up front and losing per-chunk caching. - **Avoid `!important` as a patch**: it only moves the fight to a higher tier, and the next equal-weight collision behaves the same way.
- Why do cascade layers fix the problem when the files still load in an unpredictable order?Layer priority comes from the order layers were first declared, not from where each rule's file sits in the document. Declaring `@layer base, components, overrides;` in the entry stylesheet fixes that order before any chunk loads, so a rule in `overrides` beats one in `components` whichever file arrives last. Order of appearance only breaks ties inside the same layer.
- Why not just put all CSS into one extracted file?It does fix the order, but every visitor then downloads every route's CSS before the first paint, and a change to any component invalidates the cached file for the whole site. It suits small apps; larger ones usually keep per-chunk CSS and remove the order dependency instead.
- Stylesheets apply in the order the components render on the page.
- Production CSS order always matches development because both follow import order.
- Adding `!important` is the right fix for a cross-chunk order bug.
- Merging all CSS into one file has no downside.
- With cascade layers, the layer whose file loads last wins.