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.
<nav class="skiplinks">
<a class="accessibility" href="#main">Skip to main content</a>
</nav>
<header class="site-header full" role="banner">
<div class="wrapper">
<p>Header</p>
</div>
</header>
<main id="main" class="site-main full">
<div class="site-full full">
<p>Fullwidth content - e.g. hero image, video</p>
</div>
<div class="site-wide">
<div class="wrapper">
<h1>H1 page heading inside wide content</h1>
<p>Internal grid layout below is based on an existing desktop-first grid system. Now updated to be <strong>mobile-first flexbox</strong> without changing HTML markup. Fallback to simple 2-col "inline-block" layout.</p>
<h2>4-cols with extra <code>grid-item</code></h2>
<p>Can set <code>data-cols-fill="true"</code> on parent <code>grid</code> to automatically handle rows that are incomplete or contain orphans.</p>
<div class="grid grid-4" data-cols-fill="false">
<div class="grid-item">
<div class="grid-item-wrap">
Grid item - equal height columns
</div>
</div>
<div class="grid-item">
<div class="grid-item-wrap">
Grid item
</div>
</div>
<div class="grid-item">
<div class="grid-item-wrap">
Grid item
</div>
</div>
<div class="grid-item">
<div class="grid-item-wrap">
Grid item
</div>
</div>
<div class="grid-item">
<div class="grid-item-wrap">
Grid item
</div>
</div>
</div>
<h2>Nested grid</h2>
<div class="grid" data-cols-fill="false">
<div class="grid-item">
<div class="grid-item-wrap">
<div class="grid">
<div class="grid-item">
<div class="grid-item-wrap">
Nested grid item
</div>
</div>
<div class="grid-item">
<div class="grid-item-wrap">
Nested grid item
</div>
</div>
<div class="grid-item">
<div class="grid-item-wrap">
Nested grid item
</div>
</div>
</div>
</div>
</div>
<div class="grid-item">
<div class="grid-item-wrap">
<div class="grid">
<div class="grid-item">
<div class="grid-item-wrap">
Nested grid item
</div>
</div>
<div class="grid-item">
<div class="grid-item-wrap">
Nested grid item
</div>
</div>
<div class="grid-item">
<div class="grid-item-wrap">
Nested grid item
</div>
</div>
</div>
</div>
</div>
</div>
<h2>Asymmetric grid</h2>
<p>e.g. apply <code>grid-2-wide-left</code> class to parent <code>grid</code>. Ratio is 2:1.</p>
<div class="grid grid-2-wide-left" data-cols-fill="false">
<div class="grid-item">
<div class="grid-item-wrap">
Asymmetric grid item
</div>
</div>
<div class="grid-item">
<div class="grid-item-wrap">
Asymmetric grid item
</div>
</div>
</div>
<h2>Grid items span multiple cols</h2>
<p>e.g. apply <code>.g</code> class to <code>grid-item</code>.</p>
<div class="grid grid-3" data-cols-fill="false">
<div class="grid-item g5">
<div class="grid-item-wrap">
".g5" grid item spans 5 cols
</div>
</div>
<div class="grid-item g4">
<div class="grid-item-wrap">
".g4" grid item spans 4 cols
</div>
</div>
<div class="grid-item g3">
<div class="grid-item-wrap">
".g3" grid item spans 3 cols
</div>
</div>
</div>
</div>
</div>
<!--
Need additional FLEX (or float) container for CSS GRID fallback scenarios.
This allows correct HTML source order, but sidebar can be placed above content in mobile view.
-->
<div class="site-content-sidebar-wrapper">
<div class="site-content">
<div class="wrapper">
<p>Main content</p>
<h2>3-cols with extra <code>grid-item</code></h2>
<div class="grid grid-3" data-cols-fill="false">
<div class="grid-item">
<div class="grid-item-wrap">
Grid item - equal height columns
</div>
</div>
<div class="grid-item">
<div class="grid-item-wrap">
Grid item
</div>
</div>
<div class="grid-item">
<div class="grid-item-wrap">
Grid item
</div>
</div>
<div class="grid-item">
<div class="grid-item-wrap">
Grid item
</div>
</div>
</div>
</div>
</div>
<div class="site-sidebar">
<div class="wrapper">
<p>Sidebar</p>
</div>
</div>
</div>
<div class="site-wide">
<div class="wrapper">
<p>Wide content</p>
</div>
</div>
</main>
<footer class="site-footer full" role="contentinfo">
<div class="wrapper">
<p>Sticky footer</p>
</div>
</footer>
// Modify any of the following 3 variables to update overall site width...
$grid-column-width: 60px;
$grid-column-count: 12;
$grid-gap: 20px;
// We're implicitly defining "full" and "main" grid template areas by specifying "-start" and "-end" - see
// https://rachelandrew.co.uk/archives/2017/06/01/breaking-out-with-css-grid-explained/
$grid-template-cols: [full-start] 1fr [main-start] repeat(#{$grid-column-count}, minmax(auto, #{$grid-column-width})) [main-end] 1fr [full-end];
$site-width: ($grid-column-count * ($grid-column-width + $grid-gap)) - $grid-gap; // 940px
$site-padding: $grid-gap;
$bg-blue: rgb(69, 108, 169);
$bg-grey: #ccc;
$color-light: #fff;
$mq-small: 480px;
$mq-medium: 640px;
$mq-large: 980px;
// -------------------------------------------------------
// Simple reset, basic styling
// -------------------------------------------------------
* {
box-sizing: border-box;
margin: 0;
}
html {
height: 100%;
}
.skiplinks {
position: absolute;
}
// Accessibility
.accessibility {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
&:focus {
background: $color-light;
clip: auto;
font-weight: bold;
height: auto;
left: 10px;
margin: 0;
overflow: visible;
padding: 5px;
top: 10px;
width: auto;
}
}
.full,
.site-wide {
> .wrapper {
margin: 0 auto;
padding-left: $grid-gap;
padding-right: $grid-gap;
@media (min-width: #{$site-width + (2 * $grid-gap)}) {
max-width: $site-width;
padding-left: 0;
padding-right: 0;
}
}
}
// -------------------------------------------------------
// Page template grid - CSS Grid
// -------------------------------------------------------
// -------------------------------------------------------
// Standards CSS grid support - use @supports instead of Modernizr "grid" detection
// -------------------------------------------------------
@supports (grid-area: auto) {
body {
display: grid;
grid-gap: $grid-gap;
grid-template-columns: $grid-template-cols;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
> * {
grid-column: main;
}
}
.site-main,
.site-content-sidebar-wrapper {
grid-column: 1 / span $grid-column-count + 2;
// Define inner grid
display: grid;
grid-gap: $grid-gap;
grid-template-columns: $grid-template-cols;
}
.site-sidebar,
.site-content {
grid-column: 2 / span $grid-column-count;
}
.site-sidebar {
grid-row: 1;
}
.site-wide {
grid-column: 2 / span $grid-column-count;
}
.full {
grid-column: full;
}
@media (min-width: #{$mq-medium + 1}) {
.site-content {
// e.g. 12 cols, start at col 5, span 9 cols (75% overall width)
grid-column: #{($grid-column-count * .25) + 2} / span $grid-column-count * .75;
}
.site-sidebar {
// e.g. 12 cols, span 3 cols (25% overall width)
grid-column: 2 / span $grid-column-count * .25;
}
}
}
// -------------------------------------------------------
// Page template grid layout - CSS Flexbox
// -------------------------------------------------------
// -------------------------------------------------------
// 1. Edge uses old CSS grid syntax so fallback to flexbox instead, and use flexbox for IE11/10 too - see
// https://rachelandrew.co.uk/archives/2017/04/04/edge-starts-work-on-their-grid-implementation-update/
// https://rachelandrew.co.uk/css/cheatsheets/grid-fallbacks
// 2. Sticky footer - see
// https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/
// -------------------------------------------------------
@mixin layout-page-fallback {
.site-full {
margin-left: -$grid-gap;
margin-right: -$grid-gap;
}
.site-main {
padding: 0 $grid-gap;
}
.site-header,
.site-main,
.site-full,
.site-wide,
.site-content-sidebar-wrapper,
.site-sidebar {
margin-bottom: $grid-gap;
}
.site-content-sidebar-wrapper {
display: table;
width: 100%;
}
.site-sidebar {
display: table-caption;
}
}
@mixin layout-page-flex {
@include layout-page-fallback;
body {
display: flex;
flex-direction: column; // [2]
height: 100%; // [2]
}
.site-header,
.site-footer {
flex: none; // [2]
}
.site-main,
.site-content-sidebar-wrapper {
flex: 1 0 auto; // [2]
}
.site-full,
.site-wide {
flex: 1 0 100%;
}
@media (min-width: #{$mq-medium + 1}) {
.site-main,
.site-content-sidebar-wrapper {
// Define inner flex container
align-content: flex-start;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.site-content-sidebar-wrapper {
flex-direction: row-reverse;
max-width: $site-width;
}
.site-sidebar,
.site-content {
flex: 1 0 auto;
}
.site-sidebar {
display: inline-block; // To override "table-caption" in mobile view
margin-right: $grid-gap;
max-width: (.25 * $grid-column-count * $grid-column-width) + (((.25 * $grid-column-count) - 1) * $grid-gap);
}
}
}
// e.g. Edge
@supports not (grid-area: auto) {
@include layout-page-flex;
}
// e.g. IE11/IE10
.flexbox.no-flexboxlegacy {
@include layout-page-flex;
}
// -------------------------------------------------------
// Page template grid layout - NO CSS Grid / Flexbox
// -------------------------------------------------------
// -------------------------------------------------------
// Fallbacks - no grid / no flexbox
// -------------------------------------------------------
@mixin clearfix {
&:before,
&:after {
content: '';
display: table;
}
&:after {
clear: both;
}
}
.no-flexbox.no-flexboxlegacy {
@include layout-page-fallback;
.site-main {
margin: 0 auto;
max-width: #{$site-width + (2 * $site-padding)};
}
.site-full {
margin-left: 0;
margin-right: 0;
}
.site-content,
.site-sidebar {
margin-right: 0;
max-width: 100%;
}
@media (min-width: #{$mq-large + 1}) {
.site-content-sidebar-wrapper {
@include clearfix;
}
.site-content {
float: right;
// 75% overall width
max-width: (.75 * $grid-column-count * $grid-column-width) + (((.75 * $grid-column-count) - 1) * $grid-gap);
width: 100%;
}
.site-sidebar {
float: left;
// 25% overall width
max-width: (.25 * $grid-column-count * $grid-column-width) + (((.25 * $grid-column-count) - 1) * $grid-gap);
width: 100%;
}
}
}
// -------------------------------------------------------
// Internal content "grid" layout - uses flexbox
// 1. Left over "grid-items" grow to fill row
// 1a. Deal with remainder of 1
// See http://www.heydonworks.com/article/flexbox-grid-finesse for [1] and [1a]
// 2. Or ... left over "grid-items" have same widths as all the rest
// 3. Equal height "grid-items"
// -------------------------------------------------------
@mixin flex-size($flex-width, $property: flex-basis) {
#{$property}: calc(#{$flex-width} - #{$grid-gap});
}
.grid {
display: flex;
flex-wrap: wrap;
@media (min-width: #{$mq-small + 1}) {
margin-right: -$grid-gap;
}
}
.grid-item {
display: flex; // [3]
flex: 0 0 100%; // [2]
flex-direction: column; // [3]
margin-bottom: $grid-gap;
@media (min-width: #{$mq-small + 1}) {
@include flex-size(50%);
margin-right: $grid-gap;
@for $i from 3 through 6 {
.grid-#{$i} & {
@include flex-size(100% / $i);
}
}
}
}
.grid-item-wrap {
flex: 1 0 auto; // [3]
}
// Nested grids
.grid {
.grid {
margin-bottom: -$grid-gap;
}
}
// Asymmetric grids
.grid-2-wide-left {
@media (min-width: #{$mq-small + 1}) {
> .grid-item:first-child {
@include flex-size(66.667%);
}
> .grid-item:last-child {
@include flex-size(33.333%);
}
}
}
.grid-2-wide-right {
@media (min-width: #{$mq-small + 1}) {
> .grid-item:first-child {
@include flex-size(33.333%);
}
> .grid-item:last-child {
@include flex-size(66.667%);
}
}
}
.grid-2-wide-left,
.grid-2-wide-right {
@media (min-width: #{$mq-small + 1}) and (max-width: #{$mq-medium}) {
> .grid-item:first-child,
> .grid-item:last-child {
@include flex-size(50%);
}
}
}
// Grid items span multiple cols
.grid,
.grid-3,
.grid-4,
.grid-5,
.grid-6 {
@media (min-width: #{$mq-medium + 1}) {
.g2 { @include flex-size(16.667%) }
.g3 { @include flex-size(25%) }
.g4 { @include flex-size(33.333%) }
.g5 { @include flex-size(41.667%) }
.g6 { @include flex-size(50%) }
.g7 { @include flex-size(58.333%) }
.g8 { @include flex-size(66.667%) }
.g9 { @include flex-size(75%) }
.g10 { @include flex-size(83.333%) }
}
}
// Last "grid-item" wraps before we hit mobile breakpoint
:not([data-cols-fill='true']) {
@media (min-width: #{$mq-small + 1}) and (max-width: #{$mq-medium}) {
@for $i from 3 through 6 {
&.grid-#{$i} {
.grid-item {
@include flex-size(100% / ($i - 1));
}
}
}
}
@media (min-width: #{$mq-small + 1}) and (max-width: #{$mq-large}) {
// Nested grid
.grid-item {
.grid-item {
@include flex-size(100%);
}
}
}
}
[data-cols-fill='true'] {
.grid-item {
flex-grow: 1; // [1]
// 220px (i.e. same width as sidebar, based on 25% of overall width)
min-width: (.25 * $grid-column-count * $grid-column-width) + (((.25 * $grid-column-count) - 1) * $grid-gap);
}
@for $i from 3 through 6 {
&.grid-#{$i} {
:nth-last-child(2):nth-child(#{$i}n) {
@include flex-size(100% / ($i - 1), min-width); // [1a]
}
}
}
}
// -------------------------------------------------------
// Internal content "grid" layout fallbacks - uses inline-block
// -------------------------------------------------------
.no-flexbox.no-flexboxlegacy {
.grid {
letter-spacing: -.4em;
margin-right: 0;
width: 102%;
> .grid-item {
display: inline-block;
letter-spacing: normal;
margin-right: 0;
padding-right: 2%;
vertical-align: top;
width: 100%;
@media (min-width: #{$mq-medium + 1}) {
width: 50%;
}
.grid-item {
width: 100%;
}
}
}
}
// -------------------------------------------------------
// Styling for decorative purposes only
// -------------------------------------------------------
body {
font-family: sans-serif;
}
h1,
h2,
p {
font-size: 16px;
margin-bottom: 1em;
}
.wrapper {
background-color: $bg-grey;
padding: 10px 0;
}
.full {
background-color: rgba($bg-blue, .2);
&.site-main {
background-color: transparent;
}
> .wrapper {
background: $bg-blue;
color: $color-light;
}
}
.site-header,
.site-footer {
> .wrapper {
height: 100%;
}
}
.site-full {
background-color: rgba($bg-blue, .5);
padding: $site-padding;
}
.site-main,
.site-content-sidebar-wrapper {
outline: 4px dashed tomato;
position: relative;
&:before,
&:after {
background-color: rgba(tomato, .7);
content: '<main id="main">';
display: inline-block;
color: $color-light;
padding: 5px;
position: absolute;
right: 0;
top: 0;
}
&:after {
bottom: 0;
content: '</main>';
top: auto;
}
}
.site-content-sidebar-wrapper {
outline-color: darkolivegreen;
&:before,
&:after {
background-color: rgba(darkolivegreen, .7);
}
&:before {
content: '<div class="site-content-sidebar-wrapper">';
}
&:after {
content: '</div>';
}
}
.grid-item-wrap {
background-color: darkolivegreen;
color: $color-light;
padding: $grid-gap;
.grid-item-wrap {
background-color: rgba(tomato, .5);
}
}
Also see: Tab Triggers