Curious Minds

Clear answers to science and history questions

CSS Import Another CSS File: How @import Works

· 7 min read · Science

Yes, one CSS file can import another. The standard mechanism is the @import at-rule, placed at the top of a stylesheet, which tells the browser to fetch another stylesheet and apply its rules in that position. Modern projects more often use a build step or a bundler to stitch CSS files together, but @import remains valid CSS that browsers understand natively. You can also import a stylesheet from HTML with a second <link> element, which is often the better choice for performance.

What @import Does and Where It Comes From

@import is an old CSS feature, part of the language since the early days of style sheets. Its purpose is modularity: instead of maintaining one enormous stylesheet, you split rules into logical files, such as a reset, typography, layout and components, and pull them together from one entry file.

The syntax is simple and must appear before any style rules other than @charset and a leading @layer statement:

@import url("reset.css");
@import "typography.css";
@import url("layout.css") screen and (min-width: 40em);

Two important details are easy to miss. First, the quotation marks and the url() wrapper are interchangeable; both forms are valid. Second, the position matters. If a rule such as body { margin: 0; } appears above the import, browsers are required to ignore the import entirely. This is one of the most common reasons an import silently fails.

How the Cascade Handles Imported Rules

An imported stylesheet behaves as though its contents were written at the point of the @import line. That means order determines precedence when two rules have the same specificity. If main.css imports base.css first and then defines its own h1 colour, the local rule wins because it comes later in the effective document order.

Browser behaviour follows a few consistent rules:

  • Imports are resolved recursively, so an imported file can import further files. Deep chains work but make the dependency graph hard to reason about.
  • A file imported twice is generally only applied once, because browsers track which stylesheets have already loaded, though the safe assumption is to avoid duplicate imports.
  • Imports inside a <style> block in the HTML document also work, with the same ordering restriction.
  • Specificity is not increased by importing. An imported selector is exactly as strong as it would be if you had pasted it into the parent file.

A useful mental model: imagine the browser flattening all your @import statements into one long stylesheet before it evaluates the cascade.

The Media Query and Layer Syntax

@import accepts conditions, which makes it more capable than a plain <link> in one narrow way: you can apply a whole file only under certain circumstances.

@import "print.css" print;
@import "wide.css" (min-width: 60em);
@import "dark.css" (prefers-color-scheme: dark);
@import "utilities.css" layer(utilities);

The layer() form is newer and places the imported rules into a named cascade layer. Cascade layers are a powerful tool for controlling precedence without inflating specificity, and importing into a layer is a clean way to say "this file's rules lose to my own unless nothing else matches." Support for layered imports is good in current browser versions but not universal in very old ones, so treat it as a progressive enhancement.

Why @import Can Slow Down Loading

This is the main technical objection to @import. When the browser parses the main stylesheet and encounters the import, it must fetch the imported file, and only then can it discover any imports inside that file. The result is a chain of sequential requests rather than parallel ones. Each extra level in the chain adds a round trip before rendering can complete, which is why performance guidance has long recommended <link> tags or a single combined file instead.

The practical consequences:

  • Each nesting level adds latency, which is noticeable on slow connections.
  • Browsers can sometimes discover and preload imports early, but you should not rely on it for the critical path.
  • Imports that fail are easy to overlook, because the page still renders, just unstyled in that section.
  • Relative paths in an import resolve against the location of the importing stylesheet, not the HTML page. A file used from two different directories therefore needs careful path management.

There are three common ways to combine stylesheets, and they suit different situations.

  • Multiple <link> elements in HTML. The browser fetches them in parallel, order is explicit in the document, and no extra tooling is required. Best for small sites and for separating component files you want cached independently.
  • @import in CSS. Keeps all style dependencies expressed in CSS, allows media-conditional imports and cascade layers, and lets a single <link> pull in a tree of files. The cost is serial loading.
  • A preprocessor or bundler. Sass, Less and PostCSS, or a general bundler, can concatenate files at build time. This produces one fast request, supports variables and mixins, and keeps source files modular. The trade-off is an extra build step and a less direct relationship between the files you edit and what ships.

A useful rule of thumb: use plain CSS imports during development when you want zero tooling, and compile to a single file or a small number of files for production. If you adopt Sass, note that its own @import has been deprecated in favour of @use and @forward, which have clearer scoping rules. That deprecation applies to the preprocessor, not to the CSS at-rule, which remains part of the spec.

Common Problems and How to Fix Them

Most import failures fall into a small set of causes:

  1. Rules before the import. Move every @import to the top of the file, before any selector blocks.
  2. Wrong path. The path is relative to the importing stylesheet. Check the directory, the file extension and letter case, since many servers are case-sensitive.
  3. Server not serving the file. A typo in the URL, a permissions issue or a misconfigured content type can return an error while the page still loads. The browser console and network panel show the failed request.
  4. Styles from the import being overridden. Because imported rules land at the position of the import, later rules win. If an imported rule is losing unexpectedly, either move the import later or raise specificity deliberately.
  5. Duplicate or circular imports. A file that imports itself, directly or through two others, creates confusion about final order. Keep the structure a tree, not a web.

One widespread misconception is that @import is deprecated or removed from CSS. It is not. Its role in production has diminished because of performance concerns and build tooling, but browsers continue to support it and it remains in the specification.

Do You Actually Need It?

If you are building a small site with a handful of stylesheets, multiple <link> tags are usually simpler and faster. If you need media-specific files, cascade layers, or a single entry point that lives entirely in CSS, @import earns its place. If your project already has a build pipeline, let the bundler combine the files and avoid runtime imports altogether. The right answer depends on whether the modularity is for organisation or for conditional loading.

Frequently Asked Questions

Can you import a CSS file inside another CSS file?

Yes. Use @import "filename.css"; at the very top of the stylesheet, before any other rules except @charset and a leading @layer. The imported rules take effect at that position in the cascade.

Does @import slow down page loading?

It can. Imports can create sequential fetches, because the browser has to read one file before discovering the next, so a deep chain of imports delays styling. For critical styles, multiple <link> tags or a single bundled file generally load faster.

Is @import still supported in modern browsers?

Yes. The CSS @import rule is still part of the specification and supported across browsers. Only the Sass version of @import has been deprecated in favour of @use and @forward.

Why is my @import not working?

The most common cause is placing the rule after other CSS rules, which makes browsers ignore it. Other frequent causes are an incorrect relative path, a typo in the file name, or the file not being served by the server. Check the browser's network panel for a failed request.

What is the difference between @import and a link tag?

A <link> in HTML tells the browser about a stylesheet directly from the document, so several can download in parallel. @import is discovered only after the importing stylesheet is fetched. Both apply styles in document order, but <link> is usually faster.

Can an imported file import another file?

Yes. Imports can nest, and the browser resolves them recursively. Keeping the nesting shallow, or flattening it with a build tool, avoids long request chains and confusing rule order.

In this article
  1. What @import Does and Where It Comes From
  2. How the Cascade Handles Imported Rules
  3. The Media Query and Layer Syntax
  4. Why @import Can Slow Down Loading
  5. Imports Versus Build Tools and Multiple Link Tags
  6. Common Problems and How to Fix Them
  7. Do You Actually Need It?
  8. Frequently Asked Questions