title: Fluid type examples
url: http://madebymike.com.au/writing/fluid-type-calc-examples/
hash_url: 7bd69d61367ba820906d99e127802e57

<p>A few months ago I published a <a href="/writing/precise-control-responsive-typography/">fluid type technique</a> that doesn't require any JavaScript. Even though I consider it experimental the technique works well enough as a progressive enhancement and it generated a lot of interest, comments and suggestions. So I thought I'd put together a bunch of examples and address some comments.</p>

<h2>Fluid type with pixels</h2>

<p>This is a simplified version of my original example. The minimum font size is 14px and the maximum is 22px. I've removed a redundant media query and reduced the complexity of the calc() equation.</p>
<div class="highlight"><pre><code class="language-css" data-lang="css"><span class="nc">.fluid-type</span> <span class="p">{</span>
  <span class="k">font-size</span><span class="o">:</span> <span class="m">14px</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">@media</span> <span class="nt">screen</span> <span class="nt">and</span> <span class="o">(</span><span class="nt">min-width</span><span class="o">:</span> <span class="nt">320px</span><span class="o">)</span> <span class="p">{</span>
  <span class="nc">.fluid-type</span> <span class="p">{</span>
    <span class="k">font-size</span><span class="o">:</span> <span class="n">calc</span><span class="p">(</span><span class="m">14px</span> <span class="o">+</span> <span class="m">8</span> <span class="o">*</span> <span class="p">((</span><span class="m">100</span><span class="n">vw</span> <span class="o">-</span> <span class="m">320px</span><span class="p">)</span> <span class="o">/</span> <span class="m">960</span><span class="p">));</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="k">@media</span> <span class="nt">screen</span> <span class="nt">and</span> <span class="o">(</span><span class="nt">min-width</span><span class="o">:</span> <span class="nt">1280px</span><span class="o">)</span> <span class="p">{</span>
  <span class="nc">.fluid-type</span> <span class="p">{</span>
    <span class="k">font-size</span><span class="o">:</span> <span class="m">22px</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<h3>Example</h3>

<p class="example-1">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.</p>

<h2>Fluid type with Rem units</h2>

<p>This example should have the same result as the one above when the base font size is 16px (default).</p>

<p>It shows that the technique works with any length unit, as long as you can use it in a media query. It also addresses comments regarding how my initial example will override user preferences for the default font size.</p>

<p>The only catch is that all unit types must be the same for the calc() equation to work. That's a shame because we often use different unit types for breakpoints in media queries than we do for <code>font-size</code>.</p>
<div class="highlight"><pre><code class="language-css" data-lang="css"><span class="nc">.fluid-type</span> <span class="p">{</span>
  <span class="k">font-size</span><span class="o">:</span> <span class="m">0</span><span class="o">.</span><span class="m">875</span><span class="n">rem</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">@media</span> <span class="nt">screen</span> <span class="nt">and</span> <span class="o">(</span><span class="nt">min-width</span><span class="o">:</span> <span class="nt">20rem</span><span class="o">)</span> <span class="p">{</span>
  <span class="nc">.fluid-type</span> <span class="p">{</span>
    <span class="k">font-size</span><span class="o">:</span> <span class="n">calc</span><span class="p">(</span><span class="m">0</span><span class="o">.</span><span class="m">875</span><span class="n">rem</span> <span class="o">+</span> <span class="m">0</span><span class="o">.</span><span class="m">5</span> <span class="o">*</span> <span class="p">((</span><span class="m">100</span><span class="n">vw</span> <span class="o">-</span> <span class="m">20</span><span class="n">rem</span><span class="p">)</span> <span class="o">/</span> <span class="m">60</span><span class="p">));</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="k">@media</span> <span class="nt">screen</span> <span class="nt">and</span> <span class="o">(</span><span class="nt">min-width</span><span class="o">:</span> <span class="nt">80rem</span><span class="o">)</span> <span class="p">{</span>
  <span class="nc">.fluid-type</span> <span class="p">{</span>
    <span class="k">font-size</span><span class="o">:</span> <span class="m">1</span><span class="o">.</span><span class="m">375</span><span class="n">rem</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<h3>Example</h3>

<p class="example-2">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.</p>

<h2>Reverse fluid type</h2>

<p>In this example the text gets smaller as the viewport gets larger. This might have novel uses or it might not.</p>
<div class="highlight"><pre><code class="language-css" data-lang="css"><span class="nc">.fluid-type</span> <span class="p">{</span>
  <span class="k">font-size</span><span class="o">:</span> <span class="m">22px</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">@media</span> <span class="nt">screen</span> <span class="nt">and</span> <span class="o">(</span><span class="nt">min-width</span><span class="o">:</span> <span class="nt">320px</span><span class="o">)</span> <span class="p">{</span>
  <span class="nc">.fluid-type</span> <span class="p">{</span>
    <span class="k">font-size</span><span class="o">:</span> <span class="n">calc</span><span class="p">(</span><span class="m">22px</span> <span class="o">+</span> <span class="m">-8</span> <span class="o">*</span> <span class="p">((</span><span class="m">100</span><span class="n">vw</span> <span class="o">-</span> <span class="m">320px</span><span class="p">)</span> <span class="o">/</span> <span class="m">960</span><span class="p">));</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="k">@media</span> <span class="nt">screen</span> <span class="nt">and</span> <span class="o">(</span><span class="nt">min-width</span><span class="o">:</span> <span class="nt">1280px</span><span class="o">)</span> <span class="p">{</span>
  <span class="nc">.fluid-type</span> <span class="p">{</span>
    <span class="k">font-size</span><span class="o">:</span> <span class="m">14px</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<h3>Example</h3>

<p class="example-3">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.</p>

<h2>Fluid line-height (Molten leading)</h2>

<p>In this example the line-height is fluid. This is a pure CSS implementation of Wilto's <a href="https://github.com/Wilto/Molten-Leading">Molten leading technique</a>.</p>
<div class="highlight"><pre><code class="language-css" data-lang="css"><span class="nc">.molten-leading</span> <span class="p">{</span>
  <span class="k">line-height</span><span class="o">:</span> <span class="m">1.2em</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">@media</span> <span class="nt">screen</span> <span class="nt">and</span> <span class="o">(</span><span class="nt">min-width</span><span class="o">:</span> <span class="nt">20em</span><span class="o">)</span> <span class="p">{</span>
  <span class="nc">.molten-leading</span> <span class="p">{</span>
    <span class="k">line-height</span><span class="o">:</span> <span class="n">calc</span><span class="p">(</span><span class="m">1.2em</span> <span class="o">+</span> <span class="m">0</span><span class="o">.</span><span class="m">6</span> <span class="o">*</span> <span class="p">((</span><span class="m">100</span><span class="n">vw</span> <span class="o">-</span> <span class="m">20em</span><span class="p">)</span> <span class="o">/</span> <span class="m">60</span><span class="p">));</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="k">@media</span> <span class="nt">screen</span> <span class="nt">and</span> <span class="o">(</span><span class="nt">min-width</span><span class="o">:</span> <span class="nt">80em</span><span class="o">)</span> <span class="p">{</span>
  <span class="nc">.molten-leading</span> <span class="p">{</span>
    <span class="k">line-height</span><span class="o">:</span> <span class="m">1.8em</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<h3>Example</h3>

<p class="example-4">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.</p>

<h2>Fluid box</h2>

<p>This example shows how the technique can be applied to more than just font sizes, in this case <code>width</code>.</p>
<div class="highlight"><pre><code class="language-css" data-lang="css"><span class="nc">.fluid-box</span> <span class="p">{</span>
  <span class="k">width</span><span class="o">:</span> <span class="m">200px</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">@media</span> <span class="nt">screen</span> <span class="nt">and</span> <span class="o">(</span><span class="nt">min-width</span><span class="o">:</span> <span class="nt">320px</span><span class="o">)</span> <span class="p">{</span>
  <span class="nc">.fluid-box</span> <span class="p">{</span>
    <span class="k">width</span><span class="o">:</span> <span class="n">calc</span><span class="p">(</span><span class="m">200px</span> <span class="o">+</span> <span class="m">300</span> <span class="o">*</span> <span class="p">((</span><span class="m">100</span><span class="n">vw</span> <span class="o">-</span> <span class="m">320px</span><span class="p">)</span> <span class="o">/</span> <span class="m">960</span><span class="p">));</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="k">@media</span> <span class="nt">screen</span> <span class="nt">and</span> <span class="o">(</span><span class="nt">min-width</span><span class="o">:</span> <span class="nt">1280px</span><span class="o">)</span> <span class="p">{</span>
  <span class="nc">.fluid-box</span> <span class="p">{</span>
    <span class="k">width</span><span class="o">:</span> <span class="m">500px</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<h3>Example</h3>

<p class="example-5">The width of this box will scale and at a different rate to the viewport.</p>

<h2>Fluid type mixin</h2>

<p><a href="https://twitter.com/indrekpaas">Indrek Paas</a> developed this mixin to help make fluid type using this technique easier. I use a slightly modified version to generate the examples on this page.</p>
<div class="highlight"><pre><code class="language-scss" data-lang="scss"><span class="k">@function</span> <span class="nt">strip-unit</span><span class="o">(</span><span class="err">$</span><span class="nt">value</span><span class="o">)</span> <span class="p">{</span>
  <span class="k">@return</span> <span class="err">$</span><span class="nt">value</span> <span class="o">/</span> <span class="o">(</span><span class="err">$</span><span class="nt">value</span> <span class="o">*</span> <span class="nt">0</span> <span class="o">+</span> <span class="nt">1</span><span class="o">);</span>
<span class="p">}</span>

<span class="k">@mixin</span><span class="nf"> fluid-type</span><span class="p">(</span><span class="nv">$min-vw</span><span class="o">,</span> <span class="nv">$max-vw</span><span class="o">,</span> <span class="nv">$min-font-size</span><span class="o">,</span> <span class="nv">$max-font-size</span><span class="p">)</span> <span class="p">{</span>
  <span class="nv">$u1</span><span class="o">:</span> <span class="nf">unit</span><span class="p">(</span><span class="nv">$min-vw</span><span class="p">);</span>
  <span class="nv">$u2</span><span class="o">:</span> <span class="nf">unit</span><span class="p">(</span><span class="nv">$max-vw</span><span class="p">);</span>
  <span class="nv">$u3</span><span class="o">:</span> <span class="nf">unit</span><span class="p">(</span><span class="nv">$min-font-size</span><span class="p">);</span>
  <span class="nv">$u4</span><span class="o">:</span> <span class="nf">unit</span><span class="p">(</span><span class="nv">$max-font-size</span><span class="p">);</span>

  <span class="k">@if</span> <span class="nv">$u1</span> <span class="o">==</span> <span class="nv">$u2</span> <span class="ow">and</span> <span class="nv">$u1</span> <span class="o">==</span> <span class="nv">$u3</span> <span class="ow">and</span> <span class="nv">$u1</span> <span class="o">==</span> <span class="nv">$u4</span> <span class="p">{</span>
    <span class="k">&amp;</span> <span class="p">{</span>
      <span class="na">font-size</span><span class="o">:</span> <span class="nv">$min-font-size</span><span class="p">;</span>

      <span class="k">@media</span> <span class="nt">screen</span> <span class="nt">and</span> <span class="o">(</span><span class="nt">min-width</span><span class="nd">:</span> <span class="err">$</span><span class="nt">min-vw</span><span class="o">)</span> <span class="p">{</span>
        <span class="na">font-size</span><span class="o">:</span> <span class="nf">calc</span><span class="p">(</span><span class="si">#{</span><span class="nv">$min-font-size</span><span class="si">}</span> <span class="o">+</span> <span class="si">#{</span><span class="nf">strip-unit</span><span class="p">(</span><span class="nv">$max-font-size</span> <span class="o">-</span> <span class="nv">$min-font-size</span><span class="p">)</span><span class="si">}</span> <span class="o">*</span> <span class="p">((</span><span class="mi">100</span><span class="kt">vw</span> <span class="o">-</span> <span class="si">#{</span><span class="nv">$min-vw</span><span class="si">}</span><span class="p">)</span> <span class="o">/</span> <span class="si">#{</span><span class="nf">strip-unit</span><span class="p">(</span><span class="nv">$max-vw</span> <span class="o">-</span> <span class="nv">$min-vw</span><span class="p">)</span><span class="si">}</span><span class="p">));</span>
      <span class="p">}</span>

      <span class="k">@media</span> <span class="nt">screen</span> <span class="nt">and</span> <span class="o">(</span><span class="nt">min-width</span><span class="nd">:</span> <span class="err">$</span><span class="nt">max-vw</span><span class="o">)</span> <span class="p">{</span>
        <span class="na">font-size</span><span class="o">:</span> <span class="nv">$max-font-size</span><span class="p">;</span>
      <span class="p">}</span>
    <span class="p">}</span>
  <span class="p">}</span> <span class="k">@else</span> <span class="p">{</span>
    <span class="k">@error</span> <span class="s2">"Detected mixed units. Please use the same units for all parameters."</span><span class="o">;</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<h3>Example</h3>
<div class="highlight"><pre><code class="language-scss" data-lang="scss"><span class="nc">.fluid-type</span> <span class="p">{</span>
  <span class="k">@include</span><span class="nd"> fluid-type</span><span class="p">(</span><span class="mi">320</span><span class="kt">px</span><span class="o">,</span> <span class="mi">1280</span><span class="kt">px</span><span class="o">,</span> <span class="mi">14</span><span class="kt">px</span><span class="o">,</span> <span class="mi">18</span><span class="kt">px</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div>
<p>So that's it. <a href="https://twitter.com/MikeRiethmuller">Let me know</a> if you have any more examples.</p>
