CSS Grouping and Nesting

In CSS it is possible to nest and group selectors together.

The easiest way to explain CSS grouping and nesting is by using examples.

Let’s say I want to have all my h tags (h1-h6) in a green font colour, rather than the rest which are all black. All I need to do is list the tags I want to use as selectors one after another, just before the declaration:
h1,h2,h3,h4,h5,h6{
  color:green;
}

This works for classes too:
.bold,.bolded,.bolder{
  font-weight:bold;
}

You can also nest selectors. Let’s say I have a div, in which content resides, but I want that content to be styled differently to the rest of the page, without having to declare a style for everything.
.my_div{
  background-color:green;
}
.my_div p{
  color:white;
}

That code will make all paragraph tags WITHIN the .my_div element to be styled that way. You will find this a massive time saver and of great use when dealing with larger web pages.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Live
  • MSN Reporter
  • MySpace
  • PDF
  • Ping.fm
  • Posterous
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Tumblr
  • Twitter
  • Yahoo! Bookmarks
  • Yahoo! Buzz
This entry was posted on Saturday, January 16th, 2010 at 11:23 pm and is filed under CSS Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Post a Comment