- Jan 2, 2006
- 10,455
- 35
- 91
So you use classes if you plan on re-using that particular style somewhere else on the page.
You use IDs if you only plan on using that style once and only once on the page.
But there's nothing stopping classes from being used only once. So why wouldn't you just use classes all the time? Classes do everything IDs do, but they're also reusable. I don't get it. Suppose there's one element on the page and you style it with a class. In the future you might want to have another element on the page using the same style from that class. So that's great! The class is ready to be re-used. If you had started defining that one element with an ID, and in the future you added an element that you wanted that exact style on as well, you're SOL.
Is the only reason to use IDs because you need an accurate way to hook in Javascript or jQuery code and use it as a unique identifier for the hook?
Example:
I have a jQuery image slider box. The code used all IDs, which means I can only have one image slider box on a page. Now I want two on the page at the same time. But I can't. The jQuery (I guess, I don't read jQuery) hooks on that ID and not the class, so it'll only work once on any page?
You use IDs if you only plan on using that style once and only once on the page.
But there's nothing stopping classes from being used only once. So why wouldn't you just use classes all the time? Classes do everything IDs do, but they're also reusable. I don't get it. Suppose there's one element on the page and you style it with a class. In the future you might want to have another element on the page using the same style from that class. So that's great! The class is ready to be re-used. If you had started defining that one element with an ID, and in the future you added an element that you wanted that exact style on as well, you're SOL.
Is the only reason to use IDs because you need an accurate way to hook in Javascript or jQuery code and use it as a unique identifier for the hook?
Example:
I have a jQuery image slider box. The code used all IDs, which means I can only have one image slider box on a page. Now I want two on the page at the same time. But I can't. The jQuery (I guess, I don't read jQuery) hooks on that ID and not the class, so it'll only work once on any page?
Last edited: