Skip to content Skip to sidebar Skip to footer

When Will A Browser Parse Elements Which Are Hidden?

foo
I'm thinking about bloating my DOM a bit for some (lazy probably) reasons with content which is hidden. Now I'm wondering if this

Solution 1:

Browsers follow something called the Render Tree which is formed by combining the DOM and CSSOM trees. In short, the DOM is concerned about content, while CSSOM is focused on styles applied to the document. The resulting "Render Tree" contains only the visible elements required to render the page.

Elements that are not visible or hidden via CSS aka via display: none will not be included in the Render Tree, while all elements that affect the layout will be included. So, it's safe to assume that your example will not get rendered until it becomes visible or affects the layout of the document in some way.

Constructing the Render Tree

After the Render Tree is constructed, it goes through a layout and paint cycle. The layout cycle calculates the position of each element in the Render Tree, and then the paint cycle displays each element onto the screen.

For more information about the Render Tree, see Critical Rendering Path described on the Google Developers' web fundamentals resource.

Solution 2:

The JS adding the divs in the DOM will probably cause more overhead than the browser parsing it.

Hidden elements with display:none are no different than visible elements and do not affect performance positively or negatively because there will be no effect on the layout. On the other hand, if you use visiblity:hidden then the browser must reserve a "box" for it and this will be slower because it affects the layout.

Post a Comment for "When Will A Browser Parse Elements Which Are Hidden?"