Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="sticky-parallax-header">PARALLAX COVER TO STICKY HEADER</div>

<div id="content">
	<div class="warning">
		<p>⚠️ Your browser does not support CSS Scroll-Linked Animations, so this demo won't work. If you're feeling adventurous use Chrome 89 with “Experimental Web Platform Features” enabled. Alternatively you can <a href="https://twitter.com/bramus/status/1349488687512432640" target="_top">check a recording of this feature in action</a>.</p>
	</div>
	
	<div class="info">
		<p>ℹ️ This demo usess JavaScript. In the near future we won't need JS to achieve this effect, and can go with <a href="https://codepen.io/bramus/full/ExgOPRw" target="_top">a Pure CSS Solution thanks to @scroll-timeline</a>.</p>
		<p>🐛 This demo still has one bug though: As the included <code>ScrollTimeline</code> polyfill can't properly handle <code>vh</code> units, we need to set up our <code>ScrollTimeline</code> using pixels. Upon resizing we need to recalc and attach the new timeline to the animation. It's this last part that does not play nice with the polyfill. Natively it works fine though.</p>
	</div>
	<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mattis turpis eu neque tincidunt lacinia. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam auctor ultrices tortor et volutpat. Donec eu semper orci, eu fermentum orci. Vivamus aliquam lobortis egestas. Proin vel mauris vitae sem auctor cursus. Aenean sit amet odio at magna pharetra luctus nec eget urna. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Praesent in leo at tellus ultricies laoreet.</p>

	<p>Duis nec nisi a eros dictum euismod nec non enim. Donec augue neque, aliquam id finibus a, volutpat ut ligula. Nam feugiat odio elit, ut molestie mi tempus vel. Etiam vitae velit eget massa scelerisque hendrerit cursus vel justo. Sed eget euismod lectus. Morbi tincidunt diam ac diam tempor molestie. Mauris eget lectus leo. Etiam id porta turpis.</p>

	<p>Aenean ac pellentesque ante. In hac habitasse platea dictumst. Nullam efficitur, dolor ac pharetra iaculis, sem elit maximus ex, et malesuada est ipsum tempor nisi. Phasellus auctor et turpis ultrices fermentum. Mauris ac orci sapien. Quisque auctor, mi eu vestibulum dictum, nisl felis pulvinar dui, non dictum justo tellus a urna. Morbi suscipit et odio a feugiat. Sed dui nisi, iaculis a lacus posuere, dictum laoreet felis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>

	<p>Nam pulvinar congue condimentum. Pellentesque fringilla ante sed massa tincidunt feugiat. Nulla bibendum est eros, non malesuada dolor vehicula in. Morbi eu cursus arcu. Phasellus ultricies ornare lorem, non interdum orci condimentum eu. Donec condimentum condimentum ligula ut placerat. Vestibulum id neque condimentum, molestie nunc vel, suscipit turpis. Quisque orci velit, mattis et nibh non, tempor finibus erat. Nullam tristique, tortor non commodo cursus, magna turpis rhoncus leo, quis porta quam ante sed orci. Nunc vehicula imperdiet leo in vulputate. Quisque quis dolor orci.</p>

	<p>Curabitur ultrices fringilla libero sit amet porta. Nulla rhoncus venenatis euismod. Praesent non interdum sem. Nunc in nibh tincidunt, faucibus sem at, finibus turpis. Nullam vel odio blandit, tincidunt urna et, luctus lectus. Vivamus porta sem vitae turpis interdum porta eu id ligula. Fusce non nisi fringilla, malesuada metus ut, imperdiet ligula. Nunc quis lorem nibh. Duis pharetra magna vestibulum metus convallis, id mollis ligula vulputate. Sed purus est, consectetur non suscipit sit amet, hendrerit at dolor. Maecenas feugiat, quam vel ullamcorper feugiat, sapien ipsum viverra justo, id cursus metus nisi quis enim.</p>
	<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mattis turpis eu neque tincidunt lacinia. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam auctor ultrices tortor et volutpat. Donec eu semper orci, eu fermentum orci. Vivamus aliquam lobortis egestas. Proin vel mauris vitae sem auctor cursus. Aenean sit amet odio at magna pharetra luctus nec eget urna. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Praesent in leo at tellus ultricies laoreet.</p>

	<p>Duis nec nisi a eros dictum euismod nec non enim. Donec augue neque, aliquam id finibus a, volutpat ut ligula. Nam feugiat odio elit, ut molestie mi tempus vel. Etiam vitae velit eget massa scelerisque hendrerit cursus vel justo. Sed eget euismod lectus. Morbi tincidunt diam ac diam tempor molestie. Mauris eget lectus leo. Etiam id porta turpis.</p>

	<p>Aenean ac pellentesque ante. In hac habitasse platea dictumst. Nullam efficitur, dolor ac pharetra iaculis, sem elit maximus ex, et malesuada est ipsum tempor nisi. Phasellus auctor et turpis ultrices fermentum. Mauris ac orci sapien. Quisque auctor, mi eu vestibulum dictum, nisl felis pulvinar dui, non dictum justo tellus a urna. Morbi suscipit et odio a feugiat. Sed dui nisi, iaculis a lacus posuere, dictum laoreet felis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>

	<p>Nam pulvinar congue condimentum. Pellentesque fringilla ante sed massa tincidunt feugiat. Nulla bibendum est eros, non malesuada dolor vehicula in. Morbi eu cursus arcu. Phasellus ultricies ornare lorem, non interdum orci condimentum eu. Donec condimentum condimentum ligula ut placerat. Vestibulum id neque condimentum, molestie nunc vel, suscipit turpis. Quisque orci velit, mattis et nibh non, tempor finibus erat. Nullam tristique, tortor non commodo cursus, magna turpis rhoncus leo, quis porta quam ante sed orci. Nunc vehicula imperdiet leo in vulputate. Quisque quis dolor orci.</p>

	<p>Curabitur ultrices fringilla libero sit amet porta. Nulla rhoncus venenatis euismod. Praesent non interdum sem. Nunc in nibh tincidunt, faucibus sem at, finibus turpis. Nullam vel odio blandit, tincidunt urna et, luctus lectus. Vivamus porta sem vitae turpis interdum porta eu id ligula. Fusce non nisi fringilla, malesuada metus ut, imperdiet ligula. Nunc quis lorem nibh. Duis pharetra magna vestibulum metus convallis, id mollis ligula vulputate. Sed purus est, consectetur non suscipit sit amet, hendrerit at dolor. Maecenas feugiat, quam vel ullamcorper feugiat, sapien ipsum viverra justo, id cursus metus nisi quis enim.</p>
	<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mattis turpis eu neque tincidunt lacinia. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam auctor ultrices tortor et volutpat. Donec eu semper orci, eu fermentum orci. Vivamus aliquam lobortis egestas. Proin vel mauris vitae sem auctor cursus. Aenean sit amet odio at magna pharetra luctus nec eget urna. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Praesent in leo at tellus ultricies laoreet.</p>

	<p>Duis nec nisi a eros dictum euismod nec non enim. Donec augue neque, aliquam id finibus a, volutpat ut ligula. Nam feugiat odio elit, ut molestie mi tempus vel. Etiam vitae velit eget massa scelerisque hendrerit cursus vel justo. Sed eget euismod lectus. Morbi tincidunt diam ac diam tempor molestie. Mauris eget lectus leo. Etiam id porta turpis.</p>

	<p>Aenean ac pellentesque ante. In hac habitasse platea dictumst. Nullam efficitur, dolor ac pharetra iaculis, sem elit maximus ex, et malesuada est ipsum tempor nisi. Phasellus auctor et turpis ultrices fermentum. Mauris ac orci sapien. Quisque auctor, mi eu vestibulum dictum, nisl felis pulvinar dui, non dictum justo tellus a urna. Morbi suscipit et odio a feugiat. Sed dui nisi, iaculis a lacus posuere, dictum laoreet felis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>

	<p>Nam pulvinar congue condimentum. Pellentesque fringilla ante sed massa tincidunt feugiat. Nulla bibendum est eros, non malesuada dolor vehicula in. Morbi eu cursus arcu. Phasellus ultricies ornare lorem, non interdum orci condimentum eu. Donec condimentum condimentum ligula ut placerat. Vestibulum id neque condimentum, molestie nunc vel, suscipit turpis. Quisque orci velit, mattis et nibh non, tempor finibus erat. Nullam tristique, tortor non commodo cursus, magna turpis rhoncus leo, quis porta quam ante sed orci. Nunc vehicula imperdiet leo in vulputate. Quisque quis dolor orci.</p>

	<p>Curabitur ultrices fringilla libero sit amet porta. Nulla rhoncus venenatis euismod. Praesent non interdum sem. Nunc in nibh tincidunt, faucibus sem at, finibus turpis. Nullam vel odio blandit, tincidunt urna et, luctus lectus. Vivamus porta sem vitae turpis interdum porta eu id ligula. Fusce non nisi fringilla, malesuada metus ut, imperdiet ligula. Nunc quis lorem nibh. Duis pharetra magna vestibulum metus convallis, id mollis ligula vulputate. Sed purus est, consectetur non suscipit sit amet, hendrerit at dolor. Maecenas feugiat, quam vel ullamcorper feugiat, sapien ipsum viverra justo, id cursus metus nisi quis enim.</p>
	<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mattis turpis eu neque tincidunt lacinia. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam auctor ultrices tortor et volutpat. Donec eu semper orci, eu fermentum orci. Vivamus aliquam lobortis egestas. Proin vel mauris vitae sem auctor cursus. Aenean sit amet odio at magna pharetra luctus nec eget urna. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Praesent in leo at tellus ultricies laoreet.</p>

	<p>Duis nec nisi a eros dictum euismod nec non enim. Donec augue neque, aliquam id finibus a, volutpat ut ligula. Nam feugiat odio elit, ut molestie mi tempus vel. Etiam vitae velit eget massa scelerisque hendrerit cursus vel justo. Sed eget euismod lectus. Morbi tincidunt diam ac diam tempor molestie. Mauris eget lectus leo. Etiam id porta turpis.</p>

	<p>Aenean ac pellentesque ante. In hac habitasse platea dictumst. Nullam efficitur, dolor ac pharetra iaculis, sem elit maximus ex, et malesuada est ipsum tempor nisi. Phasellus auctor et turpis ultrices fermentum. Mauris ac orci sapien. Quisque auctor, mi eu vestibulum dictum, nisl felis pulvinar dui, non dictum justo tellus a urna. Morbi suscipit et odio a feugiat. Sed dui nisi, iaculis a lacus posuere, dictum laoreet felis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>

	<p>Nam pulvinar congue condimentum. Pellentesque fringilla ante sed massa tincidunt feugiat. Nulla bibendum est eros, non malesuada dolor vehicula in. Morbi eu cursus arcu. Phasellus ultricies ornare lorem, non interdum orci condimentum eu. Donec condimentum condimentum ligula ut placerat. Vestibulum id neque condimentum, molestie nunc vel, suscipit turpis. Quisque orci velit, mattis et nibh non, tempor finibus erat. Nullam tristique, tortor non commodo cursus, magna turpis rhoncus leo, quis porta quam ante sed orci. Nunc vehicula imperdiet leo in vulputate. Quisque quis dolor orci.</p>

	<p>Curabitur ultrices fringilla libero sit amet porta. Nulla rhoncus venenatis euismod. Praesent non interdum sem. Nunc in nibh tincidunt, faucibus sem at, finibus turpis. Nullam vel odio blandit, tincidunt urna et, luctus lectus. Vivamus porta sem vitae turpis interdum porta eu id ligula. Fusce non nisi fringilla, malesuada metus ut, imperdiet ligula. Nunc quis lorem nibh. Duis pharetra magna vestibulum metus convallis, id mollis ligula vulputate. Sed purus est, consectetur non suscipit sit amet, hendrerit at dolor. Maecenas feugiat, quam vel ullamcorper feugiat, sapien ipsum viverra justo, id cursus metus nisi quis enim.</p>
</div>
              
            
!

CSS

              
                #sticky-parallax-header {
	color: #fff;
	height: 100vh;
	width: 100vw;
	background-image: url("https://source.unsplash.com/tZpmdFfU5gQ");
	background-size: cover;
	background-position: 50% 50%;
	background-blend-mode: soft-light;

	display: grid;
	place-items: center;
	text-align: center;
	font-size: calc(4vw + 1em);
}

#content {
	padding-top: 1em;
	margin: 0 auto 0;
	width: 80vw;
	max-width: 60em;
}

html,
body {
	margin: 0;
	padding: 0;
	background: #fff;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
}

.warning, .info {
	padding: 1em;
	border: 1px solid #ccc;
	background: rgba(255 255 205 / 0.8);
	display: none;
}

.info {
	display: block;
	background: transparent;
}

.warning p,
.info p {
	margin: 0;
	padding: 0;
}

.info p + p {
	margin-top: 1.2em;
}

* {
	box-sizing: border-box;
}

              
            
!

JS

              
                /*
  JavaScript version of the CSS-Only https://codepen.io/bramus/full/ExgOPRw
*/
import polyfill from "https://cdn.skypack.dev/css-typed-om";

// Stuff that needs to be done first
const bootstrap = async () => {
		
	// Load CSSKeywordValue Polyfill
	// @note: Script already imported (see import above), only needs to be attached to window
	if (typeof CSSKeywordValue == "undefined") {
		polyfill(window);
	}
	
	// Load ScrollTimeline
	// @note: This polyfill contains an IIFE, so we only need to load it.
	if (typeof ScrollTimeline == "undefined") {
		const polyfill = () => new Promise((resolve, reject) => {
			const script = document.createElement("script");
			script.async = true;
			script.src =
				"https://flackr.github.io/scroll-timeline/dist/scroll-timeline.js";
			script.onload = resolve;
			script.onerror = reject;
			document.head.appendChild(script);
		});
		
		await polyfill().catch(e => false);
	}

	// Make sure we have everything
	if (
		typeof Animation == "undefined" ||
		typeof CSSKeywordValue == "undefined" ||
		typeof KeyframeEffect == "undefined" ||
		typeof ScrollTimeline == "undefined"
	) {
		return false;
	}
	
	return true;
}

// Our Actual Demo Code
const hookAnimations = () => {

	// Some elements we'll need
	const $header = document.querySelector("#sticky-parallax-header");
	const $content = document.querySelector("#content");
	
	// Fixate the header
	// @note: We use `position: fixed` instead of `position: sticky` here because
	// the latter glitches — https://bugs.chromium.org/p/chromium/issues/detail?id=1167497
	$header.style.position = 'fixed';
	$header.style.top = 0;
	
	// As we use `position: fixed` above, we also have to make sure the content
	// is not underneath the header. With `position: sticky` this would be of no issue.
	$content.style.marginTop = '100vh';
	
	// @note: the loaded ScrollTimeline Polyfill can't handle vh units, so we have to
	// calculate it ourselves …
	let _100vh = document.documentElement.clientHeight;
	
	// Animation
	const stickyParallaxHeaderMoveAndSize = new KeyframeEffect(
		$header,
		{
			backgroundPosition: ["50% 0", "50% 100%"], 
			backgroundColor: ['transparent', '#0b1584'],
			height: [`${_100vh}px`, `${_100vh / 10}px`],
			//      ⬆ This should be `['100vh', '10vh']`
			//         but the ScrollTimeline Polyfill doesn't
            //         understand it
			fontSize: ['calc(4vw + 1em)', 'calc(1vw + 1em)'],
		},
		{ duration: 1, fill: "both", easing: 'linear' }
	);
	
	// Timeline
	const timeline = new ScrollTimeline({
		source: document.body,
		timeRange: 1,
		fill: "both",
		scrollOffsets: [
			new CSSUnitValue(_100vh * 0.9, 'px'),
			//               ⬆ This should be `new CSSUnitValue(100, 'vh')`
			//                  but the ScrollTimeline Polyfill doesn't
			//                  understand it
		],
	});
	
	// @note: the loaded ScrollTimeline Polyfill can't handle
	// vh units, so we need to dispatch hookAnimations on every resize
	// so that it uses a correct px value for 100vh.
	// If we notice an animation was already created, we simply overwrite
	// the already attached effect.
	if ($header.getAnimations()[0]) {
		$header.getAnimations()[0].effect = stickyParallaxHeaderMoveAndSize;
				
		// 🐛 The polyfill can't handle the snippet below
		// and will break on it.
		// It says .timeline is of the type DocumentTimeline
		// instead of a ScrollTimeline
		try {
			$header.getAnimations()[0].timeline = timeline;
		} catch (e) {}

		return;
	}

	// Attach the defined KeyFrames onto the element, with a ScrollTimeline
	new Animation(
		stickyParallaxHeaderMoveAndSize,
		timeline
	).play();

}

// Go!
const go = async () => {
	const bootstrapped = await bootstrap();
	
	if (!bootstrapped) {
		document.querySelector(".warning").style.display = "block";
		return;
	}
	
	// @note: the loaded ScrollTimeline Polyfill can't handle
	// vh units, so we need to dispatch hookAnimations on every resize
	// so that it uses a correct px value for 100vh.
	window.addEventListener('resize', (e) => {
		hookAnimations();
	});

	hookAnimations();
}

go();
              
            
!
999px

Console