[CSS] id vs class
Author
Discussion

zoidberg

Original Poster:

8,891 posts

313 months

Tuesday 26th June 2007
quotequote all
Anyone got a simple explaination of when to use id and when to use class in css?



.Flyer

434 posts

281 months

Tuesday 26th June 2007
quotequote all
Classes are used when you need to assign a style to any number of elements. IDs are supposed to be used only once in a document. So;

.highlight {color: yellow;}

<h1 class="highlight">HighlightedHead!</h1>
<p class="highlight">some highlighted text</p>

---

{#content_main {width: 70%;}}

<div id="content_main">
main content stuff
</div>

(ignore outer set of braces in content_main id above; PH formatting)

Edited by .Flyer on Tuesday 26th June 20:11

zoidberg

Original Poster:

8,891 posts

313 months

Tuesday 26th June 2007
quotequote all
Cool thanks. So there are no "hard and fast" rules, jsut up to the author to use best practice?

cottonfoo

6,053 posts

241 months

Tuesday 26th June 2007
quotequote all
No, there are hard and fast rules, but most browsers attempt to Do What You Mean, which is very wrong in my opinion.

http://www.w3schools.com/css/default.asp is good, links to the specs and things from there too.

Spyke

175 posts

269 months

Wednesday 27th June 2007
quotequote all
How about:
"Many elements can share the same class, but the element ids must be unique"

Edit: Dammit, that's already been said!

--Mike

Edited by Spyke on Wednesday 27th June 15:08