【发布时间】:2022-08-08 14:21:51
【问题描述】:
Can multiple HTML elements have the same ID if they're of different element types? Is a scenario like this valid? Eg:
div#foo
span#foo
a#foo
【问题讨论】:
-
While sometimes possible, it's never valid.
-
With all the above being said it is worth to note that it is likely to come across multiple same IDs in a document with user-agent-created content (think frameworks, mv*, react, polymer...). That's if anyone was wondering why a very professional looking XYZ site is full of suchbad practicecoding.
-
The comment from @PaulCreasey is a good way to answer this problematic question. The question title and body do not match; each of them are reasonable yes or no questions but with different correct answers - this could catch out people who aren't paying attention. There's a meta question about how to resolve question mismatches like this, no answers though as of yet: meta.stackoverflow.com/questions/256732
-
Hi @Tidorith! Thanks for commenting. I'm open to suggestion on changing either the title or the body if you have an idea. The original question was asked out of curiosity. Some codegen tool (I think it might've been some Microsoft UI library) was generating elements with identical IDs. I tried reading the spec and testing it out in browsers, but was left confused since browsers seemed to allow it, while the spec said no.
-
@Tidorith Edited the question body a bit. Hope it's better now!