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

Most teams don’t lose time because anyone can’t do their job. They lose time in the gaps between jobs. Dev asks whether the “card” is a reusable component or a one-off. Design says it’s a component, but the file has three slightly different paddings. Someone realizes the mobile layout wasn’t...

Article
A practical, budgetable list of digital marketing tools and tactics worth adopting in 2026—grounded in real execution, real platforms, and what holds up for both B2B and B2C teams....
Journal Entry

I can understand a medical explanation and still fall apart in a parking lot ten minutes later. That used to confuse me, mostly because it felt irrational. I assumed knowledge would do most of the emotional heavy lifting. If I could learn enough, ask enough questions, follow the plan precisely,...

Article
AI can make your brand look polished and completely forgettable. This article explores how to use AI in your creative process without losing texture, trust, or a human voice....
Article
Remote meetings often reward the loudest voices. This post explores how leaders and teams can protect airtime, practice real listening, and create a culture where ideas actually land....
Article
Choosing between a traditional, headless, or hybrid CMS can feel like a purely technical decision. It isn’t. This post breaks down each model through the lens of editors, developers, and end users so you can pick a stack that supports real content workflows, multi-channel experiences, and long-term flexibility without overengineering...