Scroll · 16 of 34

Underline Grow

An underline draws itself across the text when the heading scrolls into view. A background-image gradient whose background-size animates from zero to full width.

HTML · CSS · JavaScript · No dependencies · MIT

HTML

<section class="underline-grow-section">
  <h2 class="underline-text">Underline Grow</h2>
</section>

CSS

.underline-grow-section {
  min-height: 35vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.underline-text {
  font-size: 2.5rem;
  color: #f0f0f5;
  /* The underline is a gradient background that grows from 0% to 100% wide */
  background: linear-gradient(#7c5cff, #7c5cff) no-repeat bottom;
  background-size: 0% 2px;
  padding-bottom: 4px;
  transition: background-size 0.8s cubic-bezier(0.16, 1, 0.3, 1);
}

.underline-grow-section.in-view .underline-text {
  background-size: 100% 2px;
}

JavaScript

var section = document.querySelector('.underline-grow-section');

new IntersectionObserver(function (entries) {
  entries.forEach(function (entry) {
    entry.target.classList.toggle('in-view', entry.isIntersecting);
  });
}, { threshold: 0.25 }).observe(section);