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

Article
People don't connect with perfection. They connect with proof—especially when that proof comes from lived experience. In a digital world full of curated images and generic messaging, the most persuasive voice is often the one that has survived something real. When a story feels honest, it builds trust. And when...
Article
Short-form video isn’t just a format—it’s a new grammar of communication. Here’s what that means for your brand, your strategy, and the way you connect with modern audiences....
Article
The best brand experiences blend online and offline moments. This post explores how to bridge the gap—from QR-linked in-store content to digital follow-ups that keep physical events alive....
Article
AI automation isn’t about replacing people—it’s about removing friction. Learn what it actually means, how to start, and how to make sure your brand uses AI for good, not just speed....
Article
Dangle a carrot, swing a stick—neither creates lasting motivation. This post dives into why these tactics fall short and what modern leaders can do instead to drive real engagement....
FAQ
Most marketers stop at basic personalization—but your audience expects more. Here’s how hyper-personalization works, what makes it powerful, and how to use it without losing the human touch....