Typography · 21 of 33

Spotlight Reveal

Hover to reveal text like a flashlight

HTML · CSS · JavaScript · No dependencies · MIT

HTML

<div class="spotlight-wrapper" id="spotlightWrapper">
  <h2 class="spotlight-text">Hidden in Shadows</h2>
  <div class="spotlight-mask" id="spotlightMask"></div>
</div>

CSS

.spotlight-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 160px;
  background: #000;
  overflow: hidden;
}

.spotlight-text {
  font-size: 3.5rem;
  font-weight: 600;
  color: #222;
  z-index: 1;
  margin: 0;
}

/* Bright copy sits behind, revealed through the mask hole */
.spotlight-wrapper::after {
  content: "Hidden in Shadows";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  z-index: 0;
  font-size: 3.5rem;
  font-weight: 600;
}

.spotlight-mask {
  position: absolute;
  inset: 0;
  background: radial-gradient(circle 80px at var(--x, 50%) var(--y, 50%),
    transparent 0%, #000 100%);
  pointer-events: none;
  z-index: 2;
  mix-blend-mode: hard-light;
}

JavaScript

var wrapper = document.getElementById('spotlightWrapper');
var mask = document.getElementById('spotlightMask');

wrapper.addEventListener('mousemove', function (e) {
  var rect = wrapper.getBoundingClientRect();
  mask.style.setProperty('--x', (e.clientX - rect.left) + 'px');
  mask.style.setProperty('--y', (e.clientY - rect.top) + 'px');
});