{"id":59,"date":"2008-03-19T07:00:29","date_gmt":"2008-03-19T12:00:29","guid":{"rendered":"http:\/\/cssnewbie.com\/css-import-rule\/"},"modified":"2008-03-19T07:00:29","modified_gmt":"2008-03-19T12:00:29","slug":"css-import-rule","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/css-import-rule\/","title":{"rendered":"Using the CSS @import Rule"},"content":{"rendered":"<p><img src='https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/03\/crayons-500.jpg' alt='box of crayons' title=\"Photo by turbojoe. Used under a Creative Commons license.\" \/><\/p>\n<p>Even the most complex style sheet starts out with a single rule. But when you&#8217;re working on a particularly massive and complex website, over time your style sheet will inevitably start to reflect the site&#8217;s size and complexity. And even if you employ every <a href=\"\/organized-style-sheet\/\" title=\"5 Steps to a More Organized Style Sheet\">trick for organizing your CSS<\/a> in the book, you might find that the sheer size of the file is simply overwhelming. At that point, you might want to consider splitting your style sheet up into several smaller CSS files. That&#8217;s when the @import rule can come in quite handy.<\/p>\n<p>The @import rule is another way of loading a CSS file. You can use it in two ways. The simplest is within the header of your document, like so:<\/p>\n<pre lang=\"html4strict\" escaped=\"true\" line=\"1\">&lt;style&gt;\n\t@import url('\/css\/styles.css');\n&lt;\/style&gt;<\/pre>\n<p>But that isn&#8217;t necessarily the best method for keeping your XHTML small and your code clean. To that end, you can import a style sheet from <em>within<\/em> another stylesheet. And better still, you can import <em>any number<\/em> of styles this way. So your document\u2019s head could look like this:<\/p>\n<pre lang=\"html4strict\" escaped=\"true\" line=\"1\">&lt;link rel=\"stylesheet\" type=\"text\/css\" href=\"\/css\/styles.css\" \/&gt;<\/pre>\n<p>Nice and clean. But then your &#8220;styles.css&#8221; document can contain calls to any number of additional style sheets:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">@import url('\/css\/typography.css');\n@import url('\/css\/layout.css');\n@import url('\/css\/color.css');\n\n\/* All three CSS files above will be loaded from\n   this single document. *\/<\/pre>\n<p>This lets you break up your gargantuan stylesheet into two or more logical portions &mdash; I chose typography, layout and color for this example, but you might prefer dividing your CSS according to the site sections they style (content versus sidebar, etc) or something similar. Either way, the benefit is immense &mdash; you can have the same number of CSS rules overall, but in smaller, easier to manage units.<\/p>\n<p>You can even load both your screen <a href=\"\/7-tips-print-style-sheets\/\">and print<\/a> (or handheld, etc) stylesheets all at the same time using this trick. If you&#8217;d like to specify a media, just write your rules like this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">@import url('\/css\/header.css') screen;\n@import url('\/css\/content.css') screen;\n@import url('\/css\/sidebar.css') screen;\n@import url('\/css\/print.css') print;<\/pre>\n<p>Like most cool things in CSS, this one comes with <strong>a couple of caveats:<\/strong><\/p>\n<ul>\n<li>The @import rule isn\u2019t recognized by the really old browsers out there\u2026 Netscape Navigator 4 skips over @imports entirely, and Internet Explorer 4 ignores them if you don\u2019t use the (entirely optional, but generally used) parentheses. Of course, not too many people these days use either of these archaic browsers. And this problem can actually be <em>useful<\/em> if you\u2019d like to hide some or all of your CSS from the browsers that can\u2019t really support it.<\/li>\n<li>Your @imports <strong>must<\/strong> come before all other content in your CSS. And I mean <em>all<\/em> of your content. Even putting comments before the @import tag will cause your imports to fail. So be sure to do your imports before you do anything else.<\/li>\n<li>Internet Explorer (you knew it\u2019d come up eventually) doesn\u2019t deal well with specifying media types \u2013 it chokes. Basically, IE (versions 4-7) try to read the media type like it were part of the file name, causing the whole thing to come crashing down. As such, if you don\u2019t want your CSS to have a default media type of \u201call,\u201d you\u2019re probably better off using a combination of the &lt;link&gt; tag and imports \u2013 specifying a media type in your link, and then importing the appropriate CSS within the file you\u2019re linking to.  I haven\u2019t yet heard if IE8 suffers from this same problem (if you happen to know, please enlighten me in the comments!).<\/li>\n<\/ul>\n<p>But even with those caveats in mind, this can be a really useful technique. No matter how big your website ends up getting, you\u2019ll be able to keep your style sheets under control.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<\/p>\n<p>Even the most complex style sheet starts out with a single rule. But when you&#8217;re working on a particularly massive and complex website, over time your style sheet will inevitably start to reflect the site&#8217;s size and complexity. And even [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/css-import-rule\/\" title=\"Click to read 'Using the CSS @import Rule'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[173,189,192],"tags":[79,285,311],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/59"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=59"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/59\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}