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.
<!-- https://css-tricks.com/the-layered-look/ -->
<section class="page-wrap">
<h1>The Layered Look - <span class="nobr">Faking "lowsrc"</span></h1>
<h2>Better Responsive Images Using Multiple Backgrounds</h2>
<button data-js="retina">Toggle Retina</button>
<figure>
<div class="crop-height bg-image bg-image-radiocity"><img height="1920" width="2880" class="scale transparent" src="https://s.cdpn.io/23379/photo-radio_city_480x320bw.jpg"></div>
<div class="crop-height bg-image bg-image-camping"><img height="1920" width="2880" class="scale transparent" src="https://s.cdpn.io/23379/photo-camping_480x320bw.jpg"></div>
<div class="crop-height bg-image bg-image-crayons"><img height="1920" width="2880" class="scale transparent" src="https://s.cdpn.io/23379/photo-crayons_480x320bw.jpg"></div>
<div class="crop-height bg-image bg-image-sedona"><img height="1024" width="640" class="scale transparent" src="https://s.cdpn.io/23379/photo-sedona_512x320.jpg"></div>
<figcaption>
Faking "lowsrc": "mobile-first" images all load, then get swapped for high-res. (Empty cache, and reload page to repeat.)
</figcaption>
</figure>
<button data-js="retina">Toggle Retina</button>
</section> <!-- end .page-wrap -->
.crop-height {
max-height: 108px;
height: 108px; /* lt ie8 */
height: auto;
overflow: hidden; }
/* base background-image class, specify image below */
.bg-image {
width: 100%;
/* prevent scaling past src width (or not) */
max-width: 1024px;
/* default background-position */
background-position: center center;
/* lt ie 8 */
-ms-background-position-x: center;
-ms-background-position-y: center;
/* scale proportionately */
background-size: cover;
/* IE8 workaround - https://louisremi.github.io/background-size-polyfill/ */ }
/* mobile first "lowsrc" - see CSS media queries below for more */
.bg-image-camping {
background-image: url(https://s.cdpn.io/23379/photo-camping_480x320bw.jpg);
background-position: center 27%; }
.bg-image-crayons {
background-image: url(https://s.cdpn.io/23379/photo-crayons_480x320bw.jpg);
background-position: center 54%; }
.bg-image-radiocity {
background-image:
url(https://s.cdpn.io/23379/photo-radio_city_480x320bw.jpg);
background-position: center 80%; }
.bg-image-sedona {
background-image: url(https://s.cdpn.io/23379/photo-sedona_512x320.jpg); /* b&w */
background-position: center 22%; }
/* if you want users to be able to easily save your transparent proxy img,
be sure to manually declare a height and width on the img element itself
to match the background-image src size (if the placeholder img is smaller),
otherwise it won't fill the width of the div so user can't always click on it */
img.scale, object.scale {
display: block; /* corrects small inline gap at bottom of containing div */
width: 100% /* corrects obscure Firefox bug */
max-width: 100%;
/* just in case, to force correct aspect ratio */
height: auto !important;
/*width: auto\9;
/* ie8+9 - use modernizr instead of this \9 hack */
/* lt ie8 */
-ms-interpolation-mode: bicubic;
/* force a minimum size if img src size is known */
/* min-height: 320px; /* max-height of .crop-height */
/* min-width: 480px; /* consistent with image ratio */
/* optionally center if img src is not as wide as div */
/* margin: 0 auto; */ }
.invisible {
visibility: hidden; }
.transparent {
/* trigger hasLayout for ie */
zoom: 1;
/* 0 value in filters retains layout */
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: alpha(opacity=0);
opacity: 0; }
/* ==================================================
MEDIA QUERIES
.hd class added by jQuery after initial page load
.multiplebgs class added by modernizr.js
===================================================== */
@media
/* momma bear, plus any-retina */
only screen and (min-width : 513px),
only screen and (-webkit-min-device-pixel-ratio: 1.5),
only screen and ( min--moz-device-pixel-ratio: 1.5),
only screen and ( -o-min-device-pixel-ratio: 3/2),
only screen and ( min-device-pixel-ratio: 1.5),
only screen and ( min-resolution: 144dpi),
only screen and ( min-resolution: 1.5dppx) {
/* no-multiplebgs - medium fallback
(if you have a mid-sized image, it's probably sufficient here -
it's unlikely someone with a retina display is on an older browser) */
.no-multiplebgs .bg-image-sedona.hd,
.no-js .bg-image-sedona {
background-image: url(https://s.cdpn.io/23379/photo-sedona_1024x640.jpg); } /* sepia */
.multiplebgs .bg-image-sedona.hd {
background-image:
url(https://s.cdpn.io/23379/photo-sedona_1024x640.jpg), /* mid-sized on top */
url(https://s.cdpn.io/23379/photo-sedona_512x320.jpg); } /* "lowsrc" on bottom */
/* no-multiplebgs high-res fallback */
.no-multiplebgs .bg-image-camping.hd,
.no-js .bg-image-camping {
background-image: url(https://s.cdpn.io/23379/photo-camping2_2880x1920.jpg); }
/* hi-res retina */
.multiplebgs .bg-image-camping.hd {
background-image:
url(https://s.cdpn.io/23379/photo-camping2_2880x1920.jpg), /* high-res on top */
url(https://s.cdpn.io/23379/photo-camping_480x320bw.jpg); } /* "lowsrc" on bottom */
/* no-multiplebgs high-res fallback */
.no-multiplebgs .bg-image-crayons.hd,
.no-js .bg-image-crayons {
background-image: url(https://s.cdpn.io/23379/photo-crayons2_2880x1920.jpg); }
/* hi-res retina */
.multiplebgs .bg-image-crayons.hd {
background-image:
url(https://s.cdpn.io/23379/photo-crayons2_2880x1920.jpg), /* high-res on top */
url(https://s.cdpn.io/23379/photo-crayons_480x320bw.jpg); } /* "lowsrc" on bottom */
/* no-multiplebgs high-res fallback */
.no-multiplebgs .bg-image-radiocity.hd,
.no-js .bg-image-radiocity {
background-image: url(https://s.cdpn.io/23379/photo-radio_city2_2880x1920.jpg); }
/* hi-res retina */
.multiplebgs .bg-image-radiocity.hd {
background-image:
url(https://s.cdpn.io/23379/photo-radio_city2_2880x1920.jpg), /* high-res on top */
url(https://s.cdpn.io/23379/photo-radio_city_480x320bw.jpg); } /* "lowsrc" on bottom */
}
/* moving to larger images after the first "bump" we may choose to separate
width-based adjustments vs. device-based logic. For example, a larger retina
device can go directly from "lowsrc" default to high-res with no mid-size image */
@media
/* poppa bear - smaller here for demo */
only screen and (min-width : 716px) {
/* no-multiplebgs - medium fallback above */
.multiplebgs .bg-image-sedona.hd {
background-image:
url(https://s.cdpn.io/23379/[email protected]), /* high-res on top */
url(https://s.cdpn.io/23379/photo-sedona_1024x640.jpg), /* mid-sized in middle */
url(https://s.cdpn.io/23379/photo-sedona_512x320.jpg); } /* "lowsrc" on bottom */
}
@media
/* larger retina devices, such as iPad, triggers immediately
so we no need for mid-size image */
only screen and (min-device-width : 768px) and (-webkit-min-device-pixel-ratio: 1.5),
only screen and (min-device-width : 768px) and ( min--moz-device-pixel-ratio: 1.5),
only screen and (min-device-width : 768px) and ( -o-min-device-pixel-ratio: 3/2),
only screen and (min-device-width : 768px) and ( min-device-pixel-ratio: 1.5),
only screen and (min-device-width : 768px) and ( min-resolution: 144dpi),
only screen and (min-device-width : 768px) and ( min-resolution: 1.5dppx) {
/* no-multiplebgs - medium fallback above */
.multiplebgs .bg-image-sedona.hd {
background-image:
url(https://s.cdpn.io/23379/[email protected]), /* high-res on top */
url(https://s.cdpn.io/23379/photo-sedona_512x320.jpg); } /* "lowsrc" on bottom, */
}
/* Presentational CSS Below */
.page-wrap {
display: block;
width: 80%;
max-width: 1280px;
margin: 0 auto 2em;
padding: 1.5em 3em 3em;
background: #fff;
text-align: left; }
h1 {
margin: .5em 0 0 0; }
h2 {
margin: .25em 0 .5em;
color: #999;
font-weight: 300; }
figure {
max-width: 1008px; /* max-width of .bg-image -padding */
margin: 1.5em 0 .5em; /* change 0 to auto to center */
padding: 8px 8px 4px 8px;
background: #eaeaea; }
figcaption {
margin: .5em 0;
padding: .125em .5em;
color: #999;
text-align: center;
font-size: 85%;
font-style: italic; }
code {
background: lightyellow;
padding: 1px .25em;
font-family: Menlo, Monaco, monospace;
font-style: normal; }
.nowrap, .nobr, .nobreak {
white-space: nowrap;
}
/* waits until everything is loaded, not just DOM is ready */
$(window).load(function() {
$('.bg-image').toggleClass('hd');
});
/* don't lock class names to functionality, use '[data-js=retina]' */
$('[data-js=retina]').click(function () {
$('.bg-image').toggleClass('hd');
});
Also see: Tab Triggers