Using the same ID twice?
The text editor lets me use the exact same ID tag more than once and when I give specific attributes to that particular ID tag in the CSS file, all the elements with that one ID change. Doesn’t this make the functionality of ID exactly the same as the functionality of class?
Answer 536d27d8282ae358b60002b7
7 comments
Ahhh I see, mainly for javascript purposes. Thanks man
Thanks for asking this (and answering this). I was thinking the same thing myself.
Had the same question.Thanks for the answer
This is actually an error in the curriculum. You should not use “id” more than once. Usually, nothing will really break, but it’s bad practice.
@Lucien anybody posting a link to CSS Tricks gets an automatic upvote from me :)
how do u add id in h1 for example
@mavs: The same way you do for anything else: <h1 id="id">Text</h1>
Answer 536d6e77282ae3a786000309
For good measure, here is the CSS2 page for Selectors on w3.org.
Document languages may contain attributes that are declared to be of type ID. What makes attributes of type ID special is that no two such attributes can have the same value; whatever the document language, an ID attribute can be used to uniquely identify its element. … The ID attribute of a document language allows authors to assign an identifier to one element instance in the document tree. …
Answer 54dd206086f5523c38000087
Using same ID more than once is a bad practice although it might work in many situations.
If you code the same in VB and try, it will throw a warning and automatically change the ID’s so that they are unique.
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons