Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to CSS 1

Status
Not open for further replies.

tiamat2012

Programmer
Dec 12, 2004
144
US
Hey, I just did a few tutorials on CSS for the first time (though I've messed around with it here and there) and I was wondering when you would use an id selector instead of a class selector and vice versa?

They both seem to have the same functions.

Could anyone explain?
Thanks,
Kerry
 
An 'ID' is a unquie identifyer, but you can use class many-a-time.

For example, say i'm deisgning a site template, i would us an ID to govern the properties of my 'container' div, maybe set its width and position on the page.

Then i would use a class to set a particular item's style inside, maybe a paragraph or somthing like <p class="important"> and i would set all P's with that class to have bold red text.

In general I use ID's to layout the div's on my page, and then use classes to style the content.

hope this helps.

Rob
 
id selectors will apply the style to only the element with the id - use it when you want to apply styles to one specific thing

class selectors will apply the style to any element that has that class - use it when you want to apply a style to a couple different things on the page
Code:
<style type="text/css">

#foo {
   background-color:#cccccc;
}

.bar {
   color:#ff0000;
}

</style>

<div id="foo" class="bar">
   This will be red text on a grey background.  The id selector 
   applies only to this div, because ids must be unique
</div>
<div class="bar">
   This will be red text on a white background.
</div>

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top