🗜 Clamp number
06 Jan, 2021
👋 FYI, this note is over 6 months old. Some of the content may be out of date.
On this page
JavaScript Jump to heading
const clamp = (value: number, min: number, max: number) => {
return Math.min(Math.max(value, min), max)
}
// clamp(21, 20, 50) > 21
// clamp(10, 20, 50) > 20
// clamp(80, 20, 50) > 50
CSS Jump to heading
Use this if you only need to support Safari (11.1+) and Chrome (79+)
html {
font-size: min(max(16px, 4vw), 22px);
}
Or even this if you only support Chrome (79+)
body {
font-size: clamp(16px, 4vw, 22px);
}
← Back home