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="hero" data-full-height>
<div class="container">
<h1>🦸‍♂️ CSS-Only Fix for Full Height Heroes</h1>
<p>Currently, WebKit-based browsers support <code>-webkit-fill-available</code> for setting the size of an element to the available space in its container.</p>
<p>This is awesome for solving common height problems, like the classic <a href="https://allthingssmitty.com/2020/05/11/css-fix-for-100vh-in-mobile-webkit/" target="_blank" rel="noopener">100vh issue in mobile WebKit</a>.</p>
<details>
<summary><strong>But there are still some important gotchas to watch out for…</strong></summary>
<ul>
<li><strong>Chrome Height Bug</strong>: <a href="https://github.com/postcss/postcss-100vh-fix#readme" target="_blank" rel="noopener">As Sitnick pointed out in his PostCSS plugin</a>, Chrome currently fails to calculate the correct height in some cases (like this pen), so it’s best to use <code>100vh</code> by default and only apply <code>-webkit-fill-available</code> in browsers that need it (e.g., iOS Safari). You can use <code>@supports</code> to work around this for now.</li>
<li><strong>Safari Orientation Bug</strong>: Unfortunately, iOS Safari incorrectly resets the element height on orientation changes. For example, <a href="https://www.dropbox.com/s/f49gsckvyc6uc3e/2020-08-03%20Safari%20orientation%20bug%20with%20fill-available%20fix%20for%20full%20height%20heroes.mp4?dl=0" target="_blank" rel="noopener">see this screencast</a>. I haven't checked for an official bug report yet, but be careful using this in production in the meantime. YMMV.</li>
<li><strong>Box Sizing</strong>: As the W3C spec says (see quote below), fit available (aka “stretch fit”) <em>subtracts</em> the current element’s margin/border/padding, so if you have <code>box-sizing: border-box</code> applied you’ll end up shrinking your element by that much. To fix, simply make your element <code>box-sizing: content-box</code>.
<blockquote>
<dl>
<dt><dfn>stretch fit</dfn></dt>
<dd>The stretch fit into a given size is that size, minus the element’s computed margins (not collapsed, treating <span class="css">auto</span> as zero), border, and padding in the given dimension (such that the outer size is a perfect fit), and flooring at zero (so that the inner size is not negative).
</dd>
</dl>
</blockquote>
</li>
<li><strong>Future Keyword</strong>: This fix should help us for a while, but the keyword “fill-available” has changed in the spec, so when it finally lands in browsers as a web standard it will be called “stretch-fit” instead. Don’t worry about it for now, just expect the change later on.</li>
</ul>
<p>If you’re comfortable with these bugs, here’s some code you can use today:</p>
<code><pre>.hero {
min-height: 100vh;
}
<span class="comment">/* Only apply fix in iOS Safari */</span>
@supports (-webkit-touch-callout: none) {
.hero {
min-height: -webkit-fill-available;
<span class="comment">/* Prevent margin/border/padding from shrinking available space */</span>
box-sizing: content-box;
}
}</pre></code>
</details>
<p>P.S. 👋 Shoutout to <a href="https://github.com/postcss/postcss-100vh-fix#readme" target="_blank" rel="noopener">Sitnick’s PostCSS plugin</a> and <a href="https://allthingssmitty.com/2020/05/11/css-fix-for-100vh-in-mobile-webkit/" target="_blank" rel="noopener">Matt Smith’s article</a> for this fix! Huge, huge improvement on the JS hacks we’ve been using for so long. Thank you!</li>
</div>
</div>
<div class="container" style="margin-bottom: 10rem;">
<h2>References</h2>
<ul>
<li><a href="https://twitter.com/sitnikcode/status/1288097180721377280" target="_blank" rel="noopener">Twitter: Sitnik the Developer: I released a new @PostCSS plugin…</a></li>
<li><a href="https://github.com/postcss/postcss-100vh-fix#readme" target="_blank" rel="noopener">GitHub: PostCSS 100vh Fix</a></li>
<li><a href="https://allthingssmitty.com/2020/05/11/css-fix-for-100vh-in-mobile-webkit/" target="_blank" rel="noopener">Matt Smith: CSS fix for 100vh in mobile WebKit</a></li>
<li><a href="https://caniuse.com/#feat=intrinsic-width" target="_blank" rel="noopener">Can I use… Intrinsic & Extrinsic Sizing</a></li>
<li><a href="https://github.com/w3c/csswg-drafts/commit/c372e95e1438308623941bb28ef0f5e7e31dffb3" target="_blank" rel="noopener">GitHub: w3c/csswg-drafts: [css-sizing] Rename fill-available sizing to stretch-fit sizing</a></li>
<li><a href="https://www.w3.org/TR/css-sizing-3/#stretch-fit" target="_blank" rel="noopener">W3C: CSS Intrinsic & Extrinsic Sizing Module Level 3: Terminology: stretch fit</a></li>
</ul>
</div>
* {
box-sizing: border-box;
}
body {
margin: 0;
line-height: 1.4;
tab-size: 4;
}
pre {
border: 1px solid #00f;
padding: 1rem;
background: white;
overflow: auto;
-webkit-overflow-scroll: touch;
}
/* Use standard viewport units by default */
.hero {
min-height: 100vh;
padding: 1rem 0;
background-color: white;
background-image: linear-gradient(to bottom, white, #aaf 100vh);
}
/* Only apply fix in iOS Safari */
@supports (-webkit-touch-callout: none) {
.hero {
min-height: -webkit-fill-available;
/* Prevent margin/border/padding from shrinking available space */
box-sizing: content-box;
}
}
.hero h1 {
margin: 0;
}
.hero li {
margin: 1rem 0;
}
.container {
display: block;
width: 100%;
margin: 0 auto;
max-width: 42rem;
padding-left: 1rem;
padding-right: 1rem;
}
.comment {
color: #999;
}
Also see: Tab Triggers