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.
@jonathanstegall Using unitless values for
line-height
will get you pretty far, since you can set it once and it will be proportional to thefont-size
. So when thefont-size
changes, theline-height
doesn’t need to:This is not to say that all type in all contexts should use the same proportional line-height, which an excellent post by Frank Chimero touched on recently. So, if you want
line-height
proportions to track with your typographic scale, you could use the scale-building techniques outlined in my 24 Ways article with just the interval, no rems:As you may have noticed, only a few of the values in that scale are really viable as
line-height
values, so this approach generally works better with a smaller interval. That said, chances are you won’t need more than two or threeline-height
proportions for whatever you’re doing.