Main menu

Questing for the holy grail of web typography: a baseline, cap height, and more in CSS.

Typesetting on the web

Let’s be honest – setting typography on the web has always been clunky. CSS properties are relatively crude alongside the precise control and ease of use that applications like Photoshop and InDesign offer.

While developers have risen to the challenge, approaches to tooling have varied significantly. There has been little confidence or consensus amongst the community towards a tool that brings developers a similar level of control or transferability from design into development.

Problems with web typography arise primarily because of the zany way text is rendered on the web. The most frustrating feature of web typography is perhaps the inexplicable disparity between the way typographers have measured leading for hundreds of years – from one baseline to the next – and the way the modern web spec evolved in just the last two decades. This gave us the bizarre ‘spooky box’, formed by an unholy mating of CSS font-size and line-height.

Unfortunately, apart from a lacklustre vertical-align property, there is little concept of the typographic baseline in the browser.

Without a defined baseline on our type, we’re already a step away from defining any kind of meaningful grid on which to set our type. A recent post by Robin Rendleasked web designers and developers to consider a new standard for web typography. Well, it’s about time, I’d say. So let’s get to it.

We've created a typographic tool for the web that we like to call MegaType. We'll explain how we got there, and give you a short introduction to its use.

Why grids, why?

The use of grids is not an invention of the web. Geometric grids have been utilised by designers for centuries to provide structure and harmony to layouts. At its most fundamental level, grids serve to aid the user in the act of reading. It’s easy for the eye to traverse a document with a strong implied grid and rhythm.

Nowadays, it is commonly said that ‘the web is 95% typography’. Therefore, it follows that our grids should hold as much importance on the web as they ever have.

A typographic specification

Here's a recent artefact we’ve started producing as a cross-discipline communique from designers to developers; a typographic specification.

This is a precise descriptor of the structure of typography, spanning a range of responsive breakpoints on a site. It has become a very useful document at the early stages of development. It gives developers a clear starting point when setting typographic defaults, which if unscoped (a contentious practise itself, but that’s another story), lays the foundation of a website. As you can see, the aforementioned spooky box is absent in this spec – it just isn’t a useful or conceivable dimension in design and thus only serves as a blocker when we reach development.

We’ve come to consider that it could be useful if our tooling would eliminate this disparity, effectively allowing us to lay out type in the same language as our designers.

Best practises

Before we dive in, it’s worth getting a few assumptions out of the way about how we write responsive websites in 2016. Here’s the recommendations we usually follow.

Type should be set in relative units (ems or rems). We build responsive sites and can benefit from the ability to scale layout based on parents or root-sizes if desired.

Pixels? Never. Reserve them for things that should be measured exactly, eg; a 1px hairline.

It is technically most correct to set line-height with unitless numbers. This makes the inheritance of this value more predictable.

Even when leaning on a tool, the resulting output CSS should be as predictable as possible. Long story short - we prefer to avoid strange, unconventional implementations where possible.

When using a tool, it would be useful to have at least one spacing property to fall back on that isn't already used without destroying our grid in unpredictable ways.

Handling spacing

When designing our system, the most important step is to choose a regime for spacing and stick to it. So what considerations do we need to keep in mind?

Back in 2011, Harry Roberts introduced the concept of a Magic Number that dictated much of our type decisions throughout the web. These days, a similar effect can be achieved with much less effort by relying on rem units for typography and any layout elements that should scale alongside it. Many of the type systems that follow represent an evolution of this technique.

In another post by Harry Roberts, he suggests we eliminate the concern over collapsing margins - it’s just one less thing to worry about. This seems especially sensible when we consider that the elimination of the spooky box could likely rely on the use of negative margins, which tend to collapse in a very unintuitive manner.

One further possibility is the use of relative positioning to shift our type to sit on a baseline. As this doesn't affect the layout of other elements (which is, potentially, quite an attractive quirk), we will have to remember to factor this in when calculating the rest of our spacing.

A useful debugging mechanism is something else to keep in mind – we want to be able to visualise our spacing and baseline grid when we’re developing. And if our goal is to eliminate the spooky box, it could be nice to visualise the ‘virtual’ boxes we’re creating when we use these mixins.

Learn from the past

There are many tools out there for setting type on the web. The tools I’ve always been most interested in are those that seek to solve the problem of a typographic baseline grid.

For many years soon after the advent of CSS preprocessors, Compass was probably the most widely used tool for solving the needs of vendor prefixing. It’s also largely responsible for cementing the term ‘Vertical Rhythm’ in the minds of most web developers.

Nowadays, with the prevalence of shiny tools like postcss, it’s easy to forget that, at the time, this was a pretty handy library, and many developers first cut their teeth on baseline grids using its vertical rhythm mixins. A simple implementation might look like so:

$base-font-size: 16px; // this is the grid leading
@include establish-baseline; // initialise the grid
h1 {
@include adjust-font-size-to(20px, 2); // 20px font-size with line-height at 2 units of baseline
@include leader(1); // one line of space above
@include trailer(2); // one line of space below
}

That’s about all the config you’d need to get started. The beauty of this system was that it required barely any config to get up and running, and the mixins all produced very predictable output.

Type would flow down the page in an orderly fashion to a grid, but floated vertically in the middle of the baseline units specified - instead of sitting neatly on the baseline itself. Therefore, problems arose if you wanted to set type in columns adjacent to each other as their baselines would not line up. Thus, the simplicity of this system would ultimately lead to its downfall in the eyes of many typography geeks.

Computing cap height

More recently, Jake Giltsoff (from Typekit) released an innovative approach called Sassline. Digging into the config for this tool, there’s a SASS map for each typeface that stores a bunch of typographic information. One key in particular caught my eye:

Enter cap-height; the missing piece of the puzzle that lets us abolish the ‘spooky box’ once and for all. This excellent blog post by Razvan Onofrei delves deeper into the subject, and appears to be the inspiration for this feature in Sassline.

Utilising this concept, the calculation to give us the height of our spooky-boxless type is pretty simple:

cap-height * computed line-height

In Sassline, this is utilised by another mixin, which for those more familiar with the predictable and modular mixins of Compass might seem a little overbearing:

This allows us to set type perfectly on the baseline by telling the mixin what typeface you are using so that it can intelligently generate offsets based on its stored cap-height. Amazing!

But now we have this cluster of properties to define. Do we really need all of them? Let’s break it down.

Setting font-size, line-height, and general spacing with a $below parameter (and/or an $above parameter) are central to our goal. We need the information about the cap-height for calculating our baseline, so some kind of parameter must exist to tell our mixin about this, so $font stays. However, it seems unintuitive to declare the breakpoint we’re setting as a parameter. With other properties, we would normally set the breakpoint by wrapping them in a media query or some kind of breakpoint helper mixin.

Sassline does it this way because it needs to know a number of config values for the target breakpoint, but instead we can gain this value by accessing a contextual variable inside a custom mixin. Assuming we have a config to store some breakpoint information, and some supporting mixins and functions to retrieve it, let’s give this a go:

This lets us write a type config alongside a breakpoint config so that our mixins will always have access to their breakpoint context. With this setup, there’s no longer any reason for us to use a $breakpoint parameter.

One interesting aspect of Siegmeyer 2 took inspiration from a recent demo by Mike Riethmuller, implemented as an optional setting, aptly named type-scaling. This uses viewport units and calc() to scale the root font size – and thus, the entire baseline grid – smoothly between values set at different breakpoints.

A lot of thought was given to building modular code that would operate similarly to the Compass leader and trailer mixins. Unfortunately, this falls apart, as we need to know the cap-height, font-size and line-height in order to make any meaningful spacing calculations on an element.

We're also aware of the fact that Siegmeyer 2 was built as a boilerplate rather than a modular component. This makes it difficult to benefit from features or bug fixes if they’re added later down the track, and exposes a lot of complexity and clutter that we don’t necessarily want developers to worry about.

First steps

A few years ago, I made my first attempt at solving the problem of the spooky box.

Inspired by the setup behind Sassline, and leveraging the responsive layout tools provided by Susy 2, I put together something that gives us a way to work where the spooky box doesn’t factor into our development. I called this Siegmeyer 2:

I'm not going to delve too deep into the config for this, but, similar to Sassline, Siegmeyer 2 had a fairly heavy config. For setting type we had a very Sassliney looking mixin;

This would apply the font-size, the line-height, and compensate for the spooky box using a combination of padding and margin. And then controversially apply top and bottom spacing using borders.

After a few projects, I began to think that perhaps we could do even better.

Prototyping MegaType

I'd like to introduce MegaType, which - we believe - solves these problems.

Rather than building a boilerplate, I’ve wrapped this up as a modular component that can be included as needed. The advantage of this is that it reduces complexity for developers, cleans up our codebase, and allows everyone to benefit from bug fixes and updates.

You may have noticed that the typographic specification above does not have a baseline grid. That’s because I’ve observed that our designers, as often than not, choose to use a looser grid for type to provide increased flexibility.

But even without a formal baseline grid, it’s helpful to speak the same language as our designers. I wasn’t about to let the spooky box back into our workflow without a fight.

Let's take a look at how to set this up. We can install with npm or bower:

npm install megatype --save-dev

or

bower install megatype --save-dev

We’ll then import this into our SCSS:

@import megatype;

Before we output any styles, let’s have a look at the config. Here we’ll set up our rootsizes, responsive breakpoint settings, and grid gutters. We’ll also specify whether we want to snap our spooky-boxless type to the baseline grid or not. As an extra optional enhancement, I’ve kept an option for smooth, responsive type scaling.

Next we will initialise our rootsizes by including the megatype mixin at the start of our code, and our breakpoint containers by calling set-container on whichever elements we want contained by our max-widths.

This outputs our font-size in rems, our line-height as a unitless number, and our trailer as a margin, also in rems. The leader, however, is computed together with the offset we need to get rid of our spooky box, and results in a css top value, in rems. This is then added to our bottom margin to ensure any following items will flow correctly below - meaning that we’ve only got margins specified in one direction, and still have padding to fall back on if we want to further modify our flow down the page. Here’s what this looks like:

Gazing beyond

All of this takes us back to the initial question: why doesn’t the web have a concept of the true baseline? Could there be room in the CSS spec for further expansion to the suite of typographic properties available to us? Perhaps including a cap-height property, or other typographic information such as baseline and x-height, for example?

There's definitely room to grow in this space. We’d love to hear your own adventures in web typesetting, so let us know what you think. The journey's not over yet.

A design company for people and planet. Let’s talk about making a better future.

Sign up to our newsletter Every friday, no spam.

More about us

We are a certified B Corporation, a growing movement using the power of business to transform the planet for good.

We currently donate 5% of our time to The Life You Can Save, helping fight extreme poverty.

Today is located on the traditional lands of the Wurundjeri people of the Kulin nation and the Gadigal people of the Eora nation. We acknowledge that sovereignty was never ceded and pay our respects to elders past, present and emerging.