Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

David has posted 35 posts at DZone. You can read more from them at their website. View Full User Profile

Combine Your CSS Media Styles Into One File

01.31.2008
Email
Views: 2251
  • submit to reddit

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="all" />
<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?

References
Published at DZone with permission of its author, David Walsh. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Eric Wendelin replied on Thu, 2008/01/31 - 1:33pm

I totally agree!  Using media selectors may speed up your site because you are saving the additional HTTP request.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.