18

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?

Athiwat Chunlakhan
  • 7,589
  • 14
  • 44
  • 72
  • You assume @import will process files in a particular order... it may not. http://www.stevesouders.com/blog/2009/04/09/dont-use-import/ – Webveloper Mar 24 '13 at 06:43

4 Answers4

18

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.
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Yes you are right but it's very confusing. There's @media and stuff which I'm very confuse at. I got two files from twitter bootstrap, one is called Bootstrap and the other is Bootstrap-responsive. I need to add `body { padding-top: 60px; padding-bottom: 40px; };` in the middle and I can't omit anything in this. Or else it will not work. – Athiwat Chunlakhan Mar 25 '12 at 14:23
  • @JonathanShepherd Who says that you **have to** put the CSS between it? If there's no definition of `body {padding-top: ...; padding-bottom: ..;}` or `padding:` in the second CSS file, you can safely put it after the second `@import` rule. – Rob W Mar 25 '12 at 14:25
  • There's one `@media (max-width: 980px) { body { padding-top: 0; } ......` Now there's a huge problem, If I remove my **some css** the text will not get padded at the top. Which result in the nav-bar at the top overriding my content. If I add **some css** after bootstrap-responsive then the mobile view will get the wrong padding at the top(it pad the nav-bar not the content). – Athiwat Chunlakhan Mar 25 '12 at 15:04
  • here's three situation that I can get into. To clear what I have said above. 1. boot > code > respon = Perfect. Both desktop and mobile. 2. boot > respon = Content not padded, nav-bar override. 3. boot > respon > code = Perfect for desktop view. It adds padding at the top of the nav-bar AND the content in mobile view. If you need some test code I will add them. – Athiwat Chunlakhan Mar 25 '12 at 15:08
  • @JonathanShepherd The only thing I can think of requires one additional tag: http://pastebin.com/VBD5Vcu1 – Rob W Mar 25 '12 at 15:21
  • 1
    I found a solution, the only way to do it is to include all 3 css file to one file only by hand. and import it. I will mark your answer to correct but for other looking for this solution, look at this comment. Ty. – Athiwat Chunlakhan Mar 25 '12 at 15:24
  • @JonathanShepherd please see my answer to a possible (and easier to maintain) solution – Raffaele Mar 25 '12 at 16:52
  • 1
    @GeorgeKatsanos that's why I remove it and combile all of them by hand). – Athiwat Chunlakhan Mar 25 '12 at 17:01
5

As others said, this is invalid CSS, but I want to add my 2 cents. If you are looking for a way to overcome this problem and also write better CSS (ie CSS that is more concise, easier to read and to maintain), you can use SASS. It's a program that enhances the CSS syntax and generates valid CSS files ready to be served to browsers. It's installed as a Ruby gem, then you can launch it this way

sass --watch  style.scss:style.css

Every time style.scss is modified, a new style.css is compiled. scss is the standard file extensions for SASS source files. In SASS this code is valid

@import "import1.scss";

a {
  color: red;
}

@import "import2.scss";

and compiles to the following style.css

a {
  /* This is the content of import1.scss */
  color: white;
}
a {
  color: red;
}
a {
  /* This is the content of import2.scss */
  color: white;
}

Note that I imported files ending in scss. This is advertised by the SASS reference not to fall back to regular CSS imports.

Raffaele
  • 20,627
  • 6
  • 47
  • 86
  • Seem like a very good nice solution but I'm already using LESS for the client-side. From what I read sass is on the server-side and should work with LESS. I will take a look at this. Thanks @Raddaele – Athiwat Chunlakhan Mar 25 '12 at 17:04
  • upvoted, you practically saying that i can import other css file inside of my current css module in any where i wanted ???? – amdev Nov 27 '18 at 05:53
1

This is odd, but CSSWG intentionally disallows @import after property rules.

They say:

Flexibility includes the Working Group's ability to assign some meaning to @import after a certain point in a style sheet in the future without the new behavior being in conflict with existing practise in certain ways. You could read the current requirements as saying that using the @import rule at certain points as something the Working Group reserves for later use when it finds a usage that is more valuable to authors than being able to use it with the usual meaning right now. Assinging a specific meaning to it now would probably remove that option, so this may be a loss in the long term.

Also they say that @import itself descreases downloading speed (that's weak argument though, since it then does not matter where @import is placed — before property rules or after them, but former is allowed).

So we are forced to use a workaround like using one "TOC" stylesheet that only contains @imports in desired order (but that increments number of files that browser needs to download).

Marat Tanalin
  • 13,927
  • 1
  • 36
  • 52
  • to be honest speed really doesn't matter. Both of my test case works fine in product time after minified. The minified engine works fine with both type of import. The problem is on development and how to build script which I can't change works. It works by minifying all css in "style.css" only which I have to use import to get other css. So my solution right now is to combine all of them by hand, which works nicely. – Athiwat Chunlakhan Mar 25 '12 at 15:20
-4

Use !important to prevent overriding

Something like this

<style>
.el{color:red !important;}
.el{color:blue;}
</style>

The color will stay red

Maurice
  • 1,082
  • 1
  • 20
  • 43