CSS: @import

By Xah Lee. Date: .
xtodo

Import

ai answer

CSS @import Tutorial

The @import rule in CSS allows you to import rules from another stylesheet into the current stylesheet. It’s a way to modularize your CSS by splitting it across multiple files.

1. Basic Syntax

@import url("styles.css");
@import "styles.css";           /* url() is optional */

Example – main.css

@import url("reset.css");
@import url("typography.css");
@import url("components.css");

body {
  background: #f8f9fa;
}

2. Importing from External URLs

You can import stylesheets from other domains (CORS must allow it):

@import url("https://example.com/theme.css");

3. Media Queries with @import

You can conditionally import stylesheets based on media features:

/* Import only for screens ≤ 600px */
@import url("mobile.css") screen and (max-width: 600px);

/* Print styles */
@import url("print.css") print;

/* Multiple conditions */
@import url("high-res.css") screen and (min-resolution: 2dppx);

4. Layered Imports (@layer) – Modern CSS

Since 2022, you can assign imported styles to cascade layers for better control over specificity:

@import url("framework.css") layer(framework);
@import url("theme.css") layer(theme);

@layer theme {
  /* your overrides here */
}

This is highly recommended for large projects.

5. Full Example

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>CSS Import Demo</title>
  <link rel="stylesheet" href="main.css">
</head>
<body>
  <h1>Hello World</h1>
</body>
</html>

main.css

@import url("reset.css") layer(reset);
@import url("variables.css") layer(variables);
@import url("components/buttons.css") layer(components);

:root {
  --primary: #0066ff;
}

6. Performance Considerations

Important drawbacks of @import:

Recommendation:

<link rel="stylesheet" href="reset.css">
<link rel="stylesheet" href="main.css">

7. Best Practices

Practice Recommendation
File organization Keep imports at top
Limit depth Avoid @import inside imported files
Use layers Always prefer @import ... layer(name)
Production Use a build tool (Vite, webpack, Parcel, etc.) to bundle everything
Caching Use versioned filenames or cache headers
Order matters Imports are applied in the order they appear

8. Common Pitfalls

  1. Placing @import after rules — ignored by the browser.
  2. Too many imports → slow page loads.
  3. Circular imports (A imports B, B imports A) → unpredictable behavior.
  4. Forgetting url() when needed (though quotes alone work in most cases).
  5. Specificity surprises when mixing imported and local rules.

9. Modern Alternatives (2025+)

Quick Reference Cheat Sheet

/* Basic */
@import "file.css";

/* With media */
@import "mobile.css" (max-width: 600px);

/* With layer */
@import "lib.css" layer(third-party);

/* Multiple in one line (not recommended) */
@import url(a.css), url(b.css);

Pro Tip: In development, @import is convenient. In production, let your bundler handle it.

Would you like a section on how to use @import with Sass/SCSS, Tailwind, or a complete project structure example?