Nice demo on CodePen by Ryan Mulligan, featuring some sections with a sticky parallax background image:
See the Pen CSS Sticky Parallax Sections by Ryan Mulligan (@hexagoncircle) on CodePen.
I expected to find the the translateZ() + scale() method to create the parallax layers in there, but turns out Ryan took another approach:
- Scale down each enclosing section, using a
scaleYtransform with a number lower than 1, e.g.scaleY(0.9) - Scale up each contained direct child of those sections using an inverse
scaleYtransform, e.g.scaleY(1.11111111)
As the second scaling (1.11111) undoes the first scaling (0.9), the content is rendered at the original scale of 1 again (e.g. 0.9 * 1.11111 = 1).
:root {
--speed: 0.1;
}
.section {
transform-origin: center top;
transform: scaleY(calc(1 - var(--speed)));
}
.section > * {
transform-origin: center top;
transform: scaleY(calc(1 / (1 - var(--speed))));
}
Note that I’ve renamed Ryan’s --scale parameter to --speed here, as that’s what it affects.
Related: Also check out this pure CSS Parallax replica of the Firewatch website header.
