30秒学会 CSS 片段 · 2022年8月5日

30秒学会 CSS 片段 – Fluid typography

Creates text that scales according to the viewport width.

  • Use the clamp() CSS function to clamp the value of font-size between 1rem and 3rem.
  • Use the formula 8vw - 2rem to calculate a responsive value for font-size (1rem at 600px, 3rem at 1000px).

预览

Hello World!



HTML

<p class="fluid-type">Hello World!</p>

CSS

.fluid-type {
  font-size: clamp(1rem, 8vw - 2rem, 3rem);
}

翻译自:https://www.30secondsofcode.org/css/s/fluid-typography