Modular and Dynamic Type Scales with Plain CSS

Type scales can be difficult to maintain, but CSS Custom Properties can help.

If you have ever had to design a type scale, you know that, like grids, it can require quite a lot of calculations. Therefore if not structured properly type scales can also result into messy and difficult code to maintain.

As Mikolaj Dobrucki demonstrates in his CSS Tricks article, the dynamism of Custom Properties makes them an excellent choice for defining responsive and flexible grid styles. However, Dobrucki’s article doesn’t consider that Custom Properties are also a good option for structuring the code for your responsive type scale.

Why not use Sass?

I don’t want to make this into an article about when to use Custom Properties instead of preprocessor variables. If you’re curious about that topic, checkout Michael Riethmuller’s article on Smashing Magazine. Yet I do think it’s important to talk a bit about why I use of Custom Properties here, and the reason is scope.

Unlike Sass variables, Custom Properties are dynamically scoped. As Riethmuller explains in his article:

Custom properties work differently. Where custom properties are concerned, dynamically scoped means they are subject to inheritance and the cascade. The property is tied to a selector and if the value changes, this affects all matching DOM elements just like any other CSS property.

A Strategy Guide To CSS Custom Properties, Michael Riethmuller

When you try to change the value of a Sass variable within a media query, you cannot. Instead, you create a different variable with the same name. Therefore the code that came before it is not changed, and you must re-declare the style again.

For example, if you want to change the color at different breakpoints, this will not work.

$color-change: blue; 

Instead you will need to do this:

$color-change: blue; 
.sass-color {
color: $color-change;

On the other hand, when you change the color of a Custom Property inside a media query, you are essentially changing a CSS property, and what came before it is subject to change per the rules of the cascade.

.css-color { 
--color-change: green;
color: var(--color-change);
@media (max-width: 700px) {
.css-color {
--color-change: red;

If you are a visual person who also likes to experiment (like me!), you can check out this Code Pen and play around with some of the variables and properties.

Check out the dynamic CSS Custom Properties example in Code Pen

So what would the type scale code look like?

Because Custom Properties are rather new and best practices are still being developed, there are a few good ways to approach type scales. This is my approach.

Start of by setting the :root font-size. This will determine the values of rem measurements, which I will be using to assure consistency. If you do not set this value, most browsers will set it to 16px.

:root { 
font-size: 12px;

Afterwards we can set the base font-size for our type scale, and a scale value. These will be used to determine how big your smallest fonts are, and the ratio at which your font-sizes will increment. I recommend using a tool like to determine your base size and scale values.

:root { 
font-size: 12px;
--base-font-size: 1rem;
--scale: 1.25;

Later we can use Calc and Custom Properties to create functions for the type scale logic.

:root { 
font-size: 12px;
--base-font-size: 1rem;
--scale: 1.25;

--p-size: var(--base-font-size);
--h6-size: calc(var(--base-font-size) * var(--scale));
--h5-size: calc(var(--h6-size) * var(--scale));
--h4-size: calc(var(--h5-size) * var(--scale));
--h3-size: calc(var(--h4-size) * var(--scale));
--h2-size: calc(var(--h3-size) * var(--scale));
--h1-size: calc(var(--h2-size) * var(--scale)); }

If you want to use a different font-size for small and large devices, you can simply change the value of --scale and/or --base-font-size inside a media query. Joseph Mueller makes a few recommendations on how to create a scale that varies between devices in hisMedium article "Exploring Responsive Type Scales".

@media (min-width: 900px){ 
--base-font-size: 1.25rem;
--scale: 1.33;

Finally declare the sizes and styles for your text elements.

body, p, ul, ol { 
font-size: var(--p-size);
line-height: 1.5;
font-family: Helvetica;

Check out my Code Pen, to see what this looks like put together.

Check out the type scale code in Code Pen!

Advantages of this code structure

Some of you might be thinking this looks a bit overly complicated, but depending on your project, setting up your code this way can save you a few head aches later. Here’s a few advantages to consider:

Easy to maintain and change. Say I want to change my type scale later on. Instead of changing every single font-size to adjust to my new scale, I can simply change the --base-font value or the --scale value and this will result in a whole new cohesive type scale.

Self-documenting. Now, I don’t suggest skipping more formal documentation if you’re working on a team. However, by structuring your type scale in this way, you only need to refer to the :root declaration block to see the relationship between type sizes.

Separating responsive logic from declarations. By structuring the code this way, you set the logic for different devices on top and later declare the styles. You don’t need to worry about media queries when styling. Therefore, even though your code is longer, it’s also potentially easier to scan and read.

Reusable responsive sizes. Say I have a non-heading text item, such as the text within a button, which I want to have the size of my h6. I can simply reuse the --h6-size custom property.

button { 
font-size: var(--h6-size);


While they can take some getting used to, CSS Custom Properties offer a whole new way of structuring your CSS so it’s more readable, modular, and maintainable. The world of Custom Properties is rather new and best practices are still being developed, but I believe responsive typography is a great place to involve them. What advantages or disadvantages do you see with using Custom Properties for setting up type scales?

Originally published at on June 5, 2019.


Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Zoraida Cabrera-Mieles

Written by

UX Designer/Researcher @ Harvard University. My work ranges from design and user research, to design facilitation, to CSS theming.

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store