@import
Includes the contents of another CSS stylesheet in the current one. It can place it straight into a layer or load it only for chosen media.
Example
#@import url("reset.css") layer(reset);
@import url("theme.css") layer(theme);
@import url("print.css") print;
.button {
background: #7c3aed;
}The imports sit at the very top of the file. The styles from reset.css and theme.css go into layers, so ordinary rules such as .button always win over them. The print.css file applies only when printing. The example has no preview because the frame does not contain these files.
Definition and usage
#The @import rule loads another CSS file and treats its rules as if they stood at the place of the import. You give the address as url("file.css") or as plain quoted text. Relative paths are resolved from the location of the stylesheet containing the import, not from the HTML page.
An import must be at the very top of the stylesheet, before all other rules. Only @charset and layer order declarations in the form @layer a, b; may precede it. An import placed lower is silently ignored by the browser.
After the address you can add conditions. layer(name) puts the whole file into a cascade layer, which works great for resets and libraries: their styles lose to yours even if they have higher specificity. A media query, e.g. print or (width >= 900px), makes the rules from the file apply only when the condition is met.
Watch out for performance. The browser learns about an import only after downloading and reading the stylesheet that contains it, so files load one after another instead of in parallel. In production it is better to include stylesheets with separate <link> tags in HTML or to merge them into one file with a build tool, e.g. Vite, which resolves imports at build time.
Syntax
#@import url("file.css");
@import "file.css";
@import url("file.css") layer(<name>);
@import url("file.css") <media query>;More examples
#@layer reset, vendor, app;
@import url("reset.css") layer(reset);
@import url("vendor/slider.css") layer(vendor);
@layer app {
.slider {
border-radius: 12px;
}
}A @layer declaration can come before the imports and sets the layer order. A style from the app layer will beat the library even if its selectors were more specific.
<link rel="stylesheet" href="/css/reset.css">
<link rel="stylesheet" href="/css/theme.css">
<link rel="stylesheet" href="/css/print.css" media="print">The browser sees all the stylesheets at once and downloads them in parallel. A stylesheet with media="print" does not block rendering the page on screen.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Put imports at the very start of the file. An import after any ordinary rule will not work, and the browser reports no error.
- Import external libraries with
layer(). You will not need to raise specificity or use!importantto override their styles. - Avoid chains of imports where a file imports a file, which imports another. Every link is a separate wait for the network.
Related entries
#- @layerSplits styles into cascade layers with a fixed order. A rule from a later layer wins regardless of selector specificity.
- @mediaApplies styles only when the device meets a condition: a matching window width, a dark theme, a mouse, or printing the page.
- @font-faceRegisters a typeface under a chosen name that you then use in font-family. It lets you use fonts from your own site’s files.
- SpecificityA selector’s weight, which decides which rule wins when several rules set the same property on the same element.
- @supportsApplies styles only in browsers that support a given property, value, or selector. It lets you use new features safely.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.