Skip to content

Fluid typography examples

Some practical examples of using calc and viewport units for fluid typography and more.

A few months ago I published a fluid type technique that doesn't require any JavaScript. Even though I consider it experimental the technique works well enough as a progressive enhancement and it generated a lot of interest, comments and suggestions. So I thought I'd put together a bunch of examples and address some comments.

Fluid type with pixels permalink

This is a simplified version of my original example. The minimum font size is 14px and the maximum is 22px. I've removed a redundant media query and reduced the complexity of the calc() equation.

.fluid-type {
font-size: 14px;
}

@media screen and (min-width: 320px) {
.fluid-type {
font-size: calc(14px + 8 * ((100vw - 320px) / 960));
}
}

@media screen and (min-width: 1280px) {
.fluid-type {
font-size: 22px;
}
}

Example permalink

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.

Fluid type with Rem units permalink

This example should have the same result as the one above when the base font size is 16px (default).

It shows that the technique works with any length unit, as long as you can use it in a media query. It also addresses comments regarding how my initial example will override user preferences for the default font size.

The only catch is that all unit types must be the same for the calc() equation to work. That's a shame because we often use different unit types for breakpoints in media queries than we do for font-size.

.fluid-type {
font-size: 0.875rem;
}

@media screen and (min-width: 20rem) {
.fluid-type {
font-size: calc(0.875rem + 0.5 * ((100vw - 20rem) / 60));
}
}

@media screen and (min-width: 80rem) {
.fluid-type {
font-size: 1.375rem;
}
}

Example permalink

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.

Reverse fluid type permalink

In this example the text gets smaller as the viewport gets larger. This might have novel uses or it might not.

.fluid-type {
font-size: 22px;
}

@media screen and (min-width: 320px) {
.fluid-type {
font-size: calc(22px + -8 * ((100vw - 320px) / 960));
}
}

@media screen and (min-width: 1280px) {
.fluid-type {
font-size: 14px;
}
}

Example permalink

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.

Fluid line-height (Molten leading) permalink

In this example the line-height is fluid. This is a pure CSS implementation of Wilto's Molten leading technique.

.molten-leading {
line-height: 1.2em;
}

@media screen and (min-width: 20em) {
.molten-leading {
line-height: calc(1.2em + 0.6 * ((100vw - 20em) / 60));
}
}

@media screen and (min-width: 80em) {
.molten-leading {
line-height: 1.8em;
}
}

Example permalink

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.

Fluid box permalink

This example shows how the technique can be applied to more than just font sizes, in this case width.

.fluid-box {
width: 200px;
}

@media screen and (min-width: 320px) {
.fluid-box {
width: calc(200px + 300 * ((100vw - 320px) / 960));
}
}

@media screen and (min-width: 1280px) {
.fluid-box {
width: 500px;
}
}

Example permalink

The width of this box will scale and at a different rate to the viewport.

Fluid type in Sass permalink

Indrek Paas developed a Sass mixin to help make fluid type using this technique easier. You can find the latest fluid type Sass mixin here.

Update: I now recommend using this mixin

I use a slightly modified version to generate the examples on this page.

Example: permalink

.fluid-type {
@include fluid-type(320px, 1280px, 14px, 18px);
}

Fluid type in Less permalink

If Less is how you roll I've got you covered with a Less mixin.

Fluid type in PostCSS permalink

Rucksack is a postCSS module that makes use of this technique for fluid typography.

I have a collection of other examples on CodePen. Let me know if you have one you'd like me to share.

Tags permalink