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.
<!-- Edited 7/11/2019 - Just a tidy up -->
<div class=content>
<h1><a target=_blank title="[new window]" href="https://websemantics.uk/articles/responsive-fonts/">Responsive fonts - auto-scaling the root font-size</a></h1>
<p>The root font-size scales up and down proportionately, between defined values, dependent upon the viewport width.</p>
<h2>Easy-peasy, stretch & squeezy.</h2>
<p class="new floatright">Simplify your calculations:<br><a target=_blank title="[new window]" href="https://websemantics.uk/tools/responsive-font-calculator/">Fluid-responsive font-size calculator</a></p>
<p>In this example the font size is set at 1rem (16px) up to 48em (768px) viewport width. It then starts increasing to meet the upper defined value 2rem (32px) at 120em (1920px) wide.</p>
<p id=report1></p>
<p>All controlled from a single CSS statement.</p>
<p>Remember to define all font-sizes in em, rem or percent.</p>
<pre><code class=language-css>
<span class=comment>/* 1rem @ 48em (768px) increasing to 2rem @ 120em (1920px) */</span>
@media (min-width: 48em) {
:root {
font-size: calc(1rem + ((1vw - .48rem) * 1.389));
<span class=comment>/* .48rem = viewportWidthMinimum /100 */</span>
<span class=comment>/* 1.389rem = 100 * fontSizeDifference / viewportWidthDifference */</span>
}
}
</code></pre>
<p class=margin2>Where:</p>
<pre>
<b>fontSizeCalc = 1rem + (1vw - 48em / 100) * 100 * fontSizeDifference / viewportWidthDifference</b>
fontSizeDifference = maxFontSize - minFontSize
= 2em - 1em ≡ 32px - 16px
= 1em ≡ 16px
viewportWidthDifference = viewportMax - viewportMin
= 120em - 48em ≡ 1920px - 768px
= 72em ≡ 1152px
Using pixels:
fontSizeCalc = 1rem + (1vw - 768px / 100) * 100 * 16px / 1152px
= 1rem + (1vw - 7.68px) * 1.389
Using em or rem:
fontSizeCalc = 1rem + (1vw - 48rem / 100) * 100 * 1em / 72em
= 1rem + (1vw - .48rem) * 1.389
</pre>
<p class=margin2>Font scaling doesn't stop at the top setting but continues to increment at the same rate. This behaviour may be stopped, or adjusted further, by adding another media query:</p>
<pre><code class=language-css>
<span class=comment>/* Stop font scaling above 1920px */</span>
@media (min-width: 120em) {
:root {
font-size: 2rem;
}
}
</code></pre>
<div class=new>Try the: <a target=_blank title="[new window]" href="https://websemantics.uk/tools/responsive-font-calculator/">Fluid-responsive font-size calculator</a></div>
<p>Tested on Mac and PC: Firefox, Safari, Chrome all behave.<br>One or two tweaks applied for IE.</p>
<h2>Examples:</h2>
<p>Demo: <strong><a target=_blank title="[new window]" href="https://websemantics.uk/articles/responsive-fonts/demo2/">Responsive fonts applied to a typical layout</a></strong>.</p>
<p>Live usage in certain modules which appeared in Tesco's <strong><a target=_blank title="[new window]" href="https://websemantics.uk/portfolio/2017/food-love-stories/">Food Love Stories</a></strong> in 2017 (headings, intro copy and 'More Stories' titles).</p>
</div>
<!-- Footer include -->
[[[https://codepen.io/2kool2/pen/mKeeGM]]]
/* Generic styling */
html {
/* font-size is 100% (16px | 1em) - which is default so unstated */
}
/* Generic styling */
body {
/* template overrides */
font-size: 1rem;
line-height: 1.5;
max-width: none;
padding: 1.618em 1rem 1rem;
}
h1 {margin:0}
h2 {margin: 2em 0 .5em;}
h3 {margin:0 0 .125em}
pre {
color: #ddd;
background-color: #1a1a1a;
padding:0 1rem 1rem;
overflow-x: scroll;
}
b {
color:#fff;
font-weight:300;
}
.comment {
color:#9a9a9a;
font-weight:100;
}
sup {
font-size:inherit;
margin-right:.125em;
}
.content {
margin: 0 auto;
max-width: 36rem;
}
.strap {font-size:larger}
[id^="report"] {color:#ed0}
[id="report1"] {
clear: both;
background-color: #1a1a1a;
padding: .382rem .618rem;
text-align: center;
}
.smaller {
margin: 1rem auto;
max-width: 34rem;
}
.new {
border:1px solid red;
padding: .618rem;
text-align: center;
margin: 1rem;
}
@media (min-width: 40em) {
.floatright {
float: right;
margin: 0.382rem 0 1rem .618rem;
}
}
.margin2 {
margin: 2rem 0;
}
/* The business end: */
/* This example begins from 320px | 20em to 30px @ 1920px */
@media (min-width: 20em) {
:root {
/* font-size: calc(1rem + ((1vw - .2em) / 1.37142857)); */
}
}
/* 1rem(16px) @ 48em(768px) increasing to 2rem(32px) @ 120em(1920px) */
@media (min-width: 48em) {
:root {
font-size: calc(1rem + ((1vw - 0.48rem) * 1.3889));
/* Where: * 1.3889 = 100 * font_Size_Difference / viewport_Width_Difference */
}
}
/* Prevent font scaling beyond this breakpoint */
@media (min-width: 120em) {
:root {
font-size: 2em;
/* Setting font-size to a fixed value */
}
}
var supportsES6 = function() {
// https://gist.github.com/bendc/d7f3dbc83d0f65ca0433caf90378cd95
try {
new Function("(a = 0) => a");
return true;
}
catch (err) {
return false;
}
}();
// Reports current viewport width and font-size
function reporting(reportId, w, d) {
"use strict";
if (!supportsES6) return false;
const report = d.getElementById(reportId);
if (!report) return false;
const width = w.innerWidth;
const fontSize = w.getComputedStyle(report, null).getPropertyValue('font-size');
report.innerHTML = `Current font-size: <b>${fontSize}</b> @ <b>${parseInt(width, 10)}px</b> viewport width`;
w.requestAnimationFrame(function(){
reporting(reportId, w, d);
});
}
reporting("report1", window, document);
Also see: Tab Triggers