Can multiple different HTML elements have the same ID if they're different elements?

后端 未结 16 1888
无人及你
无人及你 2020-11-21 05:30

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
16条回答
  •  梦毁少年i
    2020-11-21 06:06

    SLaks answer is correct, but as an addendum note that the x/html specs specify that all ids must be unique within a (single) html document. Although it's not exactly what the op asked, there could be valid instances where the same id is attached to different entities across multiple pages.

    Example:

    (served to modern browsers) article#main-content {styled one way}
    (served to legacy) div#main-content {styled another way}

    Probably an antipattern though. Just leaving here as a devil's advocate point.

提交回复
热议问题