StealThis .dev

Scroll Fade In

Smooth fade-in animation triggered by Intersection Observer as elements enter the viewport on scroll.

Open in Lab
cssjsintersection-observer
Targets: TS JS HTML React

Code

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: #0f172a;
  color: #f1f5f9;
  line-height: 1.6;
}

.hero {
  min-height: 60vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 2rem;
}

.hero h1 {
  font-size: clamp(2rem, 5vw, 4rem);
  font-weight: 700;
  margin-bottom: 1rem;
  background: linear-gradient(135deg, #38bdf8, #818cf8);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.content {
  max-width: 720px;
  margin: 0 auto;
  padding: 2rem;
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.card {
  background: #1e293b;
  border: 1px solid #334155;
  border-radius: 1rem;
  padding: 2rem;
}

.card h2 {
  font-size: 1.25rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
  color: #38bdf8;
}

/* --- Fade-in animation --- */
.fade-in-el {
  opacity: 0;
  transform: translateY(24px);
  transition:
    opacity 0.6s ease-out,
    transform 0.6s ease-out;
}

.fade-in-el.is-visible {
  opacity: 1;
  transform: translateY(0);
}

/* Respect reduced motion */
@media (prefers-reduced-motion: reduce) {
  .fade-in-el {
    opacity: 1;
    transform: none;
    transition: none;
  }
}

Scroll Fade In

A lightweight scroll-triggered fade-in animation using the native Intersection Observer API — no libraries required.

How it works

Elements start invisible (opacity: 0, slightly translated down) and fade in once they enter the viewport. The observer disconnects after the element is visible, keeping things performant.

When to use it

  • Hero sections with staggered content
  • Blog post cards loading as you scroll
  • Feature grids on landing pages

Tech used

  • IntersectionObserver — native browser API, zero dependencies
  • CSS transitions — smooth, GPU-accelerated

Accessibility

Respects prefers-reduced-motion by skipping the animation for users who prefer it.