Tutorial :HTML: Display:none does this allow multiple ID-Attributes with same name (when “hidden”)?



Question:

according to the HTML Standards ID-Attributes of any HTML Tag in a webpage have to be unique in the document!?

Does this rule also apply to HTML Tags that have been "disabled/hidden" by using: display:none?

Example:

<html>  <body>  <div id="one"></div>  <div id="one" style="display:none;"></div>  </body>  </hmtl>  

Is this valid HTML or not. So the question is do "display:none"= hidden Elements also "count/matter" in regard to the rule only having unique ID-Attributes in a single webpage?

Thanks Jan


Solution:1

It's not a valid markup. Validation does not take into account CSS styles applied to DOM elements.

There still has to be only one element per ID, regardless of whether it's visible or not, whether it is behind the others in the z-stack, whether it is positioned outside the viewport etc.


Solution:2

Ids MUST be unique... to do otherwise is flirting with disaster!


Solution:3

It doesn't matter if they are hidden or not they are still in the document. Even if they are created dynamically they shouldn't have the same ID. "it's not a valid markup" - Developer Art


Solution:4

Of course, as others have mentioned, it's not valid markup. You also have to think about DOM selection here. document.getElementById() selects one element with a particular ID, regardless of its visibility/display within the document.


Solution:5

Styling an element (even applying display:none) you do not remove the element from the document tree.


Solution:6

So the question is do "display:none"= hidden Elements also "count/matter" in regard to the rule

Yes. CSS doesn't affect whether a node is considered to be part of the document.

You can only have multiple Elements with the same ID when they're not both contained within the hierarchy of the same Document (or DocumentFragment), eg. when you've just used JavaScript to createElement a new node but not appendChild​ed it into the document yet.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »