How to Dynamically Adjust Font Size Based on Viewport with CSS and JavaScript

Table of Contents

One of the biggest challenges in responsive web design is ensuring text remains readable across all screen sizes. A font size that looks perfect on a desktop screen might be too small on a mobile device—or too large on a wide monitor.

The solution? Dynamic font scaling.

By using modern CSS techniques and JavaScript, you can adjust font sizes based on the viewport, ensuring text remains legible and visually balanced on any device.


Solution 1: Use clamp() for Flexible Scaling (Best for Most Cases)

The clamp() function in CSS allows you to define:

  • A minimum font size (to prevent text from getting too small).
  • A preferred size that scales with the viewport.
  • A maximum font size (to avoid excessive scaling on large screens).

Example: Dynamically Scaling Font with clamp()

				
					h1 {
    font-size: clamp(1.5rem, 5vw, 3rem);
}

				
			

How it works:

  • The font will never be smaller than 1.5rem.
  • It will scale dynamically based on 5% of the viewport width (5vw).
  • It will never be larger than 3rem, no matter how big the screen gets.

When to Use It

clamp() is ideal for text that needs to scale smoothly, such as:

  • Headings (h1, h2, h3)
  • Large buttons
  • Hero section text

Solution 2: Use vw Units for Viewport-Based Scaling

vw (viewport width) units adjust text size based on the width of the screen.

Example: Scaling Font with vw

				
					p {
    font-size: 2vw;
}

				
			

How it works:

  • The text size is 2% of the viewport width.
  • The larger the screen, the larger the text.

Downside:

  • On very small screens, text can shrink too much, making it hard to read.
  • On extra-large screens, text can become too big.

To avoid this, use vw with min() or max() constraints:

				
					p {
    font-size: min(2vw, 1.2rem);
}

				
			

This keeps the font size responsive but prevents it from becoming too small.


Solution 3: Adjust Font Size with JavaScript (For Advanced Control)

If you need more precise control (such as font resizing based on content length or device type), JavaScript can help.

Example: JavaScript-Based Font Scaling

				
					function adjustFontSize() {
    let viewportWidth = window.innerWidth;
    let baseSize = 16; // Default font size in pixels

    if (viewportWidth < 600) {
        document.body.style.fontSize = (baseSize * 0.8) + "px"; // Smaller for mobile
    } else if (viewportWidth > 1200) {
        document.body.style.fontSize = (baseSize * 1.2) + "px"; // Larger for desktop
    } else {
        document.body.style.fontSize = baseSize + "px"; // Default size
    }
}

// Adjust font size on load and resize
window.addEventListener("resize", adjustFontSize);
window.addEventListener("load", adjustFontSize);

				
			

When to Use JavaScript for Font Scaling

✔ If you need custom breakpoints beyond CSS flexibility.
✔ When working with dynamic content that requires text resizing.
✔ If you want real-time adjustments based on interactions.


Which Method Should You Use?

MethodBest Use CaseProsCons
clamp()Headings, hero textEasy to implement, smooth scalingNot supported in older browsers
vw unitsLarge fluid typographyAuto-scales with viewportCan shrink too much on small screens
JavaScriptCustom resizing logicFull control over breakpointsRequires extra scripting

For most cases, clamp() is the best solution—it is responsive, CSS-native, and easy to use. Use JavaScript only when necessary for more complex scaling needs.


Final Thoughts: Keep Text Readable, No Matter the Screen

Font size should never be an afterthought. By using clamp(), vw, or JavaScript-based adjustments, you ensure text is:
✔ Readable on any device
✔ Adaptable to different screen sizes
✔ Balanced between aesthetics and usability

Test your site across devices and ask yourself: is this text comfortable to read? If not, it is time to make some adjustments.

Explore more posts

Not all friction in UX is functional. Some of it is emotional. And if you’re not designing your forms with...

We treat suffering like it’s a mistake.A deviation from the plan. A crack in the foundation. Something that shouldn’t be...

CX vs. UX: A Quick Definition Customer experience and user experience are often confused—but understanding the difference between CX and...

In marketing, every interaction matters—but not all moments are remembered equally. According to the Peak-End Rule, people judge an experience...

We’re no longer designing just for search engines. We’re designing for AI. As tools like Google’s Search Generative Experience (SGE),...

Make the Bed? Make Yourself. You’ve probably heard the advice: if you want to start your day with purpose, make...