No, unfortunately, this is not possible.
I suggest you read these links: MDN | A display or item from an inventory that evokes the notion of accessibility and visibility of the DOM. In fact, if you put the item in " display: none ", it will not be visible and will exit the DOM accessibility tree . However, there are some methods that allow you to make the DOM element completely invisible and no longer be considered existing in the DOM.
Then there are obviously methods you should know
First, if you want to completely mask your parent div without worrying about its dimensions , I suggest the code below. The clip property is deprecated, but without it, it will still hide our element.
console.log(document.getElementById('child').scrollHeight);
.hidden { border: 0; clip: rect(0 0 0 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; white-space: nowrap; width: 1px; z-index: -1; }
<div class="hidden parent" href="#main"> I am ignored by the DOM <div id="child">I am child</div> </div> <span style="color:red">I am the N°1</span>
Otherwise, if you want to keep the dimensions of your parent div , you can pull it out of the DOM stream.
console.log(document.getElementById('child').scrollHeight);
.hidden { height: auto; width: auto; visibility: hidden; position: absolute; z-index: -1; }
<div class="hidden parent" href="#main"> I am ignored by the DOM <div id="child">I am child</div> </div> <span style="color:red">I am the N°1</span>
I hope this answer is helpful.
source share