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.
<main>
<article>
<h1>progressive-image.js demonstration</h1>
<p><code>progressive-image.js</code> implements a lazy-loading progressive image similar to those seen on <a href="https://code.facebook.com/posts/991252547593574/the-technology-behind-preview-photos/">Facebook</a> and <a href="https://jmperezperez.com/medium-image-progressive-loading-placeholder/">Medium</a>. A very small, blurred image is replaced with the full-resolution equivalent when the element is scrolled into view.</p>
<p>Please use the code as you wish - <a href="https://twitter.com/craigbuckler">tweet me @craigbuckler</a> if you find it useful. <a href="https://github.com/craigbuckler/progressive-image.js">Download from GitHub...</a></p>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/nature5.jpg" data-srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/nature5.jpg 800w, https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/nature5big.jpg 1600w" class="primary progressive replace">
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gNzAK/9sAQwAKBwcIBwYKCAgICwoKCw4YEA4NDQ4dFRYRGCMfJSQiHyIhJis3LyYpNCkhIjBBMTQ5Oz4+PiUuRElDPEg3PT47/9sAQwEKCwsODQ4cEBAcOygiKDs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7/8AAEQgABQAUAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8A5yC3e2S3gM7OEQHdgA57VTuLAPqUoaUk7yM7R2BPSiivS5VN8stjmk2ldGVM3lyshG7bxk0UUV4skk2jdN2P/9k=" class="preview" alt="palm trees" />
</a>
<p>Benefits:</p>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/nature2.jpg" class="boxout2 progressive replace">
<img src="http://lorempixel.com/20/15/nature/2/" class="preview" alt="sunset" />
</a>
<ul>
<li>fast loading, high performance, images loaded on view</li>
<li>supports any image type (JPEG photographs are most appropriate)</li>
<li>supports responsive images (<code>srcset</code> and <code>sizes</code> attributes)</li>
<li>small: 1,007 bytes of JavaScript, 463 bytes of CSS (minified)</li>
<li>any CSS reveal effect can be applied</li>
<li>no external dependencies - works with any framework</li>
<li>works in all modern browsers (IE10+)</li>
<li>progressively-enhanced to work in older browsers</li>
<li>easy to use</li>
</ul>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/nature3.jpg" class="boxout1 progressive replace">
<img src="http://lorempixel.com/20/15/nature/3/" class="preview" alt="tide" />
</a>
<p>The preview image can be very small - perhaps 20px in width and saved with high JPEG compression. This typically results in an image less than 500 bytes in size. It be added to the page directly or inlined as a data URI using base-64 encoding.</p>
<p>The large image can be any size but should match the preview image's aspect ratio. For example, 20x15 can be scaled to 200x150, 400x300 or 1600x1200.</p>
<h2>Basic example</h2>
<p>The page must load the CSS and JavaScript. It can be placed anywhere on the page but, typically, the CSS is loaded in the <code><head></code> and the JS is loaded just before the closing <code></body></code> tag:</p>
<pre><code><link rel="stylesheet" href="css/progressive-image.min.css">
<script src="js/progressive-image.min.js"></script></code></pre>
<p>The simplest progressive image is a link to the full-size graphic around the preview. Note the <code>progressive</code> and <code>replace</code> classes on the link and the <code>preview</code> class on the image.</p>
<pre><code><a href="full.jpg" class="progressive replace">
<img src="tiny.jpg" class="preview" alt="image" />
</a></code></pre>
<p>If image loading or JavaScript fails, a blurred version of the preview image can be clicked to view the full image:</p>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/people7.jpg" class="full progressive" style="cursor:pointer;">
<img src="http://lorempixel.com/20/10/people/7/" class="preview" alt="eyes" />
</a>
<p>When JavaScript runs successfully, the large image is revealed when the preview is scrolled into view. CSS3 effects are used to fade and zoom the image:</p>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/people7.jpg" class="full progressive replace">
<img src="http://lorempixel.com/20/10/people/7/" class="preview" alt="eyes" />
</a>
<p>After replacement, the link click is disabled and resulting HTML will be:</p>
<pre><code><a href="full.jpg" class="progressive">
<img src="full.jpg" alt="image" />
</a></code></pre>
<h2>Responsive image example</h2>
<p><a href="https://www.sitepoint.com/how-to-build-responsive-images-with-srcset/">Responsive images</a> of differing sizes and resolutions can be defined in the link using the <code>data-srcset</code> and <code>data-sizes</code> attributes which map to the standard <code>srcset</code> and <code>sizes</code> attributes, e.g.</p>
<pre><code><a href="small.jpg"
data-srcset="small.jpg 800w, large.jpg 1200w"
data-sizes="100vw"
class="progressive replace">
<img src="preview.jpg" class="preview" alt="image" />
</a></code></pre>
<p><em>(carriage returns added to aid legibility)</em></p>
<p>On replacement, the image code is transformed to:</p>
<pre><code><img src="small.jpg"
srcset="small.jpg 800w, large.jpg 1200w"
sizes="100vw"
alt="image" /></code></pre>
<p>Modern browsers will load large.jpg on screens of 800px width or greater.</p>
<h2>Usage notes</h2>
<ol>
<li>The preview and full-size images must have identical aspect ratios, e.g. 20x10 and 1200x600.</li>
<li>Only vertical scrolling is checked. All images in the horizontal plane will be loaded.</li>
<li>Progressive images dynamically added to the page using JavaScript will only be replaced when a scroll or resize event occurs.</li>
<li>You may be able to improve actual or perceived performance using <a href="https://css-tricks.com/data-uris/">data URIs to inline images</a> or <a href="http://daverupert.com/2015/12/intrinsic-placeholders-with-picture/">intrinsic placeholders</a>.</li>
<li>Firefox can struggle when replacing large images and show a noticeable flicker.</li>
</ol>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/city5.jpg" data-srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/city5.jpg 500w, https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/city5big.jpg 1000w, https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/city5biggest.jpg 1500w" class="primary progressive replace">
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBkZWZhdWx0IHF1YWxpdHkK/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgABAAUAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8Amh8Sag2pQxl1KurZGD2GR396r32qXy+aFu5hhBjDnjJaiircnd6maSscfd+I9XtZ/KjvpNu1TlsMScepoooppslrU//Z" class="preview" alt="city" />
</a>
</article>
</main>
/* main styles */
*, *:before, *:after {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
font-size: 100%;
color: #444;
background-color: #fff;
overflow-x: hidden;
overflow-y: scroll;
}
article {
max-width: 40em;
padding: 0 4em;
margin: 0 auto;
}
h1, h2 {
font-weight: normal;
margin-top: 1em;
}
p, a {
margin-top: 1em;
}
h2 + p {
margin-top: 0;
}
ul, ol {
margin: 1em 0 0 2em;
}
li {
margin-top: 0.5em;
}
pre {
white-space: pre;
padding: 2px 6px;
margin-top: 1em;
background-color: #ddd;
border-radius: 3px;
overflow: auto;
}
code {
font-size: 1.1em;
}
img {
max-width: 100%;
}
a.primary {
width: 100vw;
left: 50%;
margin-left: -50vw;
}
a.full {
clear: both;
width: 100%;
box-shadow: 0 3px 4px #000;
}
a.boxout1, a.boxout2 {
clear: both;
float: left;
width: 60%;
margin: 1em 1em 0 -4em;
box-shadow: 0 3px 4px #000;
}
a.boxout2 {
float: right;
margin: 1em -4em 0 1em;
}
/* progressive image CSS */
a.progressive {
position: relative;
display: block;
overflow: hidden;
outline: none;
}
a.progressive:not(.replace) {
cursor: default;
}
a.progressive img {
display: block;
width: 100%;
max-width: none;
height: auto;
border: 0 none;
}
a.progressive img.preview {
filter: blur(2vw);
transform: scale(1.05);
}
a.progressive img.reveal {
position: absolute;
left: 0;
top: 0;
will-change: transform, opacity;
animation: reveal 1s ease-out;
}
@keyframes reveal {
0% { transform: scale(1.05); opacity: 0; }
100% { transform: scale(1); opacity: 1; }
}
// progressive-image.js
if (window.addEventListener && window.requestAnimationFrame && document.getElementsByClassName) window.addEventListener('load', function() {
// start
var pItem = document.getElementsByClassName('progressive replace'), timer;
window.addEventListener('scroll', scroller, false);
window.addEventListener('resize', scroller, false);
inView();
// throttled scroll/resize
function scroller(e) {
timer = timer || setTimeout(function() {
timer = null;
requestAnimationFrame(inView);
}, 300);
}
// image in view?
function inView() {
var wT = window.pageYOffset, wB = wT + window.innerHeight, cRect, pT, pB, p = 0;
while (p < pItem.length) {
cRect = pItem[p].getBoundingClientRect();
pT = wT + cRect.top;
pB = pT + cRect.height;
if (wT < pB && wB > pT) {
loadFullImage(pItem[p]);
pItem[p].classList.remove('replace');
}
else p++;
}
}
// replace with full image
function loadFullImage(item) {
if (!item || !item.href) return;
// load image
var img = new Image();
if (item.dataset) {
img.srcset = item.dataset.srcset || '';
img.sizes = item.dataset.sizes || '';
}
img.src = item.href;
img.className = 'reveal';
if (img.complete) addImg();
else img.onload = addImg;
// replace image
function addImg() {
// disable click
item.addEventListener('click', function(e) { e.preventDefault(); }, false);
// add full image
item.appendChild(img).addEventListener('animationend', function(e) {
// remove preview image
var pImg = item.querySelector && item.querySelector('img.preview');
if (pImg) {
e.target.alt = pImg.alt || '';
item.removeChild(pImg);
e.target.classList.remove('reveal');
}
});
}
}
}, false);
Also see: Tab Triggers