Cursor · 07 of 22

Spotlight

Light reveals content in dark

HTML · CSS · JavaScript · No dependencies · MIT

HTML

<div class="spotlight-area" id="spotlightArea">
  <div class="spotlight-layer" id="spotlightLayer"></div>
  <div class="spotlight-content">Spotlight</div>
</div>

CSS

.spotlight-area {
  position: relative;
  height: 240px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
  overflow: hidden;
  cursor: none;
  font-family: sans-serif;
}

.spotlight-content {
  z-index: 1;
  color: #222;
  font-weight: 700;
  font-size: 1.5rem;
}

/* Dark layer with a transparent hole that follows the cursor */
.spotlight-layer {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: radial-gradient(circle 60px at var(--x, 50%) var(--y, 50%),
    transparent 0%, #000 100%);
  pointer-events: none;
  z-index: 2;
}

JavaScript

var area = document.getElementById('spotlightArea');
var layer = document.getElementById('spotlightLayer');

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