As you can tell by now, coding responsive typography is pretty painful. You need to write many lines of code at different breakpoints to ensure your typography remains readable. Making typography easy and painless is no small feat.
Luckily, there’s a pattern to make responsive typography easy. The idea is to change font-size and line-height values whenever there’s a need to, then, use relative units to propagate the change throughout your website or application.
Once you see the pattern, the rest becomes much easier. You can create mixins and functions to remove the hassle of calculating everything manually (this is hard though!). Alternatively, you can also use libraries to help you with the calculation. Some popular libraries out there include: Sassline, Megatype, and Typi.
Of the three, Typi is probably the best library for you since you came so far. I built Typi with the same principles you’ve learned in the course, so it’s going to be a natural next step to help you apply responsive typography to your projects.
In this lesson, I want to share with you how to install, setup and use Typi. Before we do that, let’s first take a look at what Typi can do for you, and why I recommend it.
(Note: Before we begin, please stop using Compass if you have heard of it/are using it since it’s no longer supported and updated anymore)
What Typi can do for you
Typi does two things for you incredibly well.
First, Typi helps you write font-size and line-height declarations at multiple breakpoints without breaking a sweat.
There are a few configurations you have to make (we’ll get to them later), but once you’re done with the configurations, you just have to write a simple @include statement:
h1 { @include typi('h1');}And you CSS output will look like this:
h1 { font-size: 1.5rem; line-height: 1.3;}
@media all and (min-width: 600px) { h1 { font-size: 2.369rem; line-height: 1.2; }}With just one statement, Typi helps you write font-size, line-height and media queries at every breakpoint you need for you website/application.
Second, Typi helps you calculate vertical rhythm without having to do the math yourself. Here’s an example:
h1 { margin-top: vr(1);}The compiled CSS from the code above is:
h1 { margin-top: 1.4rem;}This Vertical Rhythm feature in Typi, helps you instantly think in baselines without worrying about calculation, which is pretty sweet since calculation takes loads of mental (or even punching-the-calculator) work.
With that, I’ve officially introduced you to the two things Typi does incredibly well. If the code above looks any interesting to you, I invite you to read on.
Typi Prerequisites
Typi requires you to know a little bit of Sass. Specifically, you need to know how to use Sass Maps, functions and mixins. If you don’t already know these, I suggest you grab some free chapters of my Susy book. (Just focus on Chapter 3 where I explain some Sass stuff).
You also need to know how to compile Sass to CSS before moving on. The best way (in the current development world is to use Gulp). If you don’t already know how, I recommend you grab yourself some free chapters from my automate your workflow book. I’ll show you how to do that in there. (Read Chapter 5 to 7).
Let’s move on.
Installing Typi
There are three ways to install Typi—with Bower, with npm and by downloading from the Github repository.
(Note: we’re going to install the latest version of Typi, which is still not released to the public because I’ve yet to find time to write documentations and create a demo page. When it’s officially released, I’ll update this lesson to reflect the new installation instructions).
Installing with Bower
Go to your project and run the bower init command. This should create a bower.json file. Head into this bower.json file and add typi#3.0.0-alpha as a dependency:
dependencies { "typi": "3.0.0-alpha"}Then, run the bower install command.
Installing with npm
Go to your project and run npm init to create a package.json file if you haven’t had it already. Then, run npm install [email protected].
Manual installation
Head to this page and download the sass files manually into your project.
Adding Typi to your project
Once you’ve downloaded Typi into your project through any of the above steps, you need to @import Typi into your project. You do so with the following code:
// Change `path-to-typi` with the correct path!@import 'path-to-typi/scss/typi';Configuring Typi
The first step you need to take with Typi is to configure two Sass maps: the (1) $typi map and the (2) $breakpoints map.
The $breakpoints map is a series of key: value pairs that tell Typi what media queries to create for each font-size and line-height property you intend to write. It looks like this:
$breakpoints: ( small: 600px, large: 1200px,);Feel free to leave breakpoint values in pixels if you intend to use a breakpoint library that’s compatible with Typi (more on that later). Otherwise, I recommend you convert these values into em.
That’s it for the $breakpoints map. It’s quite straightforward.
The second map is called the $typi map. The bare minimum version looks like this:
$typi: ( base: ( null: ( 16px, 1.4, ), small: ( 18px, ), large: ( 20px, ), ) // Other font maps here,,,,);The $typi map is a storage of different font maps that contain information about breakpoints to create and the font-sizes and line-heights that should be written that that breakpoint.
The first font-map in $typi should always be the base font-map. This tells Typi to output the correct font-sizes and line-height in the html selector. Here’s what it reads:
nullkey: Create font-size of 16px and line-height of 1.4 without breakpointssmallkey: At 600px, change font-size to 18pxlargekey: At 1200px, change font-size to 20px
You can also create other font-maps, but we’ll talk about them later to make things easier to understand. Let’s see how to use this base font-map first.
Using the base font-map with typi-init
Typi uses the base font-map to create font-size and line-height values for the html selector. You tell Typi to create these values by using the typi-init mixin.
@include typi-init;After using the typi-init mixin, you should get the following CSS. Notice how pixel values get converted into percentage values when you use typi-init.
html { font-size: 100%; /* this means 16px */ line-height: 1.4;}
@media all and (min-width: 600px) { html { font-size: 112.5%; /* this means 18px */ }}
@media all and (min-width: 1200px) { html { font-size: 125%; /* this means 20px */ }}Creating and using other font maps
Typi allows you to create other font-size and line-height and media query combinations by creating another font-map, like this:
$typi: ( // base font-map, h1: ( null: ( 24px, 1.3, ), small: ( 2.369em, 1.2, ), ));In the code above, we created a h1 font-map with that:
- Creates a font-size with a value of
24px(written in rem) and a line-height of 1.3 without breakpoints - Changes font-size to a value of
2.369em(written in rem) and line-height to 1.2 at a minimum width of600px.
You can use this h1 font-map with the typi mixin once you’ve created it, like this:
h1 { @include typi('h1');}The CSS produced by Typi is (notice how font-sizes gets converted into rem):
h1 { font-size: 1.5rem; line-height: 1.3;}
@media all and (min-width: 600px) { h1 { font-size: 2.369rem; line-height: 1.2; }}Since Typi works with em values, you can also use Modular Scale easily with Typi like this:
$typi: ( // base font-map, h1: ( null: ( 24px, 1.3, ), small: ( ms(3), 1.2, ), ));We’ve talked about why I highly recommend the use of rem over em in the Rem vs Em lesson. Head over there and give yourself a quick refresher if you need one.
Creating font-sizes in em with Typi
Although I highly recommend the use of rem, there are instances where you want to use em over rem. If this happens, all you need to do is tell Typi you want to create sizes in em by stating $rem: false, like this:
@include typi('h1', $rem: false);And Typi automatically writes sizes in the em unit.
h1 { font-size: 1.5em; line-height: 1.3;}
@media all and (min-width: 600px) { h1 { font-size: 2.369em; line-height: 1.2; }}Writing em-based media queries with Typi
I mentioned earlier in this lesson that you should use em values for media queries, unless you decide to use a breakpoint library that’s compatible with Typi. Typi will automatically convert all pixel values to em if you use such a library.
At this moment, Typi supports the use of two breakpoint libraries—Mappy breakpoints and Breakpoint Sass. You tell Typi about the existence of these libraries with:
// using Mappy Breakpoint$typi-breakpoint: mappy-bp;
// using Breakpoint Sass$typi-breakpoint: breakpoint;Then, Typi will do it’s job and convert pixels to em automatically:
h1 { font-size: 1.5em; line-height: 1.3;}
@media all and (min-width: 37.5em) { h1 { font-size: 2.369em; line-height: 1.2; }}What about vertical rhythm?
Calculating Vertical Rhythm can be difficult and confusing. You probably don’t want to do the math every time you need to create a margin or padding, which is why Typi gives you a vr() function to count baselines without requiring you to do complicated calculations yourself. It looks like this:
h1 { margin-top: vr(1); // 1 baseline margin-bottom: vr(2); // 2 baselines}Typi uses the null key in your base font-map to calculate the Vertical Rhythm. In our case, our base font-map looks like this:
$typi: ( base: ( null: ( 16px, 1.4, ), small: ( 18px, ), large: ( 20px, ), ) // Other font maps here,,,,);Typi then uses the line-height value (1.4) to calculate the Vertical Rhythm.
h1 { margin-top: 1.4rem; /* 1 baseline */ margin-bottom: 2.8rem; /* 2 baselines */}Vertical Rhythm in em units
Typi gives you the ability to write Vertical Rhythms in em instead of rem whenever you need to. To do so, you include the font-size as a second parameter and Typi will automatically do the rest. This font-size parameter can either be in pixels or em:
h1 { // This is equivalent to vr(3) if font-size is 1.5em margin: vr(3, 1.5em);}Additional features
More features are coming to Typi. However, I haven’t had the time to work them out (or write about them) yet. I’ll let you know when I manage to make Typi even better than it is now!
Wrapping Up
In this lesson, you learned how to use Typi, a responsive typography library that integrates every lesson you’ve learned so far in the course.
Just a quick recap, Typi is great for two things:
- It helps you write font-size and line-height declarations at multiple breakpoints
- It helps you calculate vertical rhythm
Exercise
There are a few things to try in this exercise.
- Try installing Typi and getting it to create your base font-map
- Try creating several font-maps with Typi. Output both rem and em values
- Try using Typi for Vertical Rhythm. Output both rem and em values
- Try integrating a breakpoint library like Mappy Breakpoints or Breakpoint to help you streamline your workflow further
If you run into compile errors, make sure you’re on Typi v3 (not v2.3). Also, check for any typos you’ve made along the way. Sass complains of errors whenever you make typos :)
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson