HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<header class="hero center-bottom">
<div class="hero-image hero-image-background">
<picture>
<source srcset="https://codepen.n2.studio/image-background.webp" type="image/webp">
<img src="https://codepen.n2.studio/image-background.jpg" alt="background" width="1920" height="1280">
</picture>
</div>
<div class="hero-image hero-image-foreground">
<picture>
<source srcset="https://codepen.n2.studio/image-foreground.webp" type="image/webp">
<img src="https://codepen.n2.studio/image-foreground.jpg" alt="foreground" width="1920" height="1280">
</picture>
</div>
<div class="hero-darken"></div>
</header>
<!-- only for demonstration -->
<main>
<h1>Super Simple Parallax Fullscreen Hero Header with Object-Fit</h1>
<a class="anchor" id="what"></a>
<h2>What does “parallax” mean?</h2>
<p>The in some corners of the web still popular “parallax effect” means that an object, in most cases an image, moves in a different speed than the default scroll speed. In other words, yes, it usually makes no sense whatsoever. Just eye candy.</p>
<p>And to make this example even more eye candier I use two congruent images, split in background and foreground, with different speeds.</p>
<a class="anchor" id="why"></a>
<h2>Why is this solution different?</h2>
<p>Most parallax solutions work with background images, which is totally fine. When they gave us <em>background-size: cover</em> back in the day it became even more flexible to make the idea work in a responsive environment.</p>
<p>However, you probably want an <em><img></em> in your html so that you can use <em><source> / <srcset></em>, have an alt text and easily control everything with the CMS of your choice.</p>
<p>The concept is super simple, as the title promised. We absolute position a <em>div</em> in a (in this case fullscreen) container and only manipulate the <em>top</em> and <em>bottom</em> values in relation to the window scroll position, so it's really just a few lines of (jQuery) code.</p>
<p>Since <em>object-fit: cover</em> lets the image fill the <em>div</em> and the overflow is hidden you won't see gaps.</p>
<a class="anchor" id="align"></a>
<h2>Aligning the image(s)</h2>
<p>This solution is of course naturally responsive. But depending on the image you may want to determine the focus, especially for small screens. Similar to <em>background-position</em> we thankfully have <em>object-position</em>. So I've added classes for that.</p>
<a class="anchor" id="extend"></a>
<h2>Extending the idea</h2>
<p>There are of course countless possibilities. You could easily add more layers – maybe for text. I've already included the option to darken the image(s). But keep an eye on the performance. Use <em><srcset></em> to serve the perfect image sizes for all screens.</p>
<p>Btw, the container doesn't need to be fullscreen, it just needs some height and the images will align.</p>
<p>Thanks for reading this far. Cool photo by <a href="https://unsplash.com/@worldsbetweenlines" target="_blank" rel="noopener noreferrer">Patrick Hendry</a> via <a href="https://unsplash.com/photos/jd0hS7Vhn_A" target="_blank" rel="noopener noreferrer">Unsplash</a>.</p>
</main>
<nav>
<ul>
<li><a href="#what">What</a></li>
<li><a href="#why">Why</a></li>
<li><a href="#align">Align</a></li>
<li><a href="#extend">Extend</a></li>
</ul>
</nav>
/* fonts */
@import url('https://fonts.googleapis.com/css2?family=Sofia+Sans+Condensed:wght@400;700&family=Sofia+Sans:ital@0;1&display=swap');
/* resets */
*,
*:before,
*:after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* styles – required */
html,
body {
height: 100%;
scroll-behavior: smooth;
}
.hero {
position: relative;
height: 100vh;
overflow: hidden;
}
.hero .hero-image {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.hero .hero-image img {
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
}
.hero.left-top .hero-image img {
object-position: left top;
}
.hero.center-top .hero-image img {
object-position: center top;
}
.hero.right-top .hero-image img {
object-position: right top;
}
.hero.left-bottom .hero-image img {
object-position: left bottom;
}
.hero.center-bottom .hero-image img {
object-position: center bottom;
}
.hero.right-bottom .hero-image img {
object-position: right bottom;
}
.hero .hero-darken {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: rgba(0,0,0,.1);
}
/* styles – only for demonstration */
html {
font-family: 'Sofia Sans', sans-serif;
font-size: calc(1rem + 0.2 * ((100vw - 20rem) / 50));
line-height: 1.4rem;
}
h1,
h2 {
font-family: 'Sofia Sans Condensed', sans-serif;
font-weight: 700;
line-height: 2.4rem;
margin-bottom: 1.4rem;
}
p {
margin-bottom: 1.4rem;
}
a {
color: black;
}
a.anchor {
display: block;
position: relative;
top: -100px;
visibility: hidden;
}
em {
opacity: .5;
}
main {
width: 90%;
max-width: 720px;
margin: 3em auto 6em auto;
}
nav {
position: fixed;
z-index: 2;
top: 30px;
left: 50%;
transform: translate(-50%, 0);
width: 80%;
max-width: 540px;
background-color: black;
border-radius: 12px;
text-align: center;
}
nav ul {
list-style: none;
}
nav ul li {
display: inline;
}
nav ul li a {
display: inline-block;
font-family: 'Sofia Sans Condensed', sans-serif;
font-weight: 400;
letter-spacing: .1em;
padding: .5em;
color: white;
text-decoration: none;
text-transform: uppercase;
}
@media screen and (min-width: 30em) {
nav ul li a {
padding: .5em 1em;
letter-spacing: .15em;
opacity: .75;
}
nav ul li a:hover {
opacity: 1;
}
}
/* scroll animation */
.hero .hero-darken:before,
.hero .hero-darken:after {
content: '';
position: absolute;
right: 0;
left: 0;
margin-left: auto;
margin-right: auto;
text-align: center;
}
.hero .hero-darken:before {
top: 104px;
width: 30px;
height: 50px;
border: 2px solid black;
border-radius: 12px;
}
.hero .hero-darken:after {
top: 112px;
width: 2px;
height: 12px;
background-color: black;
border-radius: 1px;
animation: scroll 2s infinite linear;
}
@keyframes scroll {
from {
top: 112px;
}
to {
top: 122px;
}
}
$(document).ready(function() {
$(window).scroll(function() {
var scrollPosWin = $(window).scrollTop();
var factorBackground = scrollPosWin * .4;
var factorForeground = scrollPosWin * .2;
$('.hero-image-background').css({'top': factorBackground, 'bottom': -factorBackground});
$('.hero-image-foreground').css({'top': factorForeground, 'bottom': -factorForeground});
});
});
Also see: Tab Triggers