ID Your BODY: A Simple CSS Trick With a lot of ROI

Friday, January 30th, 2009

One of the latest raves in Web design is to add an id attribute to the body tag, like <body id="coolpage">. It's a small thing, but it's a damn good idea!

Why? Let's say you have 15 pages total on your Geocities Star Wars fan site. You want the headlines (<h1>Headline!</h1>) to be 16 px in size with the exception of the home page. On the homepage you want the headlines to be 18px and bold!

Will you make a new separate CSS file just for the homepage that the user has to download in addition to the default CSS? NO! You're too good for that! You & C3PO hang out on the weekends.

Instead of all that extra CSS file nonsense, you can use <body id="homepage"> in replacement of <body>, which enables you to assign unique CSS only to the homepage like so:

/* for all pages */
h1 { font-size: 16px; }

/* for homepage only */
#homepage h1 { font-size: 18px; font-weight: bold; }

And BOOM! Your homepage headlines are styled differently without any hacks or additional files. That's awesome and incredibly useful.

So why not stop there? Because adding id attributes to the body element of each page takes time and work. When you start that new section on your Geocities Star Wars fan site about Princess Layla's secret love affair with R2D2, you'll be adding lots of id attributes for all the juicy pages. It'll be redundant and boring.

Let's use PHP to make things A LOT easier. Use the following line in replacement of <body> and your pages will automatically have id tags based on their filenames.

<body id="<?php echo (str_replace('/', '_', substr($_SERVER['REQUEST_URI'], 1, strlen($_SERVER['REQUEST_URI']) - strrpos($_SERVER['REQUEST_URI'], '.') - 1)) ? str_replace('/', '_', substr($_SERVER['REQUEST_URI'], 1, strlen($_SERVER['REQUEST_URI']) - strrpos($_SERVER['REQUEST_URI'], '.') - 1)) : 'index'); ?>">

There you have it. Use that line in your HTML template, and your pages will have id attributes assigned to <body> automagically. Yippy!

For example, c3po.php will become #c3po, c3po/loveaffairs.php will become #c3po_loveaffairs (forward slashes "/" are replaced with underlines "_"), and your homepage will be #index. Enjoy!


If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!

ABOUT THIS AUTHOR

Brian Cray is a marketing & Internet strategy consultant, web developer, and Apple fanatic living in Columbus, Ohio, USA. His work has been featured by Mashable, NETTUTS, Smashing Magazine, and many others. He's happy to share his knowledge with you on DevTips and his own blog at Brian Cray.com.
  1. January 30, 2009 at 5:17 pm
    • April 3, 2009 at 8:10 am
  2. January 30, 2009 at 7:44 pm
  3. January 30, 2009 at 10:58 pm
    • January 31, 2009 at 6:44 pm
  4. February 3, 2009 at 8:34 am
  5. February 4, 2009 at 7:45 pm
  6. February 18, 2009 at 8:19 pm
  7. Paperboy
    February 19, 2009 at 6:26 pm
  8. March 8, 2009 at 3:38 pm
  9. John
    March 18, 2009 at 8:46 am
  10. Paperboy
    March 20, 2009 at 9:53 pm
  11. April 3, 2009 at 8:11 am
  12. April 30, 2009 at 4:48 pm
  13. u24
    May 23, 2009 at 12:06 am

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates