2 Importing Style Sheets: the @import rule
2. Importing Style Sheets: the @import rule
The @import rule allows users to import style rules from other style sheets. If an @import rule refers to a valid stylesheet, user agents must treat the contents of the stylesheet as if they were written in place of the @import rule, with two exceptions:
If a feature (such as the @namespace rule) explicitly defines that it only applies to a particular stylesheet, and not any imported ones, then it doesn’t apply to the imported stylesheet.
If a feature relies on the relative ordering of two or more constructs in a stylesheet (such as the requirement that @namespace rules must not have any other rules other than @import preceding it), it only applies between constructs in the same stylesheet.
For example, declarations in style rules from imported stylesheets interact with the cascade as if they were written literally into the stylesheet at the point of the @import.
Any @import rules must precede all other valid at-rules and style rules in a style sheet (ignoring @charset), or else the @import rule is invalid. The syntax of @import is:
@import [ <url> | <string> ] <import-conditions> ; <import-conditions> = [ supports( [ <supports-condition> | <declaration> ] ) ]? <media-query-list>?
where the <url> or <string> gives the URL of the style sheet to be imported, and the optional <import-conditions> state the import conditions under which it applies.
The following conditional @import rule only loads the style sheet when the UA supports display: flex, and only applies the style sheet on a handheld device with a maximum viewport width of 400px.
@import url("narrow.css") supports(display: flex) handheld and (max-width: 400px);
If a <string> is provided, it must be interpreted as a <url> with the same value.
The following lines are equivalent in meaning and illustrate both @import syntaxes (one with url() and one with a bare string):
@import "mystyle.css"; @import url("mystyle.css");