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>
<h1>Vertical & horizontal centring*</h1>
<section class=container-flexbox>
<div class=centered-flexbox>
<figure>
<figcaption>IE10+ (preferred method)</figcaption>
<pre><code class=language-css spellcheck=false contenteditable>.container-flexbox {
min-height: 100vh; /* Unit & size irrelevant */
display: flex; /* Collapses box */
align-items: center;
justify-content: center;
}
.centered-flexbox {
}</code></pre>
</figure>
</div>
</section>
<section class=container-grid>
<div class=centered-grid>
<figure>
<figcaption>Grid method</figcaption>
<pre><code class=language-css spellcheck=false contenteditable>.container-grid {
min-height: 100vh; /* Unit & size irrelevant */
display: grid;
place-content: center;
}
.centered-grid {
}</code></pre>
</figure>
</div>
</section>
<section class=container-noFixedHeight>
<div class=centered-noFixedHeight>
<figure>
<figcaption>IE9+</figcaption>
<pre><code class=language-css spellcheck=false contenteditable>.container-noFixedHeight {
position: relative;
min-height: 100vh; /* Unit & size irrevelant */
}
.centered-noFixedHeight {
position: absolute; /* Collapses box */
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}</code></pre>
</figure>
</div>
</section>
<section class=container-float>
<div class=centered-float>
<figcaption>IE9+</figcaption>
<figure>
<pre><code class=language-css spellcheck=false contenteditable>.container-float {
height: 100vh; /* Unit irrelevant */
}
.container-float::after {
content: "";
display: table;
clear: both;
}
.centered-float {
position: relative;
float: left; /* Collapses box */
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}</code></pre>
</figure>
</div>
</section>
<section class=container-ellipsis>
<div class=centered-ellipsis>
<span class=centered-ellipsis-span>
Centre?
</span>
</div>
</section>
<section class=container-credits>
<div class=centered-credits>
<p>Further variants: <a class=lnk rel=noopener target=_blank title="[new window]" href="https://web.dev/centering-in-css/" target=_blank title="[new window]">Centering in CSS</a></p>
<p>More options and variants: <a class=lnk rel=noopener target=_blank title="[new window]" href="https://www.smashingmagazine.com/2013/08/absolute-horizontal-vertical-centering-css/" target=_blank title="[new window]">Absolute horizontal vertical centering CSS</a></p>
<p>Modern CSS: <a class=lnk rel=noopener target=_blank title="[new window]" href="https://moderncss.dev/resource-the-complete-guide-to-centering-in-css/">Complete Guide to Centering in CSS</a></p>
<p>CSS Tricks: <a class=lnk rel=noopener target=_blank title="[new window]" href="https://css-tricks.com/centering-the-newest-coolest-way-vs-the-oldest-coolest-way/">Centering - newest vs oldest</a></p>
<p>Photos: <a class=lnk rel=noopener target=_blank title="[new window]" href="https://unsplash.com/">Unsplash</a>
<p>*Brit eh?</p>
</div>
</section>
</main>
[[[https://codepen.io/2kool2/pen/mKeeGM]]]
/* Generic styles */
body {
font-family: sans-serif;
line-height: 1.5;
margin: 0;
background-color: #1a1a1a;
color: #fff;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
main {
height: 100vh;
overflow-y: scroll;
scroll-snap-type: y mandatory;
}
h1 {
font-weight: 100;
position: fixed;
color: #000;
background-color: rgba(255,255,255, .8);
top: 1rem;
left: 50%;
transform: translate(-50%, 0);
padding: .5rem 1rem;
width: 50%;
min-width: 20rem;
margin: 0 auto;
text-align: center;
line-height: 1.2;
}
p {margin: 1rem; max-width: 34rem;}
figure {margin: 0;}
figcaption {
background-color: #1a1a1a;
text-align: center;
padding: .25rem;
font-weight: 700;
}
a {
color: #fff !important;
text-decoration: none;
border-bottom: 1px solid rgba(255,255,255, .5);
}
[class^="container-"] {
scroll-snap-align: start;
}
[class^="centered-"] {
/* Optional: Keep inside viewport */
max-width: calc(100% - 2rem);
/* OPTIONAL: */
max-height: calc(100vh - 4rem);
/* Not required but plays nice */
transition: all .3s ease-out;
}
[class^="centered-"]:hover,
[class^="centered-"]:focus-within {
box-shadow: 0 0 .5rem .25rem rgba(0,0,0, .75);
}
/* Required centring styles */
/* Preferred but doesn't work at all in IE9 */
.container-flexbox {
min-height: 100vh; /* Unit & size irrevelant */
display: flex;
align-items: center;
justify-content: center;
}
.centered-flexbox {
}
/* OR */
/* Doesn't work at all in IE9 */
.container-grid {
min-height: 100vh; /* Unit & size irrevelant */
display: grid;
place-content: center;
}
.centered-grid {
/* margin: 0 auto; */
}
/* OR */
.container-noFixedHeight {
position: relative;
min-height: 100vh; /* Unit & size irrevelant */
}
.centered-noFixedHeight {
position: absolute; /* Collapses box */
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* OR */
.container-float {
height: 100vh; /* Unit irrevelant */
/* padding: 1rem; /* Optional */
}
.container-float::after {
content: "";
display: table;
clear: both;
}
.centered-float {
position: relative;
float: left; /* Collapses box */
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* OR */
.container-ellipsis {
position: relative;
height: 100vh; /* Unit irrevelant */
}
.centered-ellipsis,
.centered-ellipsis-span {
position: absolute; /* Collapses box */
max-width: 50vmin;
max-height: 50vmin;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.centered-ellipsis {
/* Sets this div to 50% sq */
bottom: 0;
right: 0;
}
.centered-ellipsis-span {
}
/* Credits section */
.container-credits {
height: 100vh; /* Unit irrevelant */
display: flex;
align-items: center;
justify-content: center;
}
/* Pretty-pretties - Not required but sweet */
/* Images unsplash.com */
.container-float {
background-position: center bottom;
background-size: cover;
/* Dmitri Popov */
background-image: url(https://images.unsplash.com/photo-1497514935393-24b153421536?w=1920);
}
.container-noFixedHeight {
background-position: center;
background-size: cover;
/* Michał Grosicki */
background-image: url(https://images.unsplash.com/photo-1489343511429-5482f78c15cf?w=1920);
}
.container-absolute {
background-position: center;
background-size: cover;
/* Michał Grosicki */
background-image: url(https://images.unsplash.com/photo-1494187042164-c3d951f93e5c?w=1920);
}
.container-flexbox {
background-position: center bottom;
background-size: cover;
/* James Padolsey */
background-image: url(https://images.unsplash.com/photo-1476958526483-36efcaa80b1b?w=1920);
}
.container-grid {
background-position: center top;
background-size: cover;
/* Wolfgang Hasselmann */
background-image: url(https://unsplash.com/photos/mZabJ9h8_jc/download?force=true&w=1920);
}
.container-ellipsis {
background-position: center;
background-size: cover;
/* Jeremy Thomas */
background-image: url(https://unsplash.com/photos/E0AHdsENmDg/download?force=true&w=1920);
}
.centered-ellipsis {
border-radius: 50%;
box-shadow: inset 0 2.5rem 5rem 1.5rem rgba(0,0,0, .8), 0 0 1rem .125rem #000;
background: linear-gradient(to bottom, #342a29 0%,#b4300a 9%,#ffc102 30%,#edddd7 44%,#e8ebf2 50%,#edddd7 56%,#ffc102 70%,#b4300a 87%,#342a29 100%);
}
.centered-ellipsis:hover {
box-shadow: inset 0 2.5rem 5rem 1.5rem rgba(0,0,0, .8), 0 0 1rem .125rem #000;
}
.centered-ellipsis-span {
background-color: #000;
padding: 1rem;
font-weight: 700;
border-radius: 50%;
box-shadow: 0 0 1rem 2rem #b4300a;
box-shadow: 0 0 3vmin 4vmin #b4300a;
}
.container-credits {
padding: 1rem;
height: calc(100vh - 2rem);
background-position: center bottom;
background-size: cover;
background-image: url(https://images.unsplash.com/photo-1431576901776-e539bd916ba2?w=1920);
}
.centered-credits {
background-color: rgba(0,0,0, .85);
}
.centered-credits:hover {
background-color: rgba(0,0,0, 1);
}
// Prism used for code highlighting
// Force dark mode
document.body.setAttribute('data-lightmode', 'dark');
Also see: Tab Triggers