1. Computing

Discuss in my forum

What is the Point of Having an ID and a Class Attribute When They Act the Same?

By , About.com Guide

Question: What is the Point of Having an ID and a Class Attribute When They Act the Same?

I was asked in the HTML forum:

I haven't been able to detect any difference in the behavior of ID vs. CLASS, so what's the point of having both?
Answer:

Ultimately, if you don't care about having well-written and valid HTML, there is no point. But here are some of the reasons I use both:

  • IDs are more specific than classes
  • IDs act as anchors for links
  • IDs provide structure to my code

When I first started writing CSS I didn't use many IDs, but as I grew into it, I started understanding why I might want to use them and found them more valuable. They aren't a critical portion of CSS, and if you choose to leave them out at first, that's fine. But they do serve a purpose, and eventually you will probably come across a situation where they would help you.

©2013 About.com. All rights reserved.