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

ID Your Body For Greater CSS Control and Specificity

01.18.2008
| 4604 views |
  • submit to reddit
Let's say you want to change the color of your links on just your contact page to red. They are blue on every other page, but it just makes sense for them to be red on your contact page (for some reason). There are a couple ways you could go about this.
  • You could declare a separate stylesheet for your contact page. This isn't ideal, because it's redundant. If you make any other changes, you'll always have to make them both on the main stylesheet and the contact page stylesheet.
  • You could give all those links a unique class on that page. This isn't ideal, because it isn't very semantic and it's also redundant. Why apply a class to every single link on the page when they really aren't any different from links elsewhere on the site, contextually speaking?
  • The best solution is to give your the body a unique ID. This solves the problem perfectly. You can use the same stylesheet and target just the links you want to with a single CSS selector.

 

How it's done

Simple, literally just apply the ID to the body tag:
   ...
</head>

<body id="contact-page">
...
Now for our example of making all links on the contact page red instead of blue, just use some CSS like this:
a { 
color: blue;
}

#contact-page a {
color: red;
}

 

How about a more practical example?

You got it. One of the most useful implementations of this technique is within navigation. Take a look at this sample navigation: tabbednav.jpg See how the forums tab is the "active" tab? Certainly that's just a slight change in CSS, probably just a shift in the position of a background image. Perhaps the XHTML looks something like this:
...
<li><a href="/fieldtips">Field tips</a></li>
<li class="active"><a href="/forums">Forums</a></li>
...
The "active" class applied to the list item is what shifts the background image. That'll do the trick, but what about when we move to the Field Tips page? We will have to remove the active class from the forums tab and apply it to the the Field Tips tab. That's not very convenient. That means the code for the navigation block is unique on every single page of the site. So let's say down the road we want to add a Contact tab, we'll have to alter the code on every single page. No fun. Let's do this a little smarter. First we don't want to include the navigation block of code on every page, we want to include it, probably with a simple PHP include like this:
<?php include_once("nav.html"); ?>

 

But then how do we apply the "active" class to the current navigation list element?

This is where we apply what we just learned about giving unique ID's to the body! Instead of applying a class to only the active list element, let's apply a unique class to each separate list item as well as give our body an unique ID.
   ...
</head>

<body id="field-tips">
...
<li class="fieldtips"><a href="/fieldtips">Field tips</a></li>
<li class="forums"><a href="/forums">Forums</a></li>
...
Now we can target specific elements in the navigation with some clever CSS:
#field-tips li.fieldtips, #forums li.forums {
background-position: bottom;
}
This means that the code for the navigation block can stay the same on every page, yet only the navigation element native to that page will be affected by this CSS and "flip" to the active state.

 

Let's get even more dynamic

CSS-Tricks reader Brian left an awesome comment on how you can use PHP to apply the unique ID to to the body element:
<body id="<?= basename($_SERVER['PHP_SELF'], ".php")?>">
This will return the name of the PHP file being executed as the ID (e.g. body id="index.php"). To leave off the .php part, just remove the ".php" part. More info on basename. More info on $_SERVER.

 

Original post here.
Published at DZone with permission of its author, Chris Coyier.

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