Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ 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

Auto Save

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

              
                <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>&lt;img&gt;</em> in your html so that you can use <em>&lt;source&gt; / &lt;srcset&gt;</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>&lt;srcset&gt;</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>

              
            
!

CSS

              
                /* 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;
  }

}

              
            
!

JS

              
                $(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});
  });

});

              
            
!
999px

Console