Blog Archive
-
▼
2012
(25)
-
▼
September
(11)
- IE and the box model
- Box model recap
- Visual Formatting Model Overview
- Removing comments and optimizing your style sheets
- Note to self
- Structuring your code
- Applying styles to your document
- Planning, organizing, and maintaining your style s...
- Inheritance
- Adding a class or an ID to the body tag
- Using specificity in your style sheets
-
▼
September
(11)
Labels
- CSS (25)
- Tutorials (4)
- Web design (7)
Powered by Blogger.
Sunday, September 2, 2012
Adding a class or an ID to the body tag
One interesting way to use specificity is to apply a class or an ID to the body tag. By doing this, you can then override styles on a page-by-page or even a site-wide basis. For instance, if you wanted all your news pages to have a specific layout, you could add a class name to the body element and use it to target your styles:
body.news {
/* do some stuff */
}
<body class="news">
<p>My, what a lovely body you have.</p>
</body>
Sometimes, you’ll need to override these styles on a particular page, such as your news archive page. In this case, you can add an ID to the body tag to target that specific page.
body.news {
/* do some stuff */
}
body#archive {
/* do some different stuff */
}
<body id="archive" class="news">
<p>My, what a lovely body you have.</p>
</body>
Using a class for the type of page and an ID for the specific page gives you a huge amount of control over the design and layout of your site. As such, this is one of my favorite techniques for writing maintainable code.
Címkék:
CSS
Subscribe to:
Post Comments (Atom)
0 Comments:
Post a Comment