User Agents

Web browsers and everyday web tech, explained

Parallax Scrolling with Pure CSS in Chrome

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to create a parallax scrolling effect using only CSS, no JavaScript, and it works perfectly in Chrome. Parallax scrolling makes background elements move at a different speed than the foreground, adding depth and visual interest to a webpage. With modern CSS, you can achieve this using the background-attachment: fixed property or the more advanced transform: translateZ() technique with perspective. We'll cover both methods, with step-by-step instructions, common pitfalls, and troubleshooting tips.

What You'll Need

  • A text editor (like VS Code, Sublime Text, or Notepad)
  • Chrome browser (latest version recommended)
  • Basic knowledge of HTML and CSS (selectors, properties, values)
  • Two or more images or colored sections to test with

Step-by-Step

Method 1: Using background-attachment: fixed (Simpler)

  1. Create an HTML file with several sections. For example, a <div> with a class like .parallax and some content sections.
  2. Add CSS for the parallax element. Set a fixed height (e.g., height: 400px;), a background image, and background-attachment: fixed;. Also set background-size: cover; and background-position: center; for proper scaling.
  3. Test in Chrome. Scroll the page. The background image should stay fixed while the content scrolls over it, creating a parallax effect.

Method 2: Using transform and perspective (More Advanced)

  1. Set up a container with perspective. Wrap all your content in a <div> with perspective: 1px; and overflow-y: auto; (or overflow-x: hidden;) to allow scrolling.
  2. Create layers with different translateZ values. For each layer (e.g., background, foreground), apply transform: translateZ(-1px) scale(2); for background layers and transform: translateZ(0); for normal content. The scale factor compensates for the zoom effect caused by perspective.
  3. Ensure proper stacking. Use position: relative; or absolute; on layers to control their z-order. The background layer should be behind content.
  4. Test and adjust. Scroll in Chrome. The background should move slower than the foreground. Adjust the translateZ values (e.g., -2px for slower) and scale accordingly (e.g., scale(3)).

Common Mistakes

  • Not setting a height on the parallax element. Without a height, the background might not show or the effect won't be visible.
  • Using background-attachment: fixed on mobile. Chrome on mobile (Android) does not support this property well; it often behaves as scroll. Use the transform method for mobile compatibility.
  • Forgetting to set overflow on the perspective container. If the container doesn't have overflow-y: auto or scroll, the perspective won't apply correctly, and the effect may break.
  • Incorrect scale factor. When using translateZ, the scale factor must be 1 + (translateZ / perspective). For example, with perspective: 1px and translateZ(-1px), scale should be 2. If you forget this, the layer will appear zoomed in or out.
  • Not setting background-size: cover. The background image might be distorted or not fill the area.

Tips

  • Use multiple layers for a richer effect. For example, a sky layer at translateZ(-3px), a mountain layer at -2px, and a ground layer at -1px.
  • Keep the effect subtle. Too much speed difference can cause motion sickness or distract from content.
  • Test performance. Large images and many layers can slow down scrolling. Optimize images and use will-change: transform to hint the browser.
  • Use CSS variables to easily adjust the translateZ values and scale factors across your stylesheet.

Troubleshooting

  • Effect not working at all: Check that you're using Chrome (the effect may not work in other browsers without prefixes). For the background-attachment method, ensure the element has a height and the background image is set correctly. For the transform method, verify the container has perspective and overflow set.
  • Background scrolls normally on mobile: This is expected for background-attachment: fixed. Switch to the transform method if mobile support is needed.
  • Layers are zoomed in or out: Recalculate the scale factor. For perspective: 1px, if translateZ is -2px, scale should be 3 (1 + 2/1).
  • Content overlaps incorrectly: Ensure you set position: relative or z-index on layers to control stacking. Background layers should have a lower z-index.
  • Janky or laggy scrolling: Reduce the number of layers, compress images, and avoid using large background images. Also consider using transform: translate3d(0,0,0) to force GPU acceleration.

Frequently Asked Questions

Does parallax scrolling work in all browsers?

No. The background-attachment: fixed method works in Chrome, Firefox, and Safari on desktop, but not on mobile browsers. The transform method works in modern browsers but may require vendor prefixes for older versions.

Can I use parallax with text content?

Yes, you can apply the transform method to any element, including text. However, be careful with readability; moving text at a different speed can make it hard to read.

Is pure CSS parallax better than JavaScript?

Pure CSS is simpler and faster because it uses the browser's compositor, but it's less flexible. JavaScript libraries allow more control and can animate elements on scroll, but they may impact performance.

Why does my background-attachment: fixed not work on iOS?

iOS Safari and Chrome on iOS ignore background-attachment: fixed due to performance issues. Use the transform method for mobile compatibility.

How do I make the effect smoother?

Use smaller images, limit the number of layers, and apply will-change: transform to the moving layers. Also, avoid using background-size: cover on large images as it can be heavy.

Can I combine parallax with scroll-triggered animations?

Yes, you can use CSS animations or transitions with the transform method, but for scroll-triggered animations, you'll need JavaScript or the Intersection Observer API.