tags:

views:

121

answers:

4

In css should I 'name' my page at the top level - like this :

<div class="page_products">

in order to write css specific to that page like this :

.page_products h3 {
   font-color:red;
}

Note: This is a completely random and probably not useful style override. H3 is the first thing that came into my head. I'm just trying to describe the practice.

I've seen a few places where this kind of practice is described - including one answer to one of my other posts, and I kinda think its horrible. I'd much rather override in a <style> tag at the top of my page or create a second .css file (less likely). But then if I look at it another way it is very appealing and I'm tempted to use it.

What do others think of this practice. It looks scarily unmaintainable to me. What are the advantages or pitfalls?

Additional point: I may also be trying to write a .css file specific for a 'iPhone' version too - and a little worried that multiple css files for that could get really clumsy to manage.

+11  A: 

Rather than have a pile of one-off CSS files with 1 or 2 rules each in them, I have found it useful to give the body an ID or class attribute similar to your example. Ultimately, pragmatism should win. Simplicity means different things at different scales - if you have massive CSS changes per page, it suddenly becomes a lot simpler to maintain different CSS files and swap the include statements. But with just a handful of rules, maybe only a few edge case pages, it is easier to include all the rules in your main stylesheet.

Rex M
this ones definitely winning so far ;)
Simon_Weaver
+3  A: 

I generally favor specifying additional CSS files. However, it results in an additional HTTP request and slows the loading time. <style> tags in the html head are unmaintainable in the long-term.

If you have scenarios where you override styles for few items for specific pages, then this approach is reasonable.

It would be better to use the body instead of a div to apply a class or id. It's cleaner, and is easier to spot for future maintenance.

gmcrist
what kind of content on the page would drive you to create a new css file? in my case i have quite different pages : testimonials, contact us, products, gallery. they're really quite different and i'm thinking they deserve their own files (or style tags). may be easier to work as a team too
Simon_Weaver
If you use HTTP caching effectively, this results in ONE additional request per the entire browsing session, and you don't have to push the inline styles (which quickly add up, btw) over and over with every page.
Piskvor
+1  A: 

Giving elements names makes things nice and easy when people want to mess with your rendering using tools like Greasemonkey. They might only want to "fix" your "page_products" tags.

For example, there's a torrent site that I'm a member off which displays a massive series of checkboxes that I don't want to look at when I'm listing the new torrents. With Greasemonkey I was able to apply some display:none CSS to the named element, making it invisible every time I go there.

mlambie
interesting. so what if i *dont* want people messing with my site in greasemonkey ;-)
Simon_Weaver
Why would that be the case?
mlambie
+2  A: 

Sometime you just don't have a choice, and you've got to have specific css for certain pages .

I'm usually adding a unique ID to the body of each page - no need for the additional div then.

<body id="page-x">
    <h3>Page Title</h3>
</body>

and then if necessary, I add as little as possible to the main stylesheet (to prevent additional http requests)

#page-x h3 {
    custom css...
}
yoavf