Sub topic 10
Loading CSS from CSS with @import
This chapter assumes you can write a basic CSS rule, link an external stylesheet with a link element, and read a relative file path. It covers the @import rule: how it works, where it must go, and why it is usually the slower choice.
What @import does
@import is an at-rule that tells the browser to load another stylesheet and apply its rules as if they were written at that spot. It lets one CSS file pull in others, so a page that links a single file can still use several.
/* main.css */
@import url("reset.css");
body {
margin: 0;
}When a page links main.css, the browser reads it, finds the @import, fetches reset.css, and applies those rules before the rules that follow in main.css.
The path inside url() is resolved relative to the file that contains the @import, not relative to the HTML page. If main.css is in css/, then reset.css is expected in css/ too. The quotes inside url() are optional for simple paths, and you can also write @import "reset.css"; without url().
Placement rule
@import must come before every other rule in the stylesheet. Only @charset and @layer statements may precede it. If you put it after a normal rule, the browser ignores it and gives no visible error.
/* broken.css */
body {
margin: 0;
}
/* Ignored: it appears after a normal rule. */
@import url("reset.css");/* working.css */
@import url("reset.css");
@import url("typography.css");
body {
margin: 0;
}Imported rules come first, so your own rules in main.css can override them when specificity is equal. This follows the source-order behavior from the cascade chapter.
Why it is usually slower
The cost comes from the order of discovery. The browser can only see an @import after it has downloaded and parsed the file that contains it. Imports then form a chain:
With two link elements, the browser finds both files while reading the HTML and requests them in parallel. With @import, the second request waits for the first response. Each level of nesting adds another round trip. Because stylesheets block rendering, that delay is also time the user sees a blank page.
Compare the HTML alternative:
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/main.css">The result is the same cascade order, since reset.css comes first, but both files download at once.
Conditional imports
@import accepts a media query after the URL. The imported file then applies only when the query matches.
@import url("print.css") print;
@import url("wide.css") (min-width: 60rem);This mirrors the media attribute on a link element. Prefer the link version in HTML when you can, for the same discovery-speed reason.
When @import is reasonable
- Build tools. Many tools (Sass, PostCSS, bundlers) resolve
@importat build time and write one combined file. The browser never sees the imports, so there is no runtime cost. - Small internal splits. A stylesheet you cannot easily link from HTML, such as one loaded by a third-party widget, may need to pull in a second file.
- Layers. Modern CSS lets you import a file into a cascade layer with
@import url("base.css") layer(base);. This is a legitimate use thatlinkelements cannot replicate.
Common errors
| Symptom | Likely cause |
|---|---|
| Imported styles never apply | `@import` appears after another rule |
| File not found in the network panel | Path written relative to the HTML page instead of the CSS file |
| Imported rules override yours | Your rules come before the import, or specificity differs |
| Slow first paint | Nested or chained imports delaying rendering |
In developer tools, the Network panel shows imported files with the CSS file as their initiator. A waterfall where reset.css starts only after main.css finishes is the sign of an import chain.
Exercise
A page links css/main.css. Inside main.css you write body { margin: 0; } followed by @import url("reset.css");, and reset.css is in the same folder. The reset has no effect. Give two things you must fix or consider, and explain why a link element would still be faster than the corrected import.
Show answer
First, move the @import to the top of main.css, because an import after a normal rule is ignored. Second, check that the path is relative to main.css, which is in css/, so reset.css must be in css/ as well. Once fixed, the import still makes the browser download main.css first and only then discover reset.css. Two link elements in the HTML let the browser request both files at the same time.