Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- 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 &amp; 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  &equiv;  32px - 16px
                   = 1em  &equiv;  16px

viewportWidthDifference = viewportMax - viewportMin
                        = 120em - 48em  &equiv;  1920px - 768px
                        = 72em  &equiv;  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]]]
              
            
!

CSS

              
                /* 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 */
  }
}
              
            
!

JS

              
                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);

              
            
!
999px

Console