tags:

views:

242

answers:

11

I am updating a website to add some mobile friendly pages.

At the moment we have one big css page with everything in. My idea is to put all the mobile specific css into a separate file and then link both sheets. The mobile css will overide anything in the default css (bigger buttons etc).

Im quite new to css, what is the best practice?

+2  A: 

yes you should use more than one css file rather using one big file. It helps you while maintaining your site also use different definitions (classe or id names) in different css otherwise it will take the one which declared later. For example

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
   <html>
     <head>
        <link href="/stylesheets/stylesheet.css" media="screen" rel="stylesheet" type="text/css" />
        <link href="/stylesheets/lightbox_new.css" media="screen" rel="stylesheet" type="text/css" />
        <link href="/stylesheets/another_css.css" media="screen" rel="stylesheet" type="text/css" />
      </head>

      <body>
         <!-- Your content here -->
      </body>
  </html>
Salil
would you then use different `media` values?
xtofl
If there is two definitions for the same selector in stylesheet.css and another_css.css, which would be selected?
Robert
@Robert If the selectors are identical, the stylesheet which is declared later in the source will have priority. In this case, that would be another_css.css.
AaronSieb
it will use last defination then.if you want defination should change for the specific page you can override it in your page also.
Salil
@xtofl: Different media values gives you the ability to serve specific stylesheets to mobile devices, for print, TVs etc.
Arve Systad
+4  A: 

In the case of styles for specific clients, I would say that it is a best practice to separate them.

Mitchel Sellers
+1  A: 

I'd use two as well. Keeps things more tidy when editing for each device (computer and mobile device). I have one huge CSS stylesheet which I use for all browsers with the help of the css browser selector script, and I hate having to scroll through 6000+ lines of CSS, so I'd say the best way at least from experience is to separate them out!

Kyle Sevenoaks
+7  A: 

One large CSS file leads to fewer HTTP requests, which can improve performance.

Several smaller files leads to easier organization which will make development and maintenance cheaper and easier.

AaronSieb
Excellent answer, concisely said.
Atømix
+5  A: 

I have a few stylesheets for any significant app I've worked on.

  • base.css - always applied.
  • print.css - this hides menus and other parts of the screen not really good for a printed page. Triggered by the media attribute. <link rel="stylesheet" type="text/css" media="print" href="print.css" />
  • ie6.css - applied second, and if and only if it's IE6. I hope to throw this out someday.
  • <clientname>.css - one stylesheet for each client that wants the site to have their logo/etc.

If I were trying for blazing fast performance, I'd combine them. However, I know sites getting hundreds of millions of hits a day don't bother, so I'd strongly recommend splitting them however makes sense to you, in order to make it easier to maintain.

For the most part, extra hardware is cheaper than extra developer hours and/or more bugs. Maintainability is usually the highest goal for me.

Dean J
+1  A: 

Group your CSS meaningfully and serve it carefully.

For example, if you have CSS that is applied through out your site (e.g. CSS reset) make it separate file and include it for each page.

Then for each logical component of your site create separate CSS file and serve it on pages that belong to respective logical component. (Say you have a blog and polls on your site, if blog never needs CSS for polls you don't need to include it in blog.) But bare in mind this isn't practical for small sites.

Group your CSS by media for which they are used. If you have style sheet for printing keep it separate of your basic sheets if it makes sense (don't use separate files if you only have single CSS property for printing since it is not worth the request time).

Keep in mind that more sheets assume more HTTP requests and each request costs certain amount of time.

So there isn't explicit way these thing should be handled, it's all about making your CSS easier to maintain and easy for client to download (less HTTP requests, smaller size etc.)

rebus
+4  A: 

Using separate stylesheets for different media is easily done.

<link href="browser.css" media="screen" rel="stylesheet" type="text/css" />
<link href="mobile.css" media="handheld" rel="stylesheet" type="text/css" />
<link href="print.css" media="print" rel="stylesheet" type="text/css" />

In this case, all the style will be downloaded and applied when the media type matches the device.

However there is another method which is neat if your app is designed for mobiles, because it downloads the stylesheet ONLY if the media type matches.

<style type="text/css" media="screen">
  @import "screen.css"; /* Note that some (older?) browsers don't support @import, so you may have to download this sheet the traditional way even on mobiles */
</style>
<style type="text/css" media="handheld">
  @import "mobile.css";
</style>
<style type="text/css" media="print">
  @import "print.css";
</style>
Tor Valamo
I think importing more than one css in a one <style> tag will be more better.
Salil
The point is the media attribute is different in each style tag...
Tor Valamo
+1  A: 

I would use multiple style sheets to keep things better organized, then compress them into one file before putting them on the site, to improve performance.

GSto
+1  A: 

You should have a range of CSS sheets for various tasks, else things get messy fast!

A: 

I think its better to use 1 for style, 1 for ie6 one for ie7. Nothing more. Organization should be automatic inside the style.css. Using logical classnames and comments.

Less httprequests is good. Less markup is good. :)

no1cobla
A: 

I prefer two style sheets myself. The first one, and the one that always comes first in my HTML, is a reset style sheet. The implementation of this first style sheet helps web pages to display more consistently across different browsers.

Often, it is not necessary to create more than one additional style sheet. Generally, CSS commands specific to IE are ignored by Firefox and other compliant browsers and vice-versa. The real problem arises when an item on a page must be positioned and sized to be exactly the same across multiple browsers - at that point, more than two sheets become necessary although it is sometimes possible to get good results through proper ordering within the sheet.

Shadeclan