CSSmediumCSS

Parallax Scrolling Effect

Create depth with parallax scrolling backgrounds using pure CSS.

01

The problem

You want backgrounds to scroll at different speeds for a 3D depth effect.

02

The solution

CSS
.parallax-container {
  height: 100vh;
  overflow-x: hidden;
  overflow-y: auto;
  perspective: 1px;
}

.parallax-layer {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.parallax-back {
  transform: translateZ(-2px) scale(3);
  z-index: -1;
}

.parallax-base {
  transform: translateZ(0);
}

/* Simple background parallax */
.parallax-bg {
  background-image: url('background.jpg');
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 100vh;
}

/* Parallax section */
.parallax-section {
  position: relative;
  height: 60vh;
  overflow: hidden;
}

.parallax-section::before {
  content: '';
  position: absolute;
  top: -20%;
  left: 0;
  width: 100%;
  height: 140%;
  background: url('image.jpg') center/cover;
  transform: translateY(var(--scroll-offset, 0));
  z-index: -1;
}

03

See it live

Isolated previewSandboxed
04

Put it to work

Example
<div class="parallax-container">
  <section class="parallax-layer parallax-back">
    <img src="stars.png" alt="">
  </section>
  <section class="parallax-layer parallax-base">
    <h1>Welcome</h1>
  </section>
</div>

<!-- Simple fixed background -->
<section class="parallax-bg">
  <h2>Content overlays the parallax image</h2>
</section>

Worth knowing

background-attachment: fixed has limited mobile support. Use JavaScript for better mobile parallax.