css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class=content>

  <h1><a target=_blank title="[new window]"  href="https://websemantics.uk/articles/responsive-fonts/">Responsive fonts - auto-scaling root font-size</a></h1>

  <p>The root font size scales up and down proportionately, between defined values, dependent upon the viewport width.</p>
  
  <div class=new>To make your life a lot easier try out the:<br><a class=calculator target=_blank title="[new window]"  href="https://websemantics.uk/tools/responsive-font-calculator/">Fluid-responsive font-size calculator</a></div>

  <h2>Easy-peasy, stretch &amp; squeezy.</h2>

  <p>In this example the font size is set at 1rem (16px) up to 48em (768px) viewport width.<br>
    It then starts increasing to meet the upper defined value 2rem (32px) at 120em (1920px) wide.<br>
    All controlled by 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>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>Font scaling doesn't stop at the top setting but continues to increment at the same rate.<br>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>

<!--
  <h3 style="margin-top:2rem">Applied to a golden-ratio Flexbox layout</h3>
  
  <p>This method will also scale any object which is sized using em or rem.</p>

</div>

<div class=flex>
  <div class=flex_m></div>
  <div class=flex_l id=test2>
    <div class=flex_txt>
      <h3>@ 48em<sup>+</sup> (768px<sup>+</sup>)</h3>
      <p>This text begins to scale-up starting at the minimum font-size 16px and proportionately increasing to 32px as the viewport is stretched to 120em (1920px).</p>
      <div id=report2></div>
    </div>
  </div>
  <div class=flex_c>&nbsp;</div>
  <div class=flex_r id=test3>
    <div class=flex_txt>
      <h3>@ 48em<sup>+</sup> (768px<sup>+</sup>)</h3>
      <p>This text starts at the minimum font-size 14.55px and scales up to 29.12px as the viewport is stretched to 120em (1920px).</p>
      <div id=report3></div>
    </div>
  </div>
  <div class=flex_m></div>
</div>
-->
<!--
<div class=content>

<h3>Why a golden-ratio example?</h3>

  <p>It's probably the most difficult fluid grid to achieve with any accuracy.<br>The value scale is awkward to say the least:<br> [0.09, 0.146, 0.236, 0.382, 0.618, 1, 1.618, 2.618, 4.236, &hellip;]</p>

  <p>In the example above @ 48em<sup>+</sup> (768px<sup>+</sup>) it consistently has:<p>
  <ul>
    <li>Left and right margins of 1.618rem (purple).
    <li>Centre space separator of 2.618rem (green).
    <li>Left column (blue) is always 1.618 * the width of the right column (red).</li>
    <li>Right column font-size is set as (1 - 0.09) * 1rem.</li>
    <li>Padding for each text-area is set at 1em which also scales beautifully.</li>
    <li>For readability; text widths are limited to 34em.</li>
  </ul>

</div>
-->

<p class=smaller><a target=_blank title="[new window]" href="https://codepen.io/2kool2/pens/public/?grid_type=list#">Pens by Mike Foskett</a> &mdash; <a target=_blank title="[new window]" href="https://websemantics.uk/">webSemantics</a></p>

<div id=report1></div>
            
          
!
            
              /* Generic styling */
html {
  /* font-size is 100% (16px | 1em) - which is default so unstated */
}

/* Generic styling */
body {
  /* template overrides */
  font-size: 1rem;
  max-width: none;
  padding: 1.618em 0 1rem;
}
h1 {margin:0}
h2 {
  margin:2em auto .5em;
  max-width: 35.236em;
}
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;
  padding: 0 1rem;
}
.strap {font-size:larger}
[id^="report"] {color:#ed0}
[id="report1"] {
  position: fixed;
  width: 100%;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: #1a1a1a;
  text-indent: .618rem
}
.smaller {
  margin: 1rem auto;
  max-width: 34rem;
}
.new {
  border:1px solid red;
  padding: 1rem;
  text-align: center;
}
.calculator {
  font-size:larger;
  padding-top: .5rem;
  display: inline-block;
}


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


/* layout using golden ratio starts at 768px | 48rem */

.flex {
  margin: 1.618rem .618rem;
  color: #fff;
}

/* Do not apply padding or margins to flexed containers */
.flex_m {background-color: rgba(255,160,255,.3);}
.flex_l {background-color: rgba(85,160,255,.3);}
.flex_c {background-color: rgba(160,255,85,.3);}
.flex_r {background-color: rgba(255,85,160,.3);}

.flex_txt {
  padding: 1em; /* note em */
  max-width: 36rem; /* for reading length only */
  margin: 0 auto;
}
.flex_m {
  text-align: center;
}
@media (max-width:47.999em) {
  .flex_m::before {
    content: "Below 48rem viewport width; columns are stacked, font-size at 16px";
    color: #bbb;
    padding: .125rem 0;
    display: inline-block;
  }
}

@media (min-width: 48em) {
  .flex {
    display: flex;
    margin: 1.618rem 0;
  }
  .flex_m {flex: 0.09;}
  .flex_l {
    flex: 1.618;
    max-width: 36rem; /* note rem */
  }
  .flex_c {flex: 0.146;}
  .flex_r {
    flex: 1;
    font-size: 0.91rem;
    max-width: 36em; /* note em */
  }
}

.warnFirefox {
  display: none;
}
.isFirefox .warnFirefox {
  display: block;
  font-weight: bold;
  text-align: center;
}
            
          
!
            
              
var isFirefox = navigator.userAgent.match("Mozilla/5.0 ") && navigator.userAgent.match(" Gecko/20100101 Firefox/");
// FF DEV mac: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:58.0) Gecko/20100101 Firefox/58.0
// Normal FF mac: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Firefox/52.0
if (isFirefox) {
  document.documentElement.className += " isFirefox";
}


function reporting(testId, reportId, w, d) {
  // Echoes the width and font-size of testId inside reportId
  "use strict";
  var test = d.getElementById(testId);
  var report = d.getElementById(reportId);
  if (test && report) {
    var width = test.getBoundingClientRect().width;
    var fontSize = w.getComputedStyle(test, null).getPropertyValue('font-size');
    report.innerHTML = "Width: " + parseInt(width, 10) + "px" + " &mdash; font: " + fontSize;
    w.requestAnimationFrame(function(){
      reporting(testId, reportId, w, d);
    });
  }
}

reporting("report1", "report1", window, document);
reporting("test2", "report2", window, document);
reporting("test3", "report3", window, document);

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console