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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<!--
gandul. Accessible Image Lazy Loading / more info:
- Documentation : https://moro.es/projects/gandul-accessible-image-lazy-loading/
- GitHub repo : https://github.com/alterebro/accessible-image-lazy-load
- NPM Module : https://www.npmjs.com/package/accessible-image-lazy-load
-->
<header>
<p><img src="https://cdn.jsdelivr.net/gh/alterebro/accessible-image-lazy-load/gandul.png" width="128" alt="gandul!" /></p>
<h1><strong>gandul</strong> : <a href="https://moro.es/projects/gandul-accessible-image-lazy-loading/" title="Accessible Image Lazy Loading" target="_blank">accessible image lazy loading</a></h1>
<p>
<a href="https://github.com/alterebro/accessible-image-lazy-load/blob/master/LICENSE"><img src="https://img.shields.io/github/license/alterebro/accessible-image-lazy-load.svg" alt="MIT license"></a>
<a href="https://www.npmjs.com/package/accessible-image-lazy-load"><img src="https://img.shields.io/npm/v/accessible-image-lazy-load.svg" alt="NPM Version"></a>
<a href="https://github.com/alterebro/accessible-image-lazy-load/blob/master/dist/gandul.min.js"><img src="https://img.shields.io/github/size/alterebro/accessible-image-lazy-load/dist/gandul.min.js.svg" alt="File Size"></a>
<a href="https://twitter.com/alterebro"><img src="https://img.shields.io/twitter/follow/alterebro.svg" alt="Twitter"></a>
</p>
<hr />
</header>
<main>
<p>
Gandul is a lightweight non-dependant javascript module that adds a different approach on lazy loading focusing on accessibility. It works by taking a common anchor hyperlink as data source and transforms it into an image element. This way you don't lose the reference to the image and when no JavaScript, the image will still be accessible. Further info about gandul:
</p>
<ul>
<li>Documentation : <strong><a href="https://moro.es/projects/gandul-accessible-image-lazy-loading/" title="Accessible lazy loading">moro.es/projects/gandul-accessible-image-lazy-loading/</a></strong></li>
<li>GitHub repo : <strong><a href="https://github.com/alterebro/accessible-image-lazy-load">github.com/alterebro/accessible-image-lazy-load</a></strong></li>
</ul>
<hr />
<h3>Width attribute on the images</h3>
<section>
<div>
<p><a href="https://placekitten.com/640/480" data-width="320" class="gandul">Nice kitten</a></p>
</div>
<pre><code class="html"><a href="https://placekitten.com/640/480" data-width="320" class="gandul">Nice kitten</a></p></code></pre>
</section>
<section>
<div>
<p><a href="https://placekitten.com/960/720" data-width="320" class="gandul">Nice kitten</a></p>
</div>
<pre><code class="html"><p><a href="https://placekitten.com/960/720" data-width="320" class="gandul">Nice kitten</a></p></code></pre>
</section>
<section>
<div>
<p>
<a href="https://cdn.jsdelivr.net/gh/alterebro/accessible-image-lazy-load/gandul.png" data-width="128" class="gandul">gandul!</a>
<a href="https://cdn.jsdelivr.net/gh/alterebro/accessible-image-lazy-load/gandul.png" data-width="128" class="gandul">gandul!</a>
<a href="https://cdn.jsdelivr.net/gh/alterebro/accessible-image-lazy-load/gandul.png" data-width="128" class="gandul">gandul!</a>
</p>
</div>
<pre><code class="html"><p>
<a href="../gandul.png" data-width="128" class="gandul">gandul!</a>
<a href="../gandul.png" data-width="128" class="gandul">gandul!</a>
<a href="../gandul.png" data-width="128" class="gandul">gandul!</a>
</p></code></pre>
</section>
<hr />
<h3>Responsive image (sizes & srcset)</h3>
<section>
<div>
<p>
<a href="https://placekitten.com/800/400"
data-srcset="https://placekitten.com/320/160 320w,
https://placekitten.com/480/240 480w,
https://placekitten.com/800/400 800w"
data-sizes="(max-width: 320px) 280px,
(max-width: 480px) 440px,
800px"
class="gandul">Nice kitten</a>
</p>
</div>
<pre><code class="html"><p>
<a href="https://placekitten.com/800/400"
data-srcset="https://placekitten.com/320/160 320w,
https://placekitten.com/480/240 480w,
https://placekitten.com/800/400 800w"
data-sizes="(max-width: 320px) 280px,
(max-width: 480px) 440px,
800px"
class="gandul">Nice kitten</a>
</p></code></pre>
</section>
<hr />
<h3>Images with <code>id</code> and <code>class</code> attributes</h3>
<section>
<div>
<p><a href="https://placekitten.com/600/400" id="cat" class="nice kitten gandul">Nice kitten</a></p>
</div>
<pre><code class="html"><p><a href="https://placekitten.com/600/400" id="cat" class="nice kitten gandul">Nice kitten</a></p></code></pre>
</section>
<section>
<div>
<p><a href="https://placekitten.com/500/500" id="kitty" class="square kitty gandul">Another Nice kitten</a></p>
</div>
<pre><code class="html"><p>
<a href="https://placekitten.com/500/500" id="kitty" class="square kitty gandul">Another Nice kitten</a>
</p></code></pre>
</section>
<hr />
<h3>Element with all options and callback action when image load finishes :</h3>
<section>
<div>
<p><a href="https://placekitten.com/1200/600" class="gandul-hyperlink">Cat image</a></p>
</div>
<pre><code class="html"><p><a href="https://placekitten.com/1200/600" class="gandul-hyperlink">Cat image</a></p>
<!-- gandul -->
<script>
gandul('a.gandul-hyperlink', { threshold: .5 }, function(el) {
el.style.border = 'solid #fff 20px';
console.log(el);
});
</script></code></pre>
</section>
<hr />
<h3>Apply to different elements than <code><a></code> hyperlinks</h3>
<section>
<div>
<p>
<a href="https://placekitten.com/1200/420">
<picture class="gandul" data-href="https://placekitten.com/1200/420">This is a test</picture>
</a>
</p>
</div>
<pre><code class="html"><p>
<a href="https://placekitten.com/1200/420">
<picture class="gandul" data-href="https://placekitten.com/1200/420">This is a test</picture>
</a>
</p></code></pre>
</section>
<section>
<div>
<span class="gandul" data-href="https://placekitten.com/1200/400">This is another test</picture>
</div>
<pre><code class="html"><span class="gandul" data-href="https://placekitten.com/1200/400">This is another test</picture></code></pre>
</section>
<hr />
</main>
<footer>
<div><a href="https://moro.es/projects/gandul-accessible-image-lazy-loading/" title="Accessible Image Lazy Loading"><strong>gandul</strong> 😴 : accessible-image-lazy-load</a> <em>( <a href="https://github.com/alterebro/accessible-image-lazy-load#readme">GitHub Repo</a> )</em></div>
</footer>
html, body, h1, h2, h3, p, pre {
margin: 0;
padding: 0;
}
html {
box-sizing: border-box;
font-size: 62.5%;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Droid Sans", "Helvetica Neue", Arial, sans-serif;
font-size: 1.6em;
line-height: 1.7;
width: 90%;
max-width: 92rem;
margin: 4rem auto;
}
*, *:before, *:after {
box-sizing: inherit;
background-repeat: no-repeat;
}
a {
color: #069;
text-decoration: none;
&:hover {
color: #000;
text-decoration: underline;
}
}
header {
text-align: center;
padding: 2rem 0;
h1 {
font-size: 32px;
font-weight: 400;
letter-spacing: -1px;
line-height: 1.4;
}
p { margin: 1rem 0 2rem; }
}
h3 {
font-size: 24px;
margin: 0 0 2rem;
line-height: 1.4;
}
hr {
box-sizing: content-box;
height: 0;
overflow: visible;
background: none;
border: none;
border-top: dotted #333 1px;
margin: 4rem 0;
}
section {
margin: 0 0 2rem;
> div {
min-height: 24rem;
box-shadow: inset 0 0 0 1px #eee;
background: #eee;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
> pre {
width: 100%;
font-family: "Andale Mono", "Monaco", "Lucida Console", "Menlo", "Consolas", "Roboto Mono", "Liberation Mono", monospace;
}
}
footer {
text-align: center;
font-size: 1.3rem;
}
// ---------
img.gandul, img.gandul-hyperlink {
display: inline-block;
border-radius: 3px;
max-width: 100%;
opacity: 0;
transition: all 1s ease-in-out;
}
img.gandul-active { opacity: 1; }
// ---------
/* highlight.js */
.hljs {
display: block;
overflow-x: auto;
padding: 2rem;
font-family: "Andale Mono", "Monaco", "Lucida Console", "Menlo", "Consolas", "Roboto Mono", "Liberation Mono", monospace;
font-size: 1.1rem;
line-height: 1.5;
box-shadow: inset 0 0 0 1px #eee;
}
.hljs, .hljs-subst { color: #555 }
.hljs-attr { color: #06f; font-weight: bold; }
.hljs-comment { color: #999; font-style: italic; }
.hljs-keyword, .hljs-attribute, .hljs-selector-tag, .hljs-meta-keyword, .hljs-doctag, .hljs-name { color: #069; font-weight: bold }
.hljs-title, .hljs-section { color: #880000; font-weight: bold }
.hljs-type, .hljs-string, .hljs-number, .hljs-selector-id, .hljs-selector-class, .hljs-quote, .hljs-template-tag, .hljs-deletion { color: #09f; }
.hljs-regexp, .hljs-symbol, .hljs-variable, .hljs-template-variable, .hljs-link, .hljs-selector-attr, .hljs-selector-pseudo { color: #BC6060 }
.hljs-literal { color: #78A960 }
.hljs-built_in, .hljs-bullet, .hljs-code, .hljs-addition { color: #90f; font-weight: bold; }
.hljs-meta { color: #1f7199 }
.hljs-meta-string { color: #4d99bf }
.hljs-emphasis { font-style: italic }
.hljs-strong { font-weight: bold }
/*
- Documentation : https://moro.es/projects/gandul-accessible-image-lazy-loading/
- GitHub repo : https://github.com/alterebro/accessible-image-lazy-load
- NPM Module : https://www.npmjs.com/package/accessible-image-lazy-load
*/
// highlight code
hljs.initHighlightingOnLoad();
// gandul stuff
let a = gandul();
let b = gandul('a.gandul-hyperlink', { threshold: .5 }, function(el) {
el.style.border = 'solid #fff 20px';
console.log(el);
});
let c = gandul('a.non-existant');
let d = gandul('picture.gandul');
let e = gandul('span.gandul');
console.log(a);
console.log(b);
console.log(c); // It should return false
console.log(d);
console.log(e);
Also see: Tab Triggers