The @import Element – When Is It Used In CSS?
nanadwumor
-
Function: The @import rule loads one stylesheet inside another.
-
Placement: It must always appear before any normal CSS rules.
-
Behavior: All imported stylesheets load actively and cannot be optional.
-
Modern Usage: Build tools and <link> tags often replace it today.
RECOMMENDED ARTICLES
The Style Element in CSS
Learn how to quickly add CSS directly to your HTML using the [crayon-6acac07a7dbc4272244853-i/] element without any external files required! Definition: The [crayon-6acac07a7dbef615259902-i/] element adds embedded CSS directly inside an HTML...
Alternate Stylesheets in CSS
Discover how native HTML and CSS allow users to switch website themes effortlessly without JavaScript! Persistent Stylesheets: Have no [crayon-6acac07a7e8ca214016154-i/] and are always active. Preferred Stylesheets: Have a...
The Link Element
Without the [crayon-6acac07a7f84b040827281-i/] element connecting your HTML structure to your CSS design, your webpage is just a skeleton waiting for its style. External Stylesheets: Separate CSS files style multiple pages simultaneously. The...
In the previous module, you learned that the <style> element can contain CSS rules directly inside an HTML document. You have also already learned about the use of the <link> element to connect CSS to HTML.
Now let’s look at a special CSS command called @import, which allows one stylesheet to load another stylesheet.
This provides a way to organize CSS into smaller, reusable files.
What Is the @import Rule?
The @import rule tells the browser:
“Before processing this stylesheet, load another stylesheet first.”
Example:
|
1 |
@import url("styles.css"); |
When the browser sees this rule, it first loads styles.css and then continues processing the current stylesheet.
Simple Analogy
Imagine you’re reading a textbook.
Before Chapter 5 begins, the author writes:
“Read Chapter 2 first.”
You stop, read Chapter 2, then return to Chapter 5.
The @import rule works in the same way.
It tells the browser:
“Load this stylesheet first, then continue.”
Basic Syntax
|
1 |
@import url("styles.css"); |
or
|
1 |
@import url(styles.css); |
Both forms are valid.
Using @import Inside a Style Element
Example:
The browser performs the following steps:
Step 1
Load styles.css
Step 2
Read the remaining CSS rules
Step 3
Apply all styles to the webpage
Why Use @import?
The main purpose is to divide CSS into smaller files.
Instead of creating one huge stylesheet:
- styles.css
you can create:
- layout.css
- typography.css
- colors.css
- forms.css
and combine them.
Example
|
1 2 3 |
@import url("layout.css"); @import url("typography.css"); @import url("colors.css"); |
Now all three stylesheets become part of the current stylesheet.
Important Rule: @import Must Come First
This is the most important rule beginners must remember.
The @import statement must appear before all normal CSS rules.
Correct Example
|
1 2 3 4 5 |
Correct Example @import url("styles.css"); body { color: black; } |
This works.
Incorrect Example
|
1 2 3 4 |
body { color: black; } @import url("styles.css"); |
This does not follow CSS rules.
Modern browsers ignore the improperly placed import.
Why Must It Come First?
When the browser begins reading a stylesheet, it needs to know immediately which additional stylesheets must be loaded.
If imports appear later, the browser has already started processing styles.
Therefore CSS requires imports to appear before other rules.
Memory Aid
Think of boarding a plane.
Passengers must board before takeoff.
You cannot suddenly decide to board more passengers after the aircraft is already in the air.
Similarly:
@import
must come before normal CSS rules.
Using Multiple Imports
The type attribute tells the browser what type of content is inside the style element.
Example:
|
1 2 3 |
@import url("layout.css"); @import url("fonts.css"); @import url("colors.css"); |
The browser loads all three stylesheets.
What Happens Next?
The browser combines all imported styles with the current stylesheet.
Example:
|
1 2 3 4 |
@import url("layout.css"); body { background-color: white; } |
The final stylesheet consists of:
Rules from layout.css
Rules from the current stylesheet
Unlike Alternate Stylesheets…
Recall that with the <link> element, you can create:
Preferred stylesheets
Alternate stylesheets
The @import rule cannot do this.
Every imported stylesheet is loaded and used.
Example
|
1 2 3 |
@import url("layout.css"); @import url("colors.css"); @import url("fonts.css"); |
Result:
layout.css -> loaded
colors.css -> loaded
fonts.css -> loaded
All three are active.
None are optional.
Restricting Imports to Specific Media
Like the <link> element, @import can target specific media types.
Import for All Devices
|
1 |
@import url("styles.css") all; |
This applies everywhere.
Import for Screens Only
|
1 |
@import url("screen.css") screen; |
This stylesheet affects:
- Monitors
- Phones
- Tablets
Import for Printing
|
1 |
@import url("print.css") print; |
This stylesheet affects printed pages only.
Multiple Media Types
Example:
|
1 |
@import url("presentation.css") screen, projection; |
The stylesheet applies to:
- Screens
- Projectors
Importing Stylesheets into Other Stylesheets
One powerful feature of @import is that it works inside CSS files.
Suppose we have:
- main.css
- main.css
|
1 2 3 4 5 6 |
@import url("layout.css"); @import url("colors.css"); @import url("typography.css"); body { margin: 0; } |
The browser loads all imported stylesheets automatically.
Why This Is Useful
Large websites often separate CSS by purpose.
Example:
- layout.css
- Page structure.
- typography.css
- Fonts and text.
- colors.css
- Colors and themes.
- forms.css
- Form styling.
This organization makes projects easier to maintain.
Relative URLs vs Absolute URLs
The imported stylesheet can be referenced using either type.
Relative URL
|
1 |
@import url("layout.css"); |
Used for local project files.
Absolute URL
|
1 |
@import url("https://example.com/layout.css"); |
Used when loading from another location.
Example Project Structure
project/
│
├── index.html
│
├── css/
│ ├── main.css
│ ├── layout.css
│ ├── colors.css
│ └── typography.css
main.css
|
1 2 3 |
@import url("layout.css"); @import url("colors.css"); @import url("typography.css"); |
This creates one combined stylesheet.
Browser Processing Example
Suppose the browser reads:
|
1 2 3 4 5 |
@import url("layout.css"); @import url("colors.css"); body { background-color: white; } |
The browser:
Step 1
Loads layout.css
Step 2
Loads colors.css
Step 3
Reads the remaining rules
Step 4
Combines everything
Step 5
Applies styles to the page
Note
Historically, @import was very popular because it allowed developers to split large stylesheets into smaller modules.
Modern developers still use it occasionally, but many prefer:
|
1 |
<link rel="stylesheet"> |
or build tools such as:
- Webpack
- Vite
- Parcel
for managing CSS dependencies.
Common Mistakes Beginners Make
- Putting @import After CSS Rules
Wrong:
|
1 2 3 4 |
body { color: black; } @import url("styles.css"); |
The import may be ignored.
- Forgetting the Semicolon
Wrong:
|
1 2 3 |
@import url("styles.css") Correct: @import url("styles.css"); |
- Assuming Imported Styles Are Optional
Every imported stylesheet is loaded.
There is no alternate stylesheet mechanism with @import.
- Mixing Up File Paths
Wrong:
|
1 |
@import url("style.css"); |
|
1 |
styles.css |
Even a small spelling mistake causes failure
- Mixing Up File Paths
Wrong:
|
1 |
@import url("style.css"); |
when the file is:
styles.css
Even a small spelling mistake causes failure.
- Importing Too Many Files
Large numbers of imports can slow page loading because each stylesheet must be requested separately.
Memory Aid
Think of @import as inviting guests to a meeting.
Before the meeting starts, everyone who was invited must arrive.
That’s why imports must come first.
Quick Summary
@import loads one stylesheet into another stylesheet.
Syntax:
|
1 |
@import url("styles.css"); |
Imports must appear before normal CSS rules.
Multiple imports are allowed.
All imported stylesheets are loaded and applied.
Media types can be specified.
Both relative and absolute URLs are supported.
@import is useful for organizing large CSS projects into smaller files.



Recent Comments