A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.md 17KB

4 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. title: Fluid type examples
  2. url: http://madebymike.com.au/writing/fluid-type-calc-examples/
  3. hash_url: 7bd69d61367ba820906d99e127802e57
  4. <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>
  5. <h2>Fluid type with pixels</h2>
  6. <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>
  7. <div class="highlight"><pre><code class="language-css" data-lang="css"><span class="nc">.fluid-type</span> <span class="p">{</span>
  8. <span class="k">font-size</span><span class="o">:</span> <span class="m">14px</span><span class="p">;</span>
  9. <span class="p">}</span>
  10. <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>
  11. <span class="nc">.fluid-type</span> <span class="p">{</span>
  12. <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>
  13. <span class="p">}</span>
  14. <span class="p">}</span>
  15. <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>
  16. <span class="nc">.fluid-type</span> <span class="p">{</span>
  17. <span class="k">font-size</span><span class="o">:</span> <span class="m">22px</span><span class="p">;</span>
  18. <span class="p">}</span>
  19. <span class="p">}</span>
  20. </code></pre></div>
  21. <h3>Example</h3>
  22. <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>
  23. <h2>Fluid type with Rem units</h2>
  24. <p>This example should have the same result as the one above when the base font size is 16px (default).</p>
  25. <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>
  26. <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>
  27. <div class="highlight"><pre><code class="language-css" data-lang="css"><span class="nc">.fluid-type</span> <span class="p">{</span>
  28. <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>
  29. <span class="p">}</span>
  30. <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>
  31. <span class="nc">.fluid-type</span> <span class="p">{</span>
  32. <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>
  33. <span class="p">}</span>
  34. <span class="p">}</span>
  35. <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>
  36. <span class="nc">.fluid-type</span> <span class="p">{</span>
  37. <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>
  38. <span class="p">}</span>
  39. <span class="p">}</span>
  40. </code></pre></div>
  41. <h3>Example</h3>
  42. <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>
  43. <h2>Reverse fluid type</h2>
  44. <p>In this example the text gets smaller as the viewport gets larger. This might have novel uses or it might not.</p>
  45. <div class="highlight"><pre><code class="language-css" data-lang="css"><span class="nc">.fluid-type</span> <span class="p">{</span>
  46. <span class="k">font-size</span><span class="o">:</span> <span class="m">22px</span><span class="p">;</span>
  47. <span class="p">}</span>
  48. <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>
  49. <span class="nc">.fluid-type</span> <span class="p">{</span>
  50. <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>
  51. <span class="p">}</span>
  52. <span class="p">}</span>
  53. <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>
  54. <span class="nc">.fluid-type</span> <span class="p">{</span>
  55. <span class="k">font-size</span><span class="o">:</span> <span class="m">14px</span><span class="p">;</span>
  56. <span class="p">}</span>
  57. <span class="p">}</span>
  58. </code></pre></div>
  59. <h3>Example</h3>
  60. <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>
  61. <h2>Fluid line-height (Molten leading)</h2>
  62. <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>
  63. <div class="highlight"><pre><code class="language-css" data-lang="css"><span class="nc">.molten-leading</span> <span class="p">{</span>
  64. <span class="k">line-height</span><span class="o">:</span> <span class="m">1.2em</span><span class="p">;</span>
  65. <span class="p">}</span>
  66. <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>
  67. <span class="nc">.molten-leading</span> <span class="p">{</span>
  68. <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>
  69. <span class="p">}</span>
  70. <span class="p">}</span>
  71. <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>
  72. <span class="nc">.molten-leading</span> <span class="p">{</span>
  73. <span class="k">line-height</span><span class="o">:</span> <span class="m">1.8em</span><span class="p">;</span>
  74. <span class="p">}</span>
  75. <span class="p">}</span>
  76. </code></pre></div>
  77. <h3>Example</h3>
  78. <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>
  79. <h2>Fluid box</h2>
  80. <p>This example shows how the technique can be applied to more than just font sizes, in this case <code>width</code>.</p>
  81. <div class="highlight"><pre><code class="language-css" data-lang="css"><span class="nc">.fluid-box</span> <span class="p">{</span>
  82. <span class="k">width</span><span class="o">:</span> <span class="m">200px</span><span class="p">;</span>
  83. <span class="p">}</span>
  84. <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>
  85. <span class="nc">.fluid-box</span> <span class="p">{</span>
  86. <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>
  87. <span class="p">}</span>
  88. <span class="p">}</span>
  89. <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>
  90. <span class="nc">.fluid-box</span> <span class="p">{</span>
  91. <span class="k">width</span><span class="o">:</span> <span class="m">500px</span><span class="p">;</span>
  92. <span class="p">}</span>
  93. <span class="p">}</span>
  94. </code></pre></div>
  95. <h3>Example</h3>
  96. <p class="example-5">The width of this box will scale and at a different rate to the viewport.</p>
  97. <h2>Fluid type mixin</h2>
  98. <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>
  99. <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>
  100. <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>
  101. <span class="p">}</span>
  102. <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>
  103. <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>
  104. <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>
  105. <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>
  106. <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>
  107. <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>
  108. <span class="k">&amp;</span> <span class="p">{</span>
  109. <span class="na">font-size</span><span class="o">:</span> <span class="nv">$min-font-size</span><span class="p">;</span>
  110. <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>
  111. <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>
  112. <span class="p">}</span>
  113. <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>
  114. <span class="na">font-size</span><span class="o">:</span> <span class="nv">$max-font-size</span><span class="p">;</span>
  115. <span class="p">}</span>
  116. <span class="p">}</span>
  117. <span class="p">}</span> <span class="k">@else</span> <span class="p">{</span>
  118. <span class="k">@error</span> <span class="s2">"Detected mixed units. Please use the same units for all parameters."</span><span class="o">;</span>
  119. <span class="p">}</span>
  120. <span class="p">}</span>
  121. </code></pre></div>
  122. <h3>Example</h3>
  123. <div class="highlight"><pre><code class="language-scss" data-lang="scss"><span class="nc">.fluid-type</span> <span class="p">{</span>
  124. <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>
  125. <span class="p">}</span>
  126. </code></pre></div>
  127. <p>So that's it. <a href="https://twitter.com/MikeRiethmuller">Let me know</a> if you have any more examples.</p>