Class or ID?
A lot of people come with the question “When do I use Class and when do I use IDs… what is the difference?”. This article talks about the differences and IDs can be used with other applications other than CSS.
Both classes and IDs can be referenced with CSS using
# respectively. However, IDs can only be used once per page,
whereas classes can be used as many times as required.
“So why not always use classes?” — well because IDs are unique to that page they can be addressed in scripts, forms and CSS without the possibility of them being found anywhere else on the page.
IDs also have precedence over classes so classes can not be used to override id styled elements.
Application of IDs
The best example of the use of IDs uniquely used on a page, is within forms. An
id on a input element is used to connect it to the
<label for="id_name">Label Title</label> <input type="text" name="parse_variable" id="id_name">
Because ids can only be used once per page, it is obvious that the label belongs only to the one
Ids make sense for elements that will only appear once on a page, such as navigation, footer and content containers.
Because of this identification they can be referenced with interal links using the
<a href="http://example.com/page.html#footer">To the footer</a> <div id="footer"></div>