Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Combine Your CSS Media Styles Into One File

22 Responses »

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?

Discussion

  1. gregf
    November 25, 2007 @ 12:57 pm

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

  2. November 25, 2007 @ 9:06 pm

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

  3. November 26, 2007 @ 2:57 am

    Really useful piece of information. Thanks for sharing!

  4. ivan
    April 25, 2008 @ 10:28 am

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

  5. April 25, 2008 @ 10:31 am

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

  6. April 25, 2008 @ 12:18 pm

    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. April 26, 2008 @ 8:06 am

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

  8. April 27, 2008 @ 4:40 pm

    Sweet tip!

  9. martyn p
    May 3, 2008 @ 10:27 am

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

  10. April 28, 2009 @ 12:51 am

    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

  11. July 9, 2009 @ 6:53 am

    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

  12. September 26, 2009 @ 8:58 am

    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!

  13. December 18, 2009 @ 12:11 am

    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)

  14. May 24, 2010 @ 11:40 pm

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

  15. May 30, 2010 @ 10:33 pm

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

    Thanks

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!