1. Computing

Discuss in my forum

When Should You Change an ID to a Class Attribute?

By , About.com Guide

Question: When Should You Change an ID to a Class Attribute?

I was asked in the HTML forum:

If I define an ID, then later discover I want to use it a second time in my document, am I supposed to change it to a CLASS first?
Answer:

Yes. To have valid HTML, you should only have one instance of an ID on any Web page.

While most browsers will display multiple duplicate IDs with the correct style, you could run into problems with future browsers or with porting the page to a new document in the future.

Think of the id tag as a way of identifying special parts of a Web page. You can use an ID tag for more than just styles too. Most modern browsers will treat an ID as an anchor, so you can type http://YOUR PAGE URL/index.html#id and the page will load at that ID marker. But that won't work if there are multiple IDs with the same name (or it will, but it will only go to the first instance of that ID and none of the others). For example, try going to: http://webdesign.about.com/#blog - it's not a named anchor, #blog is just an ID on my home page.

The most common time I have changed IDs to classes is when I realize that I want more of that same element or widget on one page. But even then I might leave them as ids and just make the new one a new name (perhaps with a number on the end) so that I can style it differently in the future. Such as:

#pullquote, #pullquote2 { ... }

©2013 About.com. All rights reserved.