tags:

views:

208

answers:

3

If i can include css per page basis then should i mak different css file for each page if needed other than reset css file?

i use eric meyer css reset code in main.css

+5  A: 

Everything that is shared between multiple pages should go into a single CSS file.

If you have certain elements on only one page then it is ok to put this into an extra CSS file. This also prevents your main CSS file from getting to large.

Keep the following in mind: Don't repeat yourself. When you noticed that you put the same CSS configuration in two different CSS files, this is an indicator that this configuration should probably go into the main CSS file or both pages should at least use the same CSS file.

Edit:

You can give the elements that should share common settings the same class. E.g.

.foo {
    color: green;
}
/* in this case, is the same as : */

#div1, #div2 {
    color: green;
}


<div id="div1" class="foo">
</div>

<div id="div2" class="foo">
</div>

Also remember that elements can have more than one class, e.g. <div class="foo bar"></div>

Maybe a CSS introduction also helps.

Felix Kling
+1 also keep in mind that every custom CSS file must be loaded by the browser. A central file is loaded just once, and then cached.
Pekka
+1 to Pekka: I reckon that, as a horribly vague rule of thumb, unless you’ve got **a lot** of styles that are only used on one page, you might as well shove them in the main file. With gzip compression they might not increase its size very much at all, and as you say, that file can be cached for a long time.
Paul D. Waite
but what if i'm using css properties are same but many pages are having that element like page-a, page-b, page-c{.........}. where i should put this type of css.
metal-gear-solid
@felix - either you are getting my comment wrong or my comment was not clear see here what i was asking http://stackoverflow.com/questions/2138032/how-to-write-this-css-in-shorter-way-see-example
metal-gear-solid
@Jitendra: Well you can add a class to all these elements and then write `.theclass p {...}`.
Felix Kling
+2  A: 

A common solution is to put everything in one css file, with either small css files for parts that change or a class on body so you can write:

body.single #some:selector {
    #css
}

body.blog #some:selector {
    #css
}

As said before, don't repeat yourself!

Pepijn
what if not possible to give .class to body?
metal-gear-solid
A: 

Of course you can do this, but you should not, for two reasons (and more, which I can't think of right now):

  1. You lose caching speed. While it's true that the page will be faster on first load (smaller CSS), each page needs to get its own CSS loaded. One CSS for all pages -- assuming it's not ridiculously huge -- lets the server cache and the client cache better. Mostly the latter is what we're trying to allow with one CSS.
  2. Unless your site is truly beyond strange, you probably do NOT want to have different CSS for each page. A uniform look and feel demands a unified stylesheet.

As someone once told me on SO, "there are no absolutes, ever! :)" So while you might have a rare case that justifies many many CSS files, I recommend that you see if you can take advantage of unifying in one unless you have a good reason not to.

From your question it's not clear if you need help on using one stylesheet to address different elements on different pages. Generally, you can handle that by getting used to multiple classes, like:

<div class="one two three">

but CSS addressing is very flexible and you can do everything you want without having to use separate files. Unless you want to.

Yar