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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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="page">
<div class="rwd-img-wrapper" style="background-image: url(https://unsplash.it/1600/900)">
<!--<img src="https://unsplash.it/1600/900" alt="Unsplash.it">-->
</div>
<div id="size"></div>
<div class="text-wrapper">
<h1>Fluid Aspect Ratio</h2>
<p>Move from a minimum viewport width of 320px and an aspect ratio of 16:9, to a maximum viewport of 1200px and an aspect ratio of 5:1.</p>
<h4><b>Targets:</b> At 320px wide, with an aspect of 16:9 the container should be 320 x 180px.<br>
At 1200px wide, with an aspect of 4:1 the container should be 1200 x 300px.</h4>
<h3>The problem: </h3>
<p>A small viewport demands an image that is more rectangular — and we typically use the 16:9 aspect ratio for 320px wide screens. Wider viewports, however, need an image size that is less tall in order that content can remain visible. Since most laptops have a 16:9 widescreen aspect ratio, we need something even more dramatic. Here, I target a 4:1 ratio for 1200px wide screens. </p>
<h3>The goal:</h3>
<p>Move fluidly from the 16:9 aspect ratio up to a 4:1 aspect ratio without using multiple media queries, resulting in a “stepped” effect when the media query abruptly changes the height of the containing element. </p>
<h3>The solution:</h3>
<p>CSS’s new <code>calc()</code> function can use dynamic units like viewport units to create a measurement that constantly changes. Thanks you, thank you, thank you to <a href="https://twitter.com/timbrown">Tim Brown</a> for his <a href="https://blog.typekit.com/2016/08/17/flexible-typography-with-css-locks/">in-depth article and crazy math around fluid line-heights</a>.</p>
<h2>How does this work?</h2>
<pre><code>min-height: 180px;
height: calc(180px + (300 - 180) * ((100vw - 320px)/(1200 - 320)));
max-height: 300px;</code></pre>
<p>Think of it as portions to the left and right of the multiplication sign — left is heights, right is widths:</p>
<ul>
<li><b>180px</b> is our minimum height at our smallest viewport width</li>
<li><b>(300 - 180)</b> is the difference between our max-height and our min-height for the image container</li>
<li><b>(100vw - 320px)</b> is the viewport width minus the smallest breakpoint width. We use <code>px</code> here so that the result resolves to a unit</li>
<li><b>(1200 - 320)</b> is the maximum viewport width minus the minimum viewport width</li>
</ul>
<p>If I were to break it down into <abbr title="Syntactically Awesome Style Sheets">SASS</abbr> variables, it might look something like this:</p>
<pre><code>height: calc($min-height + ($max-height - $min-height) * ((100vw - $min-width)/($max-width - $min-width)))</code></pre>
<p>And further, if I were to break this down into reusable <abbr title="Syntactically Awesome Style Sheets">SASS</abbr> logic:</p>
<pre><code>$min-width: 320px;
$min-aspect: 16 9;
$min-height: $min-width * (nth($min-aspect, 2) / nth($min-aspect, 1));
$max-width: 1200px;
$max-aspect: 4 1;
$max-height: $max-width * (nth($max-aspect, 2) / nth($max-aspect, 1));
.element {
min-height: $min-height;
height: calc(#{$min-height} + (#{$max-height} - #{$min-height}) * ((100vw - #{$min-width})/(#{$max-width} - #{$min-width})));
max-height: $max-height;
}</code></pre>
<p>Again, big thanks to <a href="https://twitter.com/timbrown">Tim Brown</a> for the start on this crazy calculus.<br>
Authored by <a href="https://twitter.com/artinruins">J. Hogue</a> at <a href="http://www.oomphinc.com">Oomph, Inc.</a></p>
</div>
</div>
$min-width: 320;
$min-aspect: 16 9;
$min-height: $min-width * (nth($min-aspect, 2) / nth($min-aspect, 1));
$max-width: 1200;
$max-aspect: 4 1;
$max-height: $max-width * (nth($max-aspect, 2) / nth($max-aspect, 1));
.page {
min-width: ($min-width * 1px);
max-width: ($max-width * 1px);
margin: 0 auto;
}
.rwd-img-wrapper {
//background-color: grey;
background: center no-repeat transparent;
background-size: cover;
min-height: ($min-height * 1px);
height: calc(#{($min-height * 1px)} + (#{$max-height} - #{$min-height}) * ((100vw - #{($min-width) * 1px})/(#{$max-width} - #{$min-width})));
max-height: ($max-height * 1px);
overflow: hidden;
position: relative;
width: 100%;
z-index: 1;
}
//.rwd-img-wrapper img {
// height: auto;
// position: absolute;
// top: 50%; left: 0;
// -webkit-transform: translateY(-50%);
// transform: translateY(-50%);
// width: 100%;
// z-index: 2;
//}
#size {
background-color: red;
color: white;
display: inline-block;
padding: .25em 1em;
}
.text-wrapper {
margin: 0 auto;
max-width: 960px;
padding: 1em 2em 3em;
}
$(window).on('resize', showSize);
showSize();
function showSize() {
var $elem = $('.rwd-img-wrapper');
$('#size').html( $elem.innerWidth() + ' x ' + $elem.innerHeight());
}
Also see: Tab Triggers