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.
<div class="controls js-controls">
<h2 class="controls__heading js-controls-drag" draggable="true">Enhance!</h2>
<input type="range" min="0" max="3" class="controls__slider js-slider"/>
<div class="controls__status js-status" aria-live="polite" role="status">Basic, but ok!</div>
</div>
<header class="site__header">
<h1 class="site__title">Use Grid today!</h1>
<nav class="nav">
<ul class="nav__items">
<li class="nav__item"><a href="#" class="nav__link nav__link--active">Home</a></li>
<li class="nav__item"><a href="https://www.smashingmagazine.com/2017/07/enhancing-css-layout-floats-flexbox-grid/" class="nav__link">About this demo</a></li>
<li class="nav__item"><a href="http://gridbyexample.com/" class="nav__link">Gridbyexample</a></li>
<li class="nav__item"><a href="https://alistapart.com/article/understandingprogressiveenhancement" class="nav__link">Progressive Enhancement</a></li>
<li class="nav__item"><a href="https://cssgridgarden.com/" class="nav__link">Grid Garden</a></li>
</ul>
</nav>
</header>
<main class="site__content">
<section class="teaser">
<div class="teaser__content">
<h2 class="teaser__content__heading">Progressive Enhancement Demo</h2>
<p class="teaser__content__sub">Leverage the power of CSS Grid Layout!<br />Read <a href="https://www.smashingmagazine.com/2017/07/enhancing-css-layout-floats-flexbox-grid/">this article</a> for details on how to use CSS Grid Layout today.</p>
</div>
</section>
<section class="featured clearfix">
<h2 class="featured__heading heading">Four levels of enhancement</h2>
<article class="featured__post">
<h3 class="featured__post__heading">No Positioning</h3>
<a href="#" class="featured__post__link">
<picture>
<source srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/320_480.jpg" media="(min-width: 600px)"/>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/480_320.jpg" alt="" class="featured__post__img" />
</picture>
</a>
</article>
<article class="featured__post">
<h3 class="featured__post__heading">Floated Layout</h3>
<a href="#" class="featured__post__link">
<picture>
<source srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/320_480.jpg" media="(min-width: 600px)"/>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/480_320.jpg" alt="" class="featured__post__img" />
</picture>
</a>
</article>
<article class="featured__post">
<h3 class="featured__post__heading">Flexbox Enhancements</h3>
<a href="#" class="featured__post__link">
<picture>
<source srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/320_480.jpg" media="(min-width: 600px)"/>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/480_320.jpg" alt="" class="featured__post__img" />
</picture>
</a>
</article>
<article class="featured__post">
<h3 class="featured__post__heading">CSS Grid Layout</h3>
<a href="#" class="featured__post__link">
<picture>
<source srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/320_480.jpg" media="(min-width: 600px)"/>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/480_320.jpg" alt="" class="featured__post__img" />
</picture>
</a>
</article>
</section>
<section class="text">
<h2 class="text__heading heading">Browser Support</h2>
<div class="text__content">
<div class="text__content__section">
<h3 class="text__subheading">Works even in very old browsers</h3>
<p>
Check this page in IE 8, Opera Mini Extreme Mode or UC Browser. Thanks to progressive enhacement it just works.
</p>
</div>
<div class="text__content__section">
<h3 class="text__subheading">How does it work?</h3>
<p>
I've written <a href="https://www.smashingmagazine.com/2017/07/enhancing-css-layout-floats-flexbox-grid/">a post</a> where I've highlighted and commented the important lines.
</p>
</div>
</div>
</section>
<section class="news clearfix">
<h2 class="news__heading heading">Latest articles</h2>
<article class="article">
<h3 class="article__heading">Heading</h3>
<a href="#" class="article__link">
<img class="article__image" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/320_213.jpg" alt="" />
</a>
<div class="article__content">
<p>Lorem ipsum dolor sit amet, consectetur <a href="#">adipisicing</a> elit. Vel molestiae ut voluptates eveniet id voluptatem sit blanditiis deserunt tempora reiciendis a minima distinctio voluptas explicabo.</p>
</div>
</article>
<article class="article article--flexible">
<h3 class="article__heading">Heading</h3>
<a href="#" class="article__link">
<img class="article__image" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/320_213.jpg" alt="" />
</a>
<div class="article__content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vel molestiae ut voluptates eveniet id voluptatem sit blanditiis deserunt tempora reiciendis a minima distinctio voluptas explicabo, officia, voluptatibus? Doloribus, iusto, reprehenderit.</p>
</div>
</article>
<article class="article article--flexible">
<h3 class="article__heading">Heading</h3>
<a href="#" class="article__link">
<img class="article__image" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/320_213.jpg" alt="" />
</a>
<div class="article__content article__content--flexible">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vel molestiae ut voluptates eveniet id voluptatem.</p>
</div>
</article>
<article class="article article--flat">
<h3 class="article__heading">Heading</h3>
<a href="#" class="article__link">
<img class="article__image" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/320_119.jpg" alt="" />
</a>
<div class="article__content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</article>
<article class="article article--flat">
<h3 class="article__heading">Heading</h3>
<a href="#" class="article__link">
<img class="article__image" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/320_119.jpg" alt="" />
</a>
<div class="article__content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</article>
<article class="article article--flat">
<h3 class="article__heading">Heading</h3>
<a href="#" class="article__link">
<img class="article__image" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/144736/320_119.jpg" alt="" />
</a>
<div class="article__content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</article>
</section>
</main>
<footer class="site__footer">
<div class="site__footer__wrap">
<ul class="site__footer__items">
<li class="footer__item"><a href="#" class="footer__link footer__link--active">Home</a></li>
<li class="footer__item"><a href="#" class="footer__link">About this demo</a></li>
<li class="footer__item"><a href="#" class="footer__link">Gridbyexample</a></li>
<li class="footer__item"><a href="#" class="footer__link">Progressive Enhancement</a></li>
</ul>
<small>by <a href="https://www.twitter.com/mmatuzo">Manuel Matuzović</a></small>
</div>
</footer>
$prim: #212121;
$sec: #eb1734;
$ter: #f6f6f6;
$qua: #ffffff;
$bp1: 600px;
$bp2: 900px;
$bp3: 1200px;
$bp4: 1400px;
$bp5: 1600px;
@mixin feature($feature) {
.#{$feature} & {
@content;
}
}
* {
box-sizing: border-box;
}
img {
max-width: 100%;
border: 0;
}
a:link, a:visited {
color: $sec;
}
.site {
font-size: 62.5%;
// Header
&__header {
align-items: flex-end;
justify-content: space-between;
grid-column: 2 / -2;
@media (min-width: $bp1) {
overflow: hidden;
}
@media (min-width: $bp2) {
@include feature('float') {
padding: 1em;
}
}
@media (min-width: $bp3) {
margin-bottom: 2em;
@include feature('flex') {
display: flex;
}
}
}
// Content
&__content {
justify-content: space-between;
flex-wrap: wrap;
@media (min-width: $bp2) {
grid-column: 1 / -1;
}
@include feature('flex') {
display: flex;
}
@include feature('grid') {
@media (min-width: $bp3) {
display: grid;
grid-template-columns: 1fr;
grid-template-areas: "teaser"
"featured"
"text"
"news";
}
@media (min-width: $bp5) {
grid-template-columns: 10em minmax(auto, 60em) 10em;
grid-template-areas: "teaser teaser teaser"
". featured ."
"text text text"
". news .";
}
}
}
&__title {
border-bottom: 4px solid $sec;
padding-bottom: 0.25em;
margin: 0;
@media (min-width: $bp2) {
display: inline-block;
}
}
&__footer {
background-color: $ter;
padding: 1em;
grid-column: 1 / -1;
&__items {
margin: 0 0 1em;
padding: 0;
list-style: none;
}
&__wrap {
max-width: 54em;
margin: 0 auto;
}
}
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-size: 2em;
line-height: 1.5;
max-width: $bp1;
margin: 0 auto;
padding: 0.625em;
grid-template-columns: 1fr minmax(auto, 80em) 1fr;
@media (min-width: $bp1) {
max-width: 70em;
}
@media (min-width: $bp2) {
@include feature('grid') {
display: grid;
@supports(display: grid) {
max-width: 100%;
}
}
}
}
// Nav
.nav {
@include feature('grid') {
@supports(display: grid) {
margin-bottom: 0;
}
}
&__items {
margin: 0;
padding: 1.5em 0 1em;
list-style: none;
justify-content: right;
@include feature('float') {
overflow: hidden;
@media (min-width: $bp1) {
float: right;
}
}
@media (min-width: $bp2) {
@include feature('flex') {
display: flex;
}
}
}
&__item {
font-weight: bold;
margin: 0 0.5em 0.5em 0;
@include feature('float') {
float: left;
margin: 0 0.5em 0 0;
@media (min-width: $bp2) {
margin: 0 0 0 1em;
}
}
}
&__link {
color: $prim;
display: block;
text-decoration: none;
&:link, &:visited {
color: $prim;
}
&--active:link,
&--active:visited,
&:focus,
&:hover {
color: $sec;
text-decoration: underline;
outline: none;
}
}
}
// Teaser
.teaser {
background-color: $sec;
color: $ter;
font-size: 1.5em;
padding: 0.5em 0.75em 0.75em;
margin: 0 auto 2em;
clear: both;
width: 100%;
text-align: center;
justify-content: center;
align-items: center;
grid-area: teaser;
@include feature('flex') {
display: flex;
}
@media (min-width: $bp2) {
font-size: 2em;
margin-bottom: 1.5em;
@include feature('float') {
height: 10em;
display: table;
}
}
a:link, a:visited {
color: $prim;
}
a:hover {
text-decoration: none;
}
&__content {
@media (min-width: $bp1) {
@include feature('float') {
display: table-cell;
vertical-align: middle;
}
}
&__heading {
line-height: 1.3;
margin: 0 0 0.4em;
}
&__sub {
font-size: 0.75em;
margin: 0;
}
}
}
.heading {
font-size: 2em;
color: $sec;
line-height: 1.2;
margin: 0 0 0.5em;
}
// Featured
.featured {
margin: 0 0 3em;
flex-wrap: wrap;
justify-content: space-between;
grid-area: featured;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: minmax(5em, 1fr) 1fr 1fr;
align-items: start;
grid-gap: 1em;
@include feature('flex') {
@media (min-width: $bp1) {
display: flex;
}
}
@include feature('grid') {
@media (min-width: $bp1) {
display: grid;
}
}
@media (min-width: $bp2) {
grid-template-columns: repeat(3, 1fr);
grid-template-rows: none;
}
@media (min-width: $bp3) {
grid-template-columns: repeat(4, 1fr);
}
&__heading {
flex: 0 0 100%;
grid-row: 1;
grid-column: 2;
@media (min-width: $bp2) and (max-width: $bp3) {
grid-column: 3;
}
}
&__post {
$post: &;
flex-direction: column;
&:not(:last-child) {
margin-bottom: 1em;
}
@media (min-width: $bp1) and (max-width: $bp2) {
width: 49.5%;
grid-row: span 2;
&:nth-child(even) {
margin: 0 1% 0 0;
}
}
@media (min-width: $bp2) {
width: 24.25%;
grid-row: auto;
&:not(:last-child) {
margin: 0 1% 0 0;
}
}
@media (min-width: $bp3) {
grid-row: span 2;
&:nth-of-type(2) {
grid-column: 2;
grid-row: 2 / span 2;
}
}
&__heading {
margin: 0;
order: 1;
}
@include feature('float') {
float: left;
}
@include feature('flex') {
display: flex;
}
@include feature('grid') {
@supports(display: grid) {
width: auto;
&:nth-of-type(3) {
@media (min-width: $bp3) {
margin-top: 2em;
}
}
}
}
&__link {
&:focus,
&:hover {
#{$post}__img {
outline: 4px solid $sec;
filter: saturate(2);
}
}
}
&__img {
vertical-align: middle;
width: 100%;
}
}
}
// Text
.text {
background: $ter;
margin: 0 0 3em;
padding: 1em 0.75em 0.5em;
width: 100%;
grid-area: text;
@media (min-width: $bp3) {
grid-area: text;
grid-template-columns: repeat(2, 1fr);
align-items: start;
@include feature('grid') {
@supports(display: grid) {
padding-left: 0;
padding-right: 0;
}
}
}
@include feature('grid') {
@media (min-width: $bp3) {
display: grid;
}
}
&__subheading {
margin: 0;
}
&__heading {
@media (min-width: $bp3) {
margin: 0.5em 0;
grid-column-start: 2;
}
}
&__content {
@media (min-width: $bp2) and (max-width: $bp3) {
grid-template-columns: repeat(2, 1fr);
grid-gap: 1em;
@include feature('flex') {
display: flex;
}
@include feature('grid') {
display: grid;
}
&__section {
flex: 1 0 0;
padding-right: 2rem;
@include feature('grid') {
@supports(display: grid) {
padding-right: 0;
}
}
}
}
@media (min-width: $bp3) {
max-width: 20em;
grid-column-start: 2;
}
}
}
.news {
margin-bottom: 3em;
width: 100%;
@media (min-width: $bp1) {
flex-wrap: wrap;
justify-content: space-between;
grid-area: news;
grid-template-columns: repeat(2, 1fr);
grid-gap: 1em;
justify-self: center;
&__heading {
flex-basis: 33.333%;
max-width: 33.333%;
grid-column: 1;
@include feature('grid') {
@supports(display: grid) {
max-width: 100%;
}
}
}
}
@media (min-width: $bp2) {
grid-template-columns: repeat(3, 1fr);
}
@include feature('flex') {
@media (min-width: $bp1) {
display: flex;
}
}
@include feature('grid') {
@media (min-width: $bp1) {
display: grid;
@supports(display: grid) {
max-width: 54em;
}
}
}
}
// Articles
.article {
background-color: $prim;
color: $qua;
padding: 1em;
margin-bottom: 0.5em;
grid-template-columns: 1fr;
&:first-child {
grid-row: span 2;
}
@media (min-width: $bp2) {
&:nth-child(2),
&:nth-child(3) {
grid-template-columns: 16em 1fr;
grid-template-rows: minmax(10em, 1fr);
grid-column: 2 / -1;
grid-gap: 1em;
}
}
&--flat {
align-self: start;
grid-template-rows: none;
}
&__heading {
margin: 0;
padding: 0.5rem 1rem;
font-size: 1.4em;
grid-row: 1;
grid-column: 1;
z-index: 1;
align-self: start;
justify-self: end;
@include feature('grid') {
@supports(display: grid) {
background-color: $sec;
color: $ter;
}
}
}
&__link {
grid-row: 1;
grid-column: 1;
display: block;
&:focus,
&:hover {
outline: 4px solid $sec;
}
}
&__image {
object-fit: cover;
width: 100%;
display: block;
max-width: 30em;
height: 100%;
}
@include feature('float') {
@media (min-width: $bp1) {
width: 50%;
float: left;
border: 5px solid #ffffff;
}
@media (min-width: $bp1) and (max-width: $bp2) {
&:nth-of-type(odd) {
clear: left;
}
}
@media (min-width: $bp2) {
width: 33.3%;
&:nth-of-type(4n) {
clear: left;
}
}
}
@media (min-width: $bp2) {
@include feature('flex') {
flex-basis: 33.333%;
max-width: 33.333%;
}
@include feature('grid') {
@supports(display: grid) {
max-width: 100%;
}
}
}
@include feature('grid') {
display: grid;
@supports(display: grid) {
max-width: 100%;
width: 100%;
border: none;
@media (min-width: $bp1) {
margin-bottom: 0;
}
&__content {
padding: 0.5em 0;
p {
margin: 0;
}
&--flexible {
//padding: 0 0.5em;
}
}
}
}
}
// Controls
.controls {
background-color: $qua;
left: 1em;
padding: 1em 1.5em;
top: 1em;
z-index: 5000;
width: 225px;
&__heading {
color: $sec;
margin: 0 0 0.75em;
cursor: move;
}
&__slider {
width: 100%;
}
&__status {
font-style: italic;
margin-top: 0.5em;
}
@media (min-width: $bp1) {
border-radius: 5px;
box-shadow: 0 0 10px 0 rgba(0,0,0,0.2);
position: fixed;
}
}
.clearfix:after {
content: "";
display: table;
clear: both;
}
// IE8
main, nav, aside, header, footer, section, article {
display: block;
}
var slider = document.querySelector('.js-slider');
slider.value = 0;
var features = ['', 'float', 'flex', 'grid']
var message = ['Basic, but ok!', 'Floated, not bad!', 'Flexbox, niiiice!', 'Holy Grid!'];
var _root = document.documentElement;
function switchFeature(e) {
_root.className = 'site';
for (var i = 0; i <= parseInt(e.target.value); i++) {
document.querySelector('.js-status').textContent = message[i];
_root.className += features[i] + " ";
}
}
slider.addEventListener('change', switchFeature);
slider.addEventListener('input', switchFeature);
var controls_drag = document.querySelector('.js-controls-drag');
function drag_start(event) {
console.log(event.target.parentNode)
var style = window.getComputedStyle(event.target.parentNode, null);
event.dataTransfer.setData("text/plain",
(parseInt(style.getPropertyValue("left"), 10) - event.clientX) + ',' + (parseInt(style.getPropertyValue("top"), 10) - event.clientY));
}
function drag_over(event) {
event.preventDefault();
return false;
}
function drop(event) {
var offset = event.dataTransfer.getData("text/plain").split(',');
var controls = document.querySelector('.js-controls');
controls.style.left = (event.clientX + parseInt(offset[0],10)) + 'px';
controls.style.top = (event.clientY + parseInt(offset[1],10)) + 'px';
event.preventDefault();
return false;
}
controls_drag.addEventListener('dragstart',drag_start,false);
document.body.addEventListener('dragover',drag_over,false);
document.body.addEventListener('drop',drop,false);
Also see: Tab Triggers