CSS, How to use type, class and id selectors

Selectors are one of the most important aspects of CSS because they are used to “select” elements on an HTML page so the can be styled.

Sample HTML

<body>
<div id=”content”>
<h1> Heading Here </h1>
<p> Lorem ipsum dolor sit amet. </p>
<p>Lorem ipsum dolor <a href=”#”>sit</a> amet.</p>
<div>

<div id=”nav”>
<ul>
<li><a href=”#”>Item 1</a></li>
<li><a href=”#”>Item 2</a></li>
<li><a href=”#”>Item 3</a></li>
</ul>
</div>

<div id=”footer”>
Lorem ipsum dolor <a href=”#”>sit</a> amet.
</div>
</body>

CSS Type Selectors

Type selectors will select any HTML element on a page that matches the selector. In the HTML shown above there are seven HTML elements that could be used as type selectors, includeing <body<, <div>, <h1>, <p>, <ul>, <li> and <a>. For example to select all the <li> elements on the page, the <li> selected is used.

li {
color: blue
}

CSS Class Selectors

Class selectors can be used to select any HTML element that has been given a class attribute. In the HTML sample shown above there are two HTML elements with class attributes <p> and < href =”#”>. To select all instances of the intro class, the .intro selector is used.

.intro {
font-weight: bold
}

You also can select specific instances of a class by combining type and class selectors. For example, you might want to select the <p> and the <a href=”#”> seperatly. This is done using p.intro and a.intro.

p.intro {
color: red;
}

a.intro {
color: blue;
}

ID Selectors

ID selectors are similar to class selectors. They can be used to select any HTML element that has an ID attribute. However, each ID can be used only once within a document, whereas classes can be used as often as needed. In this CSS, How To Tutorial IDs are used to identify unique parts of the document structure. such as the content, navigation and/or footer. In the HTML sample shown above, there are three IDs: <div id=”content”>, <div id=”nav”>, <div id=”footer”>. To select <div id=”nav”>, the #nav selector is used.

#nav {
color: blue;
}

Helpful Articles

Trying to Lose Weight, Go Out With Friend And Family And Not Offend

This may sound amazing, but one of the hardest things to do when you are losing weight is to get the help of friends and family...

Top 10 Weight Loss Myths

Have you sometimes felt confused about weight loss and nutrition guides that should help you take the right decisions about your health, yet for some reason they don’t? Here are some of the most frequent weight loss theories, and their rebuttals...

How Do I Get Out Of A Weight Loss Plateau, Problem And Solution No. 2

When you ask the question “how do I get our of a weight loss plateau?” there can be more than one answer, depending on what the reason for the problem is...

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BlinkList
  • blogmarks
  • email
  • Fark
  • LinkaGoGo
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati
  • Twitter
  • Propeller
  • Yahoo! Buzz

Tags: , ,

Leave a Reply