Generate a typographic scale of CSS variables with any interval (fixed or proportional) and any number of sizes. Just edit $interval
, $body-text
, $scale-min
, and $scale-max
:
:root {
$interval: 1.5; // Unitless for proportional, unit for fixed
$body-text: 1rem; // Must have a unit
$scale-min: -2; // Unitless negative integer
$scale-max: 2; // Unitless positive integer
--int: #{$interval};
--scale0: #{$body-text};
@if $scale-min < 0 {
// Generate scale variables smaller than the body text size
@for $i from -1 through $scale-min {
@if type-of($interval) == number {
@if unitless($interval) {
--scale#{$i}: calc(var(--scale#{$i + 1}) / var(--int));
} @else {
--scale#{$i}: calc(var(--scale#{$i + 1}) - var(--int));
}
}
}
}
@if $scale-max > 0 {
// Generate scale variables larger than the body text size
@for $i from 1 through $scale-max {
@if type-of($interval) == number {
@if unitless($interval) {
--scale#{$i}: calc(var(--scale#{$i - 1}) * var(--int));
} @else {
--scale#{$i}: calc(var(--scale#{$i - 1}) + var(--int));
}
}
}
}
}
The Sass above generates this CSS:
:root {
--int: 1.5;
--scale0: 1rem;
--scale-1: calc(var(--scale0) / var(--int));
--scale-2: calc(var(--scale-1) / var(--int));
--scale1: calc(var(--scale0) * var(--int));
--scale2: calc(var(--scale1) * var(--int));
}
For more details, see my 24 Ways article, A Modern Typographic Scale.
Do you have any insights about how to calculate
line-height
when using this for font sizes?