@import – imported CSS

You can use imported CSS.

This is done by placing the @import rule in a CSS file or in an HTML5 file. The loading of another CSS file will then be launched automatically. In large and complex websites, if you put everything in the same CSS file, it will become huge and you won't be able to find your way through all your code.

With @import url(yourCSSfile.css) in a CSS file, you can create multiple CSS files: one for your page header, another for your footer, yet another for your article content, for your menu... Then you add @import url rules, or you can use the <link> tag with href="myCssFile.css", depending on your preference.

Example in an HTML5 page:

<style type="text/css">
@import url(monFichierCss.css);
</style>

There are priorities in CSS files. When there are multiple elements coming from multiple stylesheets, there are priorities. In ascending order of priority, from lowest to highest, you have:

  • Browser default properties
  • External stylesheets
  • Internal stylesheets
  • Inline styles

The highest priority goes to inline styles, which is what we recommend you use.

Otherwise, you have the !important declaration, which means it is possible to override all these priorities by using the !important value.

Is it better to link your CSS stylesheet with the <link> tag or use the @import rule inside a <style> block? This is a frequently asked question, as both techniques apparently produce the same result.

The @import rule in detail

@import is a CSS2 property that must be followed by the URL of a file containing styles to apply in addition to the current stylesheet. You can use @import:

  • between the <style> tags in the <head> section of an HTML page;
  • at the beginning of a CSS file, to include one or more other stylesheets;

This second option is interesting because it allows you to create more scalable stylesheets (you link a single file from the HTML page and manage the stylesheet imports directly from this root CSS file). The problem is that this approach can slightly slow down the loading of styles and therefore of the page, and it is not recommended in a client-side performance optimization approach.

Example:

<style type="text/css">
  @import url(/styles/habillage.css);
  @import url(/styles/texte.css);
</style>

You can optionally add a media list. However, note that Internet Explorer 6-7 did not understand this syntax and would not import the corresponding stylesheet at all (Internet Explorer 8+ fixed this issue).

<style type="text/css" media="print">
  @import url(impression.css);
</style>

This property also allows you to import stylesheets into other stylesheets. This offers possibilities for creating dynamic stylesheets without having to copy the same code multiple times.

In practice, what are the differences?

In practice, the result for the HTML document presentation is exactly the same, but there are two important subtleties:

  • @import (CSS2) is not recognized by very old browsers that are not yet up to CSS standards, so styles will be applied everywhere except on these dinosaur browsers. What's the point? Simply to allow users of these dinosaurs to browse the site without too many problems. Indeed, without a stylesheet the site remains more visible than with styles interpreted incorrectly. This is therefore a recommended technique for interoperability and backward compatibility.
  • On some browsers, @import seriously reduces performance, because this technique does not allow parallel loading of multiple stylesheets at the same time, which slows down page rendering and makes the visitor wait a bit longer than necessary. To learn more about this topic, see the original article "Don't use @import" by Steve Souders.

So the choice between the two techniques depends on your priorities: browser compatibility or loading performance.

Recent Posts

Recent Comments

"

Itamde is also an online programming school.

Itamde

Learn what you want, at your own pace

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

You may also be interested in...

Web

Behind the Scenes of a Web Development Project

When you visit a finished website, you only see the final result: clean pages, smooth animations, well-organized content. But behind this facade lies a far more complex creation process, made up of strategic thinking, technical decisions, and collaboration between...

Blog image

Essential Tools Every Web Developer Needs

The web development profession is constantly evolving, and with it, the tools needed to work efficiently. Between code editors, frameworks, debugging tools, and deployment platforms, the choices can feel overwhelming for beginners and experienced developers alike who...

Recreate Snake in HTML and vanilla JavaScript

Recreate Snake in HTML and vanilla JavaScript

Sometimes the best projects come into being without any particular pretension, just for the pleasure of coding and diving back into video game classics. That's exactly the spirit behind this retro version of Snake, built entirely with HTML and vanilla JavaScript, with...

Stay up to date with the latest news and developments

Access restricted content

Discover behind-the-scenes details of our projects, exclusive resources, and the progress of our creations in real time.

Sign up for our newsletter

Receive our news, creative insights, and updates from the studio directly in your inbox.

Follow us

Join our community on social media to follow our daily projects and interact with us.