|
|
|
|
|
by rafski
4631 days ago
|
|
"No, the primary reason is that IDs prevent reuse." The fact that they cannot be reused in a single page is what makes IDs so useful. There are benefits of IDs others mentioned, but for me properly used IDs make CSS so much clearer. An ID in CSS code is a flag saying: "custom-crafted for this part of the page only, not to be reused", or "non-modular code". Unlike a class, an ID informs me with 100% certainty that if modified, it will not affect other instances on the page, because there are none. It's hard to overestimate the value of such information, especially when working on older or someone else's code. |
|
This isn't enforced by browsers. They will happily render all elements with the same ID and apply the same styles to all of them.
This kind of thing really isn't useful. If you accidentally put the logo twice on the page, it will be clearly visible. It virtually never happens and when it does it isn't hard to track down.
This "just once" property of IDs really doesn't add anything useful.
>Unlike a class, an ID informs me with 100% certainty that if modified, it will not affect other instances on the page, because there are none.
If I modify one of my building blocks, I know that every instance of it will now look like this.
Consistency is awesome.