Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS @import and the order of it

Tags:

html

css

Is it possible to use @import in one css file like this.

@import file1
some css here
@import file2

chrome doesn't recognize the second import for the above but this will work

@import file1
@import file2
some css here

The problem here is I need "some css" to be in the middle because file2 will override some of them. Is there any other solution other than importing 3 files from html?

like image 825
Athiwat Chunlakhan Avatar asked Mar 25 '12 14:03

Athiwat Chunlakhan


People also ask

In what order are CSS styles applied?

Style rules take precedence in right-to-left order. Rules to the right take precedence over rules to the left. Properties that are not declared are carried over and reapplied to succeeding style sheets.

How does @import work in CSS?

The @import rule allows you to import a style sheet into another style sheet. The @import rule must be at the top of the document (but after any @charset declaration). The @import rule also supports media queries, so you can allow the import to be media-dependent.

Does CSS go in order?

CSS Order Matters In CSS, the order in which we specify our rules matters. If a rule from the same style sheet, with the same level of specificity exists, the rule that is declared last in the CSS document will be the one that is applied.

Why is @import only at the top?

98. Why can @import be at the top only? A style sheet that is imported into another one has a lower ranking in the cascading order: the importing style sheet overrides the imported one. Programmers may recognize this as the same model as in Java, Modula, Object-Pascal, Oberon and other modular programming languages.


1 Answers

That is not possible:

From http://www.w3.org/TR/CSS21/cascade.html#at-import:

The '@import' rule allows users to import style rules from other style sheets. In CSS 2.1, any @import rules must precede all other rules (except the @charset rule, if present).

From http://www.w3.org/TR/CSS21/syndata.html#at-rules:

Assume, for example, that a CSS 2.1 parser encounters this style sheet:

@import "subs.css";
h1 { color: blue }
@import "list.css";

The second '@import' is illegal according to CSS 2.1. The CSS 2.1 parser ignores the whole at-rule, effectively reducing the style sheet to:

@import "subs.css";
h1 { color: blue }


There is no need to define the rules between the @imports though. If you want to override properties in file 1, they can also be added after the @import blocks. Properties which are overridden in file 2 can be omitted.
like image 192
Rob W Avatar answered Nov 15 '22 21:11

Rob W