Creating a modular typography system in CSS creates an extremely reusable font-size system with a harmonious vertical rhythm.
- Choose the typeface for the body copy
- Choose an appropriate font-size; usually 16 px unless the typeface is larger
- Choose an appropriate line-height for the body text
- Choose a type scale, often based on a musical or other natural scale
- Set up the CSS font-sizes, line-heights, and margins to fit the scale
Example type sizes
- font-family: Georgia
- font-size: 16px
- line-height: 24px
- type-scale: 1.125 (major second)
How to choose a type scale
- A List Apart: More Meaningful Typography
- 24 Ways: Composing the New Canon
- R(a|ela)tional Design
- Typographic Scale
Tools
- http://type-scale.com/
- http://modularscale.com/
- ☛ Typografier — A tool I created for myself to generate the default CSS code
I usually start my type-scale with the h6
set at the same font-size as the body copy, 1rem
, then work upwards from there.
To calculate the font-size there’s just some simple multiplication:
font-size: previous-font-size × type-scale
So for the h5
, it would be 1 × 1.125
.
h5 {
font-size: 1.125rem; /* (1 × 1.125) */
}
h6 {
font-size: 1rem;
}
A more general equation would be this:
font-size: base-font-size × type-scale ^ distance-from-base
Using the formula to calculate the h1
:
h1 {
/* (1 × 1.125 ^ 5) - H1 is 5 steps away from the base font-size */
font-size: 1.8020rem;
}
With a font-size system set to a scale we will want to use those font-sizes outside of h1
so we should assign classes to them.
Some designers like to use the Greek alphabet for their font-sizes, some like to use the Metric prefixes. I personally prefer the Metric prefixes because they make sense to me and I don’t know whether alpha is my biggest or smallest font-size.
So, we would then assign classes to all the significant font-sizes:
h1, .exa {
font-size: 1.8020rem;
}
⋮
h6, .kilo {
font-size: 1rem;
}
Often we need larger font-sizes (for banners or hero graphics) and smaller font-sizes (for captions or footnotes). So we should make classes that go above and below the heading sizes:
.yotta {
font-size: 2.2807rem;
}
.zetta {
font-size: 2.0273rem;
}
⋮
small, .milli {
font-size: 0.8889rem;
}
.micro {
font-size: 0.7901rem;
}
Links
With our different font-sizes we now need to set everything to a semi-baseline grid—or at least create a harmonious vertical rhythm.
We start by applying a consistent margin to all the typography related elements:
h1, h2, h3, h4, h5, h6,
p, ul, ol, dl, dd, figure
blockquote, details, hr,
fieldset, pre, table {
margin: 0 0 1.5rem;
}
The margin-bottom is the same size as our line-height.
Next, we assign a line-height to every font-size in our type-scale that aligns with our base line-height.
Here’s the formula to calculate the appropriate line-height:
line-height: ceil(font-size ÷ base-line-height) × (base-line-height ÷ font-size)
To calculate the h1
it would look like this:
h1, .exa {
/* line-height: ceil(1.802 ÷ 1.5) × (1.5 ÷ 1.802) */
font-size: 1.8020rem;
line-height: 1.6648;
}
Links
To keep things consistent throughout our design we want to use a series of consistent bottom margins, usually multiples of the line-height.
So, we can create a series of classes to add consistent margins:
/* Normal, line-height size space */
.push {
margin-bottom: 1.5rem;
}
/* No space */
.push-none {
margin-bottom: 0;
}
/* Double normal space */
.push-double {
margin-bottom: 3rem;
}
/* Half normal space */
.push-half {
margin-bottom: 0.75rem;
}
We might even want to add a series of classes for consistent paddings for closed in boxes so they can conform to our baseline grid. Check out the CSS file for a complete series of these classes.
Often we want a different typeface and colours for the headings, it’s best to separate that into another series of classes for reuse.
h1, h2, h3, h4, h5, h6,
.brand {
font-family: Helvetica,sans-serif;
color: #393;
}
.brand-family {
font-family: Helvetica,sans-serif;
}
.brand-color {
color: #393;
}
Now we can apply these classes to any element to assign them the brand colour or typeface or both.
When starting a new website it’s best to first consider the typography. I generally like to style all the typography related elements to create a mini styleguide.
Sample styleguides
- http://paulrobertlloyd.com/about/styleguide/
- http://barebones.paulrobertlloyd.com/_styleguide.php
- http://clearleft.com/styleguide/