Skip to content

FlashcardNot attempted

Is it in the viewport?

Recall it before you reveal

How do you check whether an element is visible in the viewport, and what is the better modern approach?

Space to reveal
Show answer & explanation

Manual check with getBoundingClientRect() (viewport-relative):

const r = el.getBoundingClientRect();
const visible = r.top < innerHeight && r.bottom > 0 && r.left < innerWidth && r.right > 0;

But calling that on every scroll forces layout. Prefer IntersectionObserver: asynchronous, no layout thrash, with thresholds and rootMargin. It's the basis of lazy loading, infinite scroll and impression tracking.