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.
<h1>How to lazy load images in a performant way</h1>
<p>
There is also an example image how to use it with an background image.
<br>
Take a look at the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Browser_compatibility">Browser support table.</a>
and use <a href="https://www.npmjs.com/package/intersection-observer#browser-support">Polyfill</a> for better support. This one here does work properly in Safari, thanks to the <a href="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver">Polyfill Script</a> I added to the source. You will see that the HTML is bloated up quite a bit, this is because I added some noscript fallbacks for no-js browsing people.</p>
<noscript>
<!--
Hide the lazy load images if we do not have JS
-->
<style>
.image__list-lazy-image {
display: none;
}
</style>
</noscript>
<div class="image-container">
<ul class="image__list">
<li class="image__list-item">
<noscript>
<img class="image__list-image" src="http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796300/pexels-photo-559768.jpg" alt="blue lake with surrounding mountains">
</noscript>
<img class="image__list-image image__list-lazy-image" data-src="http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796300/pexels-photo-559768.jpg" alt="blue lake with surrounding mountains">
</li>
<li class="image__list-item">
<noscript>
<img class="image__list-image" src="http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796305/pexels-photo-104336.jpg" alt="street in the middle of a forrest">
</noscript>
<img class="image__list-image image__list-lazy-image" data-src="http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796305/pexels-photo-104336.jpg" alt="street in the middle of a forrest">
</li>
<li class="image__list-item">
<noscript>
<img class="image__list-image" src="http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796376/les-anderson-175603.jpg" alt="two elder people opening togehter a christmas present">
</noscript>
<img class="image__list-image image__list-lazy-image" data-src="http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796376/les-anderson-175603.jpg" alt="two elder people opening togehter a christmas present">
</li>
<li class="image__list-item">
<noscript>
<img class="image__list-image" src="http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796381/pexels-photo-737108.jpg" alt="black and white clothes hanging on a clothes rail">
</noscript>
<img class="image__list-image image__list-lazy-image" data-src="http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796381/pexels-photo-737108.jpg" alt="black and white clothes hanging on a clothes rail">
</li>
<li class="image__list-item">
<noscript>
<img class="image__list-image" src="http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796475/tobias-van-schneider-122288.jpg" alt="book collection photographed from top">
</noscript>
<img class="image__list-image image__list-lazy-image" data-src="http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796475/tobias-van-schneider-122288.jpg" alt="book collection photographed from top">
</li>
<li class="image__list-item">
<noscript>
<img class="image__list-image" src="http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796300/pexels-photo-559768.jpg" alt="two elder people opening togehter a christmas present">
</noscript>
<img class="image__list-image image__list-lazy-image" data-src="http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796300/pexels-photo-559768.jpg" alt="brown meadow with a tower in the very back">
</li>
<li class="image__list-item">
<noscript>
<div class="image__list-image--bg" style="background-image:url('http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796300/pexels-photo-559768.jpg');" alt="two elder people opening togehter a christmas present"></div>
</noscript>
<div class="image__list-image--bg image__list-lazy-image" data-lazybackground="http://res.cloudinary.com/dsteinel/image/upload/c_scale,w_450/v1523796300/pexels-photo-559768.jpg">
</div>
</li>
</ul>
</div>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
max-width: 450px;
margin: 0 auto;
padding: 2em 0;
font-size: 18px;
line-height: 1.5;
}
h1 {
display: block;
margin: 0 auto 2rem;
line-height: 1.1;
}
ul {
padding: 0;
margin: 0;
list-style-type: none;
}
a {
color: #A55; /* HAHA! */
}
p {
margin-bottom: 3rem;
}
.image__list-image {
opacity: 1;
min-height: 100%;
}
.image__list-lazy-image {
opacity: 0;
}
.image__list-item {
width: 100%;
height: 250px;
overflow: hidden;
margin-bottom: 40px;
}
.image__list-image--bg {
background-size: cover;
background-repeat: no-repeat;
height: 100%;
}
const options = {
rootMargin: '0px',
threshold: 0.1
}
const allTheLazyImages = document.querySelectorAll('.image__list-lazy-image');
let observer;
// Does your browser support InersectionObserver?
if('IntersectionObserver' in window) {
observer = new IntersectionObserver(lazyLoader, options);
// select all our image which we want to have lazy loaded
allTheLazyImages.forEach(image => {
// put them in the observer
observer.observe(image);
});
} else {
allTheLazyImages.forEach(image => {
lazyLoadImage(image); // if it is not supported, load all
});
}
function lazyLoader (entries) {
// loop through all images
entries.forEach(entry => {
// does the viewport hit the current image
if (entry.intersectionRatio > 0) {
// yes! load the image
lazyLoadImage(entry.target);
}
});
}
function lazyLoadImage(observedImage) {
/**
* We hit an observed image!
* First, remove the lazy loading class. This will show the image
* Then remove the observer
*/
observedImage.classList.remove('image__list-lazy-image');
// If we have an background image, replace the source with the data-lazyBackground attribute
if(observedImage.dataset.lazybackground) {
observedImage.style.backgroundImage = `url(${observedImage.dataset.lazybackground})`;
}
// does it have a data attribute with image-src?
if (observedImage.getAttribute('data-src')) {
// yes! So lets make the image source equal to the data image source and render it!
observedImage.src = observedImage.dataset.src;
if('IntersectionObserver' in window) {
// now that the image has bee renderes, we don't need to observe it anymore
observer.unobserve(observedImage);
}
}
}
Also see: Tab Triggers