Jump to content

CSS Question on ID and Class


dhermanus

Recommended Posts

Hi I would like to ask. :) Whats the different between ID and class?Here's a quote I've got from the web "The difference between an ID and a class is that an ID can be used to identify one element, whereas a class can be used to identify more than one."Is this true? Because I tried using ID on more than 1 element... and it works... on TWO elements.eg.

<html><head><style type="text/css">#top { background-color:red} </style></head><body><div > <h1 id="top">Chocolate curry</h1> <p id="top">This is my recipe for making curry purely with chocolate</p> <p>Mmm mm mmmmm</p> </div></body></html>
Thanks,Naruto
Link to comment
Share on other sites

To further aspnetguy's answer, many browsers will allow you to use the same ID for HTML elements but it's really bad practice.
Thanks, if it is bad practice I usually don't bother mentioning that they can do it...no need to learn this the wrong way :)
Link to comment
Share on other sites

Just a bit more...According to the W3C, an id must be unique and is used to identify an html element, while the class assigns the class name to the element, and multiple elements can share the class name. Honestly, the id shouldn't be thought of as a way to apply a style, but more to identify an element, which in turn, css can specify a style to be applied to...it's a subtle difference, which I may have made even more confusing with my wording, but to me seems to be an important differenc. The link below should help a bit morehttp://www.w3.org/TR/html4/struct/global.html#h-7.5.2

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...