Check if element is visible in DOM
To quickly determine an element's visibility, use the properties .offsetParent
and getBoundingClientRect()
. An element is visible if it has a non-null .offsetParent
and the dimensions of its bounding rectangle are greater than 0.
Executing the function:
Delving into display properties
The CSS properties display
and visibility
are key players. An element with its display
set to 'none'
is invisible regardless of its other properties. Likewise, visibility
set to 'hidden'
or opacity
set to 0 makes an element non-visible, regardless of any space it occupies.
Viewport matters: in or out?
Use el.getBoundingClientRect()
to first determine if an element is found within the viewport:
Is your element playing 'king of the hill'?
The element on top of the stacking context can be confirmed with a document.elementFromPoint
check:
Seeing through the eyes of parent nodes
Even an ancestor can influence an element's visibility. Walk up the tree with a traversal of parent nodes:
Performance & browser differences
Be aware that performance matters in your checks. Start with less expensive checks (offsetParent
), and proceed to more intensive checks such as getComputedStyle
only if necessary.
Are you a performance lover? So is offsetParent
When browsers start acting too unique
A reminder: Browser differences exist. Your visibility checks need to withstand multiple browser environments. Some browsers have peculiar behavior with offsetParent
, especially when dealing with CSS position: fixed
.
Diving deeper into visibility checks
With dynamic and async operations
Content visibility could change over time due to dynamic content changes. Configure event listeners or MutationObservers for asynchronous changes to DOM or style.
Trick or treat with CSS transitions
Visibility over time can be affected by transitions. Account for any transition delays or durations in your checks.
The anarchy of iframes
Cross-Document checks, respecting the same-origin policy, are required for iframe content.
Was this article helpful?