In my previous article I used the maps feature of Sass to create a simple interface that can DRY up the creation of colour variations and help to bridge the gap between developers and designers. This article covers a similar methodology for a designer’s favourite but many developer’s nightmare; managing typographic rhythm consistently across different screen sizes.
The first step is to define a set of named breakpoints and a simple interface for using them. You may already have a similar set of variables, functions and mixins in your codebase but if not then this is a serviceable implementation:
The second step is the key to this technique and it’s just another map. The map of text sizes gives a name to each size, each size defines breakpoints and each breakpoint contains the font-size and line-height properties. Only the breakpoints that will change need to be defined.
If the base font size—set on the document element or body—will change then it’s best to avoid using em units as calculating other font sizes against a changing baseline would cause unnecessary complexity. The font sizes and line heights used below are from A More Modern Scale for Web Typography.
The text sizes map should be easy to read and modify but because of its size it’s unwieldy to use. The addition of a few functions to access individual text sizes and breakpoints makes it easier to consume:
The functions are only building blocks for the mixins below and probably won’t be used outside of them. There are two mixins; one for a singular font size and line height for a given breakpoint and a second that will loop over the requested text size and output all its breakpoints. The latter also takes an argument for the default breakpoint, which in the example is set to the narrowest size because we should be building mobile first.
The mixins are not very complex, there are no crazy formulas. The actual implementation is not particularly important but what is important is that it creates a really readable interface to use throughout the rest of the project:
I’ve worked on so many codebases littered with different font sizes and they’re hard to manage consistently when building and maintaining a responsive site. This technique sandboxes the problem in an easily understandable way. Let me know what you think about my solution to responsive typography in the comments below or check out an example.
I'm a 28 year old front-end developer building new stuff at the Financial Times based in London. I specialise in crafting scalable, performance-driven code, tackle accessibility issues and keep an opinionated interest in the latest hotness. I like my tea robustly brewed, white and with no sugar, thanks!