brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: font-size: clamp() in css scales text between a minimum and a maximum

Css
~/tips/tip-css-clamp-fluid-font-size

h1 { font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3rem); }

Sets a value that grows with the window but stays between a lower and an upper limit.

I would rather set fluid type once than add a media query for every breakpoint.

h1 { font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3rem); }
p  { font-size: clamp(1rem, 0.95rem + 0.25vw, 1.125rem); }

clamp(min, preferred, max) returns the preferred value, but never below the minimum or above the maximum. The heading above grows with the viewport between 1.75rem and 3rem. Mix a rem part into the middle value, as shown, so browser zoom and the user’s font setting still have an effect. A middle value in pure vw can ignore zoom, which is an accessibility problem. The same function works for padding, gap and width.

Further reading: MDN: clamp()

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link