Curious Minds

Clear answers to science and history questions

CSS Import Another CSS: How @import Works

· 5 min read · Science

Yes, you can import one CSS file into another using the @import rule. It must appear at the very top of the stylesheet (after any @charset rule) and before any other rules except @layer statements. For example: @import url("typography.css");. This tells the browser to load and apply the rules from typography.css as if they were written at that point in the importing file.

How @import Works Under the Hood

When a browser parses a CSS file and encounters an @import rule, it fetches the referenced stylesheet and inserts its rules into the cascade at the location of the @import. The imported file's rules are treated as if they appeared at that spot, which affects the cascade order: later rules (including those after the @import in the importing file) can override earlier ones based on specificity and source order.

The @import rule accepts a URL and optional media query or layer() function. For example:

  • @import url("print.css") print; — applies only for print media.
  • @import url("theme.css") screen and (min-width: 600px); — applies only on screens wider than 600px.
  • @import url("base.css") layer(base); — assigns the imported rules to a cascade layer named base.

Browsers fetch imported stylesheets sequentially: the browser must download the importing CSS, parse it, discover the @import, then download the imported file. This creates a chain of requests that can delay rendering, especially if imports are nested (an imported file itself imports another). Modern browsers may preload or parallelize some requests, but the dependency chain remains.

Syntax Rules and Placement

The @import rule is only valid at the top of a stylesheet, before any style rules. If you place it after a rule like body { margin: 0; }, the browser ignores it. The only rules allowed before @import are @charset (which must be the very first thing) and @layer statements (which declare layer order without importing).

Here is a valid order:

  1. @charset "UTF-8";
  2. @layer reset, base, components; (optional)
  3. @import url("reset.css") layer(reset);
  4. @import url("base.css") layer(base);
  5. Your own style rules.

If you need to import a stylesheet conditionally based on user interaction or JavaScript, @import is not the right tool; use JavaScript to inject a <link> element instead.

Common Problems and How to Fix Them

  • Import ignored: The @import appears after other rules. Move it to the top of the file.
  • Slow loading: Nested imports create a waterfall of requests. Flatten your CSS or use a build tool to combine files.
  • Media query not applying: Check that the media query syntax is correct and that the imported file's rules are not overridden by later rules with higher specificity.
  • Layer order confusion: When using layer(), the order of first declaration determines layer precedence. Declare your layer order explicitly with @layer statements before any imports.

A common misconception is that @import is deprecated or bad practice. It is still part of the CSS specification and works in all modern browsers. However, for performance-critical sites, linking stylesheets directly in HTML with <link> tags is generally preferred because it allows the browser to discover and download all stylesheets in parallel.

Both methods load external CSS, but they differ in discovery and performance:

  • <link> in HTML: The browser's preload scanner finds all <link rel="stylesheet"> tags early and downloads them in parallel. This is the fastest way to load multiple stylesheets.
  • @import in CSS: The browser must download and parse the parent CSS before discovering the import. This serializes requests and can delay rendering. However, @import keeps related CSS together and can be useful for modular organization during development.

For production, many developers use a build step to combine CSS files into one, eliminating the need for either multiple <link> tags or @import. If you must choose at runtime, prefer <link> for performance, and use @import only when you need to keep stylesheets self-contained (e.g., a component library that imports its own dependencies).

Using @import with Cascade Layers

Cascade layers, introduced in modern CSS, let you control the priority of different style sources. The @import rule supports a layer() function to assign imported rules to a named layer. For example:

@layer reset, base, components;
@import url("reset.css") layer(reset);
@import url("base.css") layer(base);
@import url("components.css") layer(components);

This imports each file into its own layer, and the layer order (declared first) determines which rules win when specificity ties. This is a powerful way to organize CSS without worrying about source order in a single file.

Alternatives to @import

If @import does not fit your needs, consider these alternatives:

  • Multiple <link> tags: Load each stylesheet separately in HTML. Best for parallel loading.
  • CSS preprocessors: Tools like Sass or Less have their own @import that combines files at build time, producing a single CSS file. This avoids runtime requests.
  • JavaScript dynamic imports: Use import() or DOM manipulation to load CSS on demand, though this is less common for static styles.
  • CSS bundlers: Build tools like webpack, Vite, or Parcel can inline @import statements into a single file during the build.

Each approach has trade-offs: preprocessors and bundlers require a build step, while multiple <link> tags increase HTML size but improve loading parallelism.

Frequently Asked Questions

Can I use @import inside a media query?

No, @import must be at the top level of the stylesheet, not nested inside a media query or any other rule. However, you can add a media query to the @import rule itself, like @import url("print.css") print;.

Does @import work in all browsers?

Yes, @import is supported in all modern browsers and has been for many years. The layer() function is newer and may not work in older browsers, but the basic @import syntax is universally supported.

Why is my @import not loading?

The most common reason is that the @import rule is not at the top of the stylesheet. It must come before any style rules (except @charset and @layer statements). Also check the URL path and that the file exists.

Can I import a CSS file from another domain?

Yes, @import can load cross-origin stylesheets, but the server must allow it via CORS headers if the imported file is used in certain contexts. For simple styles, cross-origin imports generally work without special headers, but some browsers may restrict them for security reasons.

Is @import bad for performance?

It can be, because it creates a serial request chain. For best performance, use <link> tags in HTML or bundle your CSS at build time. However, for small projects or during development, @import is perfectly acceptable.

What is the difference between @import and @use in Sass?

In Sass, @import is the older directive that globally includes variables and mixins, while @use is the newer module system that loads members into a namespace and avoids global scope pollution. This is specific to Sass, not native CSS.

In this article
  1. How @import Works Under the Hood
  2. Syntax Rules and Placement
  3. Common Problems and How to Fix Them
  4. @import vs. &lt;link&gt;: Which to Use
  5. Using @import with Cascade Layers
  6. Alternatives to @import
  7. Frequently Asked Questions