FrontendAtlas
Interview Questions
Home>CSS interview questions> CSS Coding Challenges >Fluid Type & Spacing with clamp()

Fluid Type & Spacing with clamp()

intermediate
By FrontendAtlas Editorial · Updated Jan 30, 2026
Implement fluid typography and spacing with clamp(). Use clamp(min, preferred, max) to scale values across viewport sizes while keeping readable bounds. Explain why clamp is safer than raw vw scaling and reduces the need for extra media queries.

Solution

Overview

clamp() blends fluid scaling with guardrails, which is perfect for headings and padding. The preferred value can use vw so the size grows with the screen, while min and max ensure legibility on small devices and prevent oversized type on large monitors. This keeps layouts responsive without brittle breakpoints or hard jumps.

1

Approach — clamp() for Type & Padding

Apply clamp() to a hero heading (e.g. clamp(1.75rem, 3vw, 3rem)) and to horizontal padding like padding-inline: clamp(16px, 4vw, 48px). Choose the min value for phone readability and the max for desktop balance. The middle value uses vw to scale smoothly between the bounds.

<html>
<header class='hero'>
  <h1>
    Fluid Headline
  </h1>
  <p>
    Scales smoothly between small and large screens.
  </p>
</header>
# CSS
.hero {
  background: #222;
  color: #fff;
  padding-inline: clamp(1rem, 4vw, 3rem);
  padding-block: 2rem;
  border-bottom: 1px solid #444;
}
.hero h1 {
  margin: 0 0 0.5rem;
  font-size: clamp(1.5rem, 3vw + 1rem, 3rem);
}
.hero p {
  margin: 0;
  opacity: 0.9;
}

Notes & Pitfalls

Pitfalls
  • Using an overly small min can make headings unreadable on tiny screens.
  • Setting an excessive max can cause overflow or awkward line breaks on large displays.
Techniques
  • Combine vw with a base rem (e.g., 3vw + 1rem) inside clamp() for smoother scaling.
  • Use padding-inline to set left/right together; it mirrors automatically in RTL layouts.

Resources

  • MDN – clamp()
  • MDN – CSS Values and Units (vw, rem)

Similar questions

Flexbox: Responsive NavbarintermediateGrid: Card Gallery (2→4 Columns)intermediateForms & Pseudo-classes: Focus, Invalid, Required, Helper Textintermediate

Guides

Frontend interview preparation guideGuideFrontend coding interview questions and prep guideBlueprintJavaScript Problems That Actually Show UpBlueprintBuild Great UI in 60 MinutesBlueprint

Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.

Open frontend interview questionsBrowse CSS interview questionsOpen Essential 60Open Machine Coding HubOpen Frontend Coding ChallengesOpen System DesignOpen Interview Prep GuideOpen System Design BlueprintOpen Framework Prep PathsOpen Study PlansOpen 7-day crash study planOpen Company Prep
↗Incidents hub
← Prev←6 / 10Next →→