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.
<section id="banner">
<div class="textbox animated">
<h1>Absolutely Positioned Element
<small style="display:block; font-size:65%;">That Lines up with left margin of a max-width element.</small>
</h1>
</div>
</section>
<div class="maxwidth">
<p style="background:#fea; padding:8px; border:solid 2px #ea2;"><strong>Updated: Nov. 11, 2021</strong> <a href="#improved">Skip to Best Solution »</a></p>
<p>Adjust the width of this window.</p>
<p>This box has a max-width of <b>500px</b>, while the title above is absolutely positioned.</p>
<p>But they always line up.</p>
<p>This is accomplished by usign <b>CSS Calc</b> to calculate the <b>padding-left</b> of the text box above.</p>
<p>The math is actually pretty simple. We take the full width of the viewport minus the max width, and divide that by two.</p>
<p>Here's what the code looks like...</p>
<code>
padding-left:calc( (100vw - 500px) / 2);
</code>
<p>There's only one tiny problem.</p>
<p>Scroll bars.</p>
<p>They mess up this lovely formula. So you have to remove 8 extra pixels to make it perfect, like this...</p>
<code>
padding-left:calc( (100vw - 500px) / 2 - 8px);
</code>
<hr/>
<h2 id="improved">Improved Solution!!! (Nov. 11, 2021)</h2>
<p>Until now, scroll bars were the Achilles' heel of this code.</p>
<p>Previously you were forced manually remove 8px to account for the standard 17px scrollbar. But adjusting 8px for the scrollbar isn't ideal because you're <b>assuming there <em>will be</em> a 17px wide scrollbar.</b></p>
<p>That assumption is far from certain. Some browsers have floating scroll bars (like smartphones or Safari on MacOS) and short pages (or tall screens) might not have scroll bars at all.</p>
<p> So, here's a better solution!</p>
<p>You can actually get the <b>viewport width minus the scrollbars</b> with the following equation that I found on <a href="https://stackoverflow.com/questions/33606565/is-it-possible-to-calculate-the-viewport-width-vw-without-scrollbar"> Stack Overflow</a>.</p>
<code>width: calc(100vw - (100vw - 100%));</code>
<p>However, this <b>only works if the element you are sizing is a direct child of body, or an element that has the same width as body.</b></p>
<p>That is, unless you use a CSS Variable.</p>
<p>Create a CSS variable on the body, as shown below...</p>
<code>
body {
--screen-width:calc(100vw - (100vw - 100%));
}
</code>
<p>Then use it anywhere it is needed. Here's an example...</p>
<code>
.textbox {
padding-left: calc( ( var(--screen-width) - 500px) / 2);
}
</code>
<p>The code works on pages with scroll bars or without scroll bars, with <em>zero</em> need for javascript.</p>
<p>What do you think? Did I miss anything?</p>
<p>Feel free to comment on this pen with your thoughts.</p>
</div>
<div class="ruler"></div>
html, body {display:block; height:100%; }
body {
margin:0;
font-family:sans-serif;
position:relative;
--screen-width:calc(100vw - (100vw - 100%));
}
*, *:before, *:after { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }
#banner {
height:36vw;
background:url(https://www.launch2success.com/wp-content/uploads/2019/07/measuring-grass.jpg) center #489;
background-size:cover;
color:white;
position:relative;
}
.maxwidth {
max-width:500px;
margin:0 auto;
padding-bottom:16px;
}
.textbox {
position:absolute;
bottom:16px;
padding:8px;
padding-left:calc( (100vw - 500px) / 2 - 8px);
padding-left: calc( ( var(--screen-width) - 500px) / 2);
}
.textbox.animated {
animation:0.75s slideIn ease-out;
background:rgba(0,50,60,0.8);
box-shadow:0 0 1px 1px #fff4;
}
@keyframes slideIn {
0%{ transform:translateX(-100%);}
100%{ }
}
.textbox h1 {margin:0; font-size:1.8em;}
p {line-height:1.35;}
code {
display:block;
background:#444;
color:white;
padding:12px;
white-space: pre-wrap;
}
.ruler {
border-left:dashed 1px #ea9;
position:fixed;
left:calc( (100vw - 500px) / 2 - 10px );
left:calc( ( var(--screen-width) - 500px) / 2 - 1px );
top:0; height:100%;
animation:1s fadeIn 1s both;
}
@keyframes fadeIn {
0% { opacity:0;}
100%{ opacity:1; }
}
Also see: Tab Triggers