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
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...
Article
This post reframes audience research through the DISC model—Red, Yellow, Green, and Blue—so you can spot behavioral patterns in your data and design experiences that match how different personalities make decisions....
Journal Entry
My ADHD loves big plans and then forgetting all of them. The 1–3–5 rule is how I keep that from running my life: one workout, three acts of basic care, five small learning blocks every day. Paired with a Sunday planner ritual, it turns to-do lists into actual promises I...
Article
Most buyers aren’t giving your campaign their full attention. They’re skimming between notifications and tabs. This post reframes the classic funnel as attention windows and shows how to design campaigns that earn one more second, then another, until you finally win real focus with creative, UX, and media working together....
Article

Most brand work is either outward-facing (“What do customers think of us?”) or inward-facing (“How do we attract talent?”). The problem is that your buyers and your employees experience the same company. When the story they’re told doesn’t match the reality inside, trust erodes fast. A modern brand has to...

Article
Accessibility is often treated as a late-stage checklist item, but it is one of the fastest ways to improve overall UX, expand your market, and build trust. This post reframes accessibility as a strategic advantage and walks through concrete, realistic ways to bake it into design, development, and content from...