DEV Community

Cover image for Responsive typography
nolasco7a
nolasco7a

Posted on

1

Responsive typography

I have experience as a developer but I get surprise because every time you can discover new things, in my case I discover the power of font-size as size base for HTML document or web page

let me explain, everybody knows 1rem = 16px, but.... why.
this is thanks to font-size base in HTML that is 16px by default, then if you change your font-size to 20px, now 1rem = 20px, and then instead of changing all our typography classes or styles, we can simplify this task making the following to create a fully responsive typography:

Imagine that you have the following styles for you typography

p {margin-bottom: 1rem;}

h1, h2, h3, h4, h5 {
  margin: 3rem 0 1.38rem;
  font-family: 'Poppins', sans-serif;
  font-weight: 400;
  line-height: 1.3;
}

h1 {
  margin-top: 0;
  font-size: 3.052rem;
}

h2 {font-size: 2.441rem;}

h3 {font-size: 1.953rem;}

h4 {font-size: 1.563rem;}

h5 {font-size: 1.25rem;}

small, .text_small {font-size: 0.8rem;}
Enter fullscreen mode Exit fullscreen mode

Now to become our styles to a fully responsive typography we only have to add a media queries to change our font-size base
example:

/* Base style */
html {
  font-size: 16px;
}

/* Media queries */
@media (max-width: 768px) {
  html {
    font-size: 12px;
  }
}
Enter fullscreen mode Exit fullscreen mode

And then we have a typography fully responsive for our website, where for small devices 1rem = 12px and for other device sizes 1rem = 16px.

I hope this blog helps someone.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay