Combine Your CSS Media Styles Into One File

Written by David Walsh on November 23, 2007 · 26 Comments

When a user comments on one of my blog posts, and they provide a website URL, I always visit the site. I appreciate the time a visitor takes to comment on a post, so I return the favor by checking out the user's website...and the source code.

Often I see the following:

<link href="styles/main.css" rel="stylesheet" type="text/css" media="screen" />
<link href="styles/print.css" rel="stylesheet" type="text/css" media="print" />

The above code requests two separate stylesheets, one for global media styles (screen, print, handheld, tv...) and one for print only. There's nothing wrong with the above, but if load time is an issue you could save yourself a server request by combining your CSS files:

/*  all media  */
@media all {
	body	{ color:#666; font:13px arial, helvetica, sans-serif; padding:20px 0 30px 0; }
}

@media print {
	body	{ color:#000; font:12px arial, helvetica, sans-serif; padding:0; }
}

You address each media by using "@media [media] { /* css here */ }". I also use a single CSS file to make editing easier -- no switching buffers, and how often do you set specific styles for print and screen per a CSS class?

Comments

  1. Wow, this is great. Going to put this to use soon. My only question is do all the major browsers support doing this?

  2. Absolutely, Greg — this is basic CSS syntax. Thanks for sharing your question with everyone!

  3. Really useful piece of information. Thanks for sharing!

  4. So, this means that media=”" should be removed from tag in head of document?

  5. @Ivan: Right Ivan, you don’t need that if you use the above code.

  6. That’s funny, I just wrote about the same tip some days ago !

    You might want to link to my post, as a french “translation” of yours.

  7. Wow, that’s new for me!
    Thanks for sharing!

  8. Martyn P May 3, 2008

    Thanks so much! I had no idea that this was possible!

  9. Question, if its so popular then why is it that developers not use this more often? I question this because my primary development platform is Joomla! and you see CSS and JS being used freely and all over the place. For instance I did a little test to see how many CSS files where in my website folder (NOT HOW MANY WHERE BEING USED) and the total…. are you ready for this: 1457!

    No joke at all! So, from a user/developer standpoint I see some issues with wanting to use this.
    1. Have to sort out all of the CSS Files and figure out whats real and whats now
    2. Have to spend a LOT of time doing so
    3. It goes on

    On the other hand to reduce HTTP Request would be beneficial as mentioned above for load times so for someone developing a small platform with minimal CSS files this seems like a good option. I may give it a try if I can reduce 1400 to 100.. lol

  10. Thanks for the post. What do you all think of the future of blogging? I have been reading a lot about google’s attempt to gain preferential treatment for its site from internet providers. This will result in their sites being faster than sites owned by individuals and/or smaller comapnies. It’s my beleif that if google is able to succeed there will be far less websites out there and therefore far less blogging. For more information : http://www.allrentdeals.com/City/noida-rent-commercial-properties.aspx

  11. Hi David,

    I have stumbled upon your website a couple of times now while searching for web related issues. Each time i find a clear, well written and useful post. Thanks for the efforts, keep up the good work.. its not all about tweets!!

    Ta!

  12. David, nice one! the @media all was key for me. I was looking for a way to hide elements/controls when printing WITHOUT having to include all the sceen css stuff inside the print section. I got my answer on YOUR blog. Keep up this Blog, it’s important to share like this PLUS it takes time.

    @media all{
    body
    {
    font-family: Verdana;
    color: #808080;
    background-color: #CCCCCC;
    }

    }

    @media print
    {
    .noShow {display:none;}

    }

    Thanks
    Dave Stuart (Calgary, Alberta, Canada)

  13. Good point, especially as IE allows only two open conection at the time… So, same is true for JS files and images (css clipping)…

  14. This is what I was looking for exactly. I had two CSS files now I combined and made them one.

    Thanks

  15. With larger sites, css files can grow to a point where any give page may only be using 15% percent of your css classes at a time. If your css classes are broken up into page specific groups, you can cut down on load time by only calling the classes you need on a page by page basis. This will cut down your css load time significantly and improve performance.

    • Agreed. This isn’t an enterprise solution. Keep in mind that many large sites don’t manager their resources that well, though. HTML a mess. A billion JS files included. This wouldn’t be the worst solution in the world. :)

  16. Hi David,
    Nice post, I’ve been recently looking for some additional information about combining my CSS and JS files but haven’t really gotten around to doing it, or at least feeling safe about it.

    I use wordpress and I have about 30 plugins active at the moment. So would I be able to combine most of the plugins CSS files into one? making sure there are no conflicts between ‘names’ of certain styles right?

  17. I don’t know which files are using the css files. Is there any tools to find out which files reference the css files?

  18. When combining two separate SYTLESHEETS into one STYLESHEET that is making it three but you are referencing the third stylesheet,which contains the two different sytles to your HTML page….How is done ? PLEASE RESPONDS !!

  19. Searched in google 2 sec, read your post 5 sec, found the solution.

    only 7 sec to solve my problem, thank you!

  20. So after I combine my style sheets into one, lets call it “styles.css” what do I put in the head of the HTML portion? Will it still be or do I need to put something else?

  21. While this is a great solution is your a web developer, in my case this is too technical for me to implement.

    Is there any easier way….maybe with a plugin that will do this?

    While our site loads on average anything from 700ms to 1.2 seconds (depending where in the world you are) we would like to improve it.

  22. how to bloack print screen option in website ?

  23. [...] Kombinera din CSS Media Styles till en fil [...]

Be Heard

Tip: Wrap your code in <pre> tags or link to a GitHub Gist!

Use Code Editor
Older
Weekend Links - CSS Art, URL vs. URI, Web Browsers, MooTools Mocha UI, Web 2.0, and MooCanvas
Newer
Horizontally Center Your Website Structure Using CSS