Support0

CSS text-fit playground

Explore text-fit with none, grow, and shrink, the consistent, per-line, and per-line-all modes, and an optional percentage that limits the scaling factor.

Ask AI

CSS text-fit playground

Response generated with openai/gpt-5.4-nano. AI can make mistakes. Always review the result.

Design headlines that fill every line without JavaScript calculations.

Your browser does not support text-fit yet. The preview keeps the original font-size, while the generated code preserves the property.

HTML
<h2 class="text-fit-sample">
  Design headlines that fill every line without JavaScript calculations.
</h2>
Tailwind CSS
Examples for Tailwind CSS 4.3 with the default theme. Custom values keep arbitrary classes when there is no equivalent utility. Changing the theme can change utility values.
<h2 class="w-[520px] max-w-full font-extrabold text-[32px] leading-[1.05] whitespace-normal [text-fit:grow_consistent_180%]">
  Design headlines that fill every line without JavaScript calculations.
</h2>
CSS
.text-fit-sample {
  inline-size: 520px;
  max-inline-size: 100%;
  font-family: system-ui, sans-serif;
  font-size: 32px;
  font-weight: 800;
  line-height: 1.05;
  white-space: normal;
  text-fit: grow consistent 180%;
}

Educational guide to text-fit

Fit text to the available space

Compare growth, shrinking, and per-line fitting. Start with a readable size, limit scaling, and preserve wrapping text when the experimental enhancement is unavailable.

Mental model

text-fit scales text during layout to fill the available inline space. It applies to block containers and is inherited. It differs from justify, which distributes spacing, and from selecting a font width with font-stretch.

  1. Available space

    The block needs a width determined by layout. text-fit does not change its intrinsic size: a fit-content width does not itself create room to grow.

  2. Starting size

    font-size sets the base. none does not scale; grow only enlarges and shrink only reduces. Choose one direction, not both at once.

  3. Factor and limit

    The mode determines which lines share a factor. A percentage limits how much they can grow or shrink relative to the starting size.

Static diagram of text fittingThe diagram describes the process; the following examples use native CSS. The result depends on content, line breaks, the font, and actual available space.

Grow with an explicit limit

Declare direction, mode, and percentage, in that order. grow consistent 180% permits at most 1.8 times the base size; 120% permits 1.2 times. Reaching the limit may leave space unfilled, which is a valid result.

One phrase without fitting and with two maximums

text-fit: none

Design with CSS

text-fit: grow consistent 120%

Design with CSS

text-fit: grow consistent 180%

Design with CSS

All three rows start at 1.5rem and share a width. With support, the last two grow until they fill the line or reach their maximum; narrow layouts can wrap. Without support they retain the base size.
One phrase without fitting and with two maximums
<div class="text-fit-guide-growth" lang="en">
  <div>
    <p class="label">text-fit: none</p>
    <p class="sample fit-0">Design with CSS</p>
  </div>
  <div>
    <p class="label">text-fit: grow consistent 120%</p>
    <p class="sample fit-1">Design with CSS</p>
  </div>
  <div>
    <p class="label">text-fit: grow consistent 180%</p>
    <p class="sample fit-2">Design with CSS</p>
  </div>
</div>
.text-fit-guide-growth {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1rem;
  min-inline-size: 0;
  inline-size: 100%;
  max-inline-size: 32rem;

  .label {
    margin-block: 0 0.5rem;
    font-size: 1rem;
    line-height: 1.5;
    overflow-wrap: anywhere;
  }

  .sample {
    margin: 0;
    font-size: 1.5rem;
    line-height: 1.4;
    white-space: normal;
    overflow-wrap: anywhere;
    text-wrap: wrap;
    text-fit: none;
  }

  @supports (text-fit: grow consistent 180%) {
    .fit-1 {
      text-fit: grow consistent 120%;
    }

    .fit-2 {
      text-fit: grow consistent 180%;
    }
  }
}

The percentage is a scaling factor, not a percentage of the container. With grow it must be at least 100%; with shrink, between 0% and 100%. Omitting it or using a percentage outside that range leaves scaling unlimited. The playground restricts its controls; those ranges do not cover all CSS syntax.

One shared factor or a factor per line

consistent, the default mode, uses a shared factor for every line. per-line calculates each line separately, except the last one and lines ending in a forced break. per-line-all includes those lines too.

Natural wrapping, a br, and a short final line

grow consistent 180%

Text layout depends on the available width and on the words that fit on each line.
With CSS

grow per-line 180%

Text layout depends on the available width and on the words that fit on each line.
With CSS

grow per-line-all 180%

Text layout depends on the available width and on the words that fit on each line.
With CSS

The paragraph can wrap and ends with a br before “With CSS”. per-line leaves the line before the br and the final line unscaled; per-line-all can enlarge them up to 180%. Below 20rem of available space, all three rows keep the base size and wrapping for readability. They may also look identical when there is no room to grow.
Natural wrapping, a br, and a short final line
<div class="text-fit-guide-lines" lang="en">
  <div>
    <p class="label">grow consistent 180%</p>
    <p class="sample fit-0">Text layout depends on the available width and on the words that fit on each line.<br>With CSS</p>
  </div>
  <div>
    <p class="label">grow per-line 180%</p>
    <p class="sample fit-1">Text layout depends on the available width and on the words that fit on each line.<br>With CSS</p>
  </div>
  <div>
    <p class="label">grow per-line-all 180%</p>
    <p class="sample fit-2">Text layout depends on the available width and on the words that fit on each line.<br>With CSS</p>
  </div>
</div>
.text-fit-guide-lines {
  container-type: inline-size;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1rem;
  min-inline-size: 0;
  inline-size: 100%;
  max-inline-size: 32rem;

  .label {
    margin-block: 0 0.5rem;
    font-size: 1rem;
    line-height: 1.5;
    overflow-wrap: anywhere;
  }

  .sample {
    margin: 0;
    font-size: 1.5rem;
    line-height: 1.4;
    white-space: normal;
    overflow-wrap: anywhere;
    text-wrap: wrap;
    text-fit: none;
  }

  @container (inline-size >= 20rem) {
    @supports (text-fit: grow consistent 180%) {
      .fit-0 {
        text-fit: grow consistent 180%;
      }
    }

    @supports (text-fit: grow per-line 180%) {
      .fit-1 {
        text-fit: grow per-line 180%;
      }
    }

    @supports (text-fit: grow per-line-all 180%) {
      .fit-2 {
        text-fit: grow per-line-all 180%;
      }
    }
  }
}

per-line-all can overemphasize a short final word. Use it for short headings where that hierarchy is intentional; a uniform size is usually more predictable for continuous reading. text-wrap chooses line breaks and text-fit chooses scaling; they are not interchangeable.

Shrink without making nowrap mandatory

With white-space: normal, words usually wrap before shrinking is needed. nowrap makes shrink observable on a single line, but the minimum scale may still prevent text from fitting. It does not guarantee freedom from overflow.

An enhancement conditional on available space

Base · 2rem with wrapping allowed

Design interfaces with readable words

Enhancement · shrink consistent 80% from 32rem

Design interfaces with readable words

The second row enables nowrap and shrink only if the syntax is accepted and the container is at least 32rem wide. At smaller widths, with enlarged text, or without support, wrapping remains. The 80% minimum preserves at least 1.6rem; this phrase and threshold do not suit every translation.
An enhancement conditional on available space
<div class="text-fit-guide-shrink" lang="en">
  <div>
    <p class="label">Base · 2rem with wrapping allowed</p>
    <p class="sample">Design interfaces with readable words</p>
  </div>
  <div>
    <p class="label">Enhancement · shrink consistent 80% from 32rem</p>
    <p class="sample fitted">Design interfaces with readable words</p>
  </div>
</div>
.text-fit-guide-shrink {
  container-type: inline-size;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1rem;
  min-inline-size: 0;
  inline-size: 100%;
  max-inline-size: 32rem;

  .label {
    margin-block: 0 0.5rem;
    font-size: 1rem;
    line-height: 1.5;
    overflow-wrap: anywhere;
  }

  .sample {
    margin: 0;
    font-size: 1.5rem;
    line-height: 1.4;
    white-space: normal;
    overflow-wrap: anywhere;
    text-wrap: wrap;
    text-fit: none;
  }

  .sample {
    font-size: 2rem;
  }

  .fitted {
    @supports (text-fit: shrink consistent 80%) {
      @container (inline-size >= 32rem) {
        white-space: nowrap;
        text-fit: shrink consistent 80%;
      }
    }
  }
}

Fitting does not change the computed font-size: it may still report 2rem even when the glyphs look smaller. Other properties’ em lengths are not recalculated using the scale either. Check the actual rendering and let the block grow; do not fix its height or clip content to hide a reached limit.

Common mistakes

Fitting makes no visible difference

Expected result
Scaling responds to the space left on the line.
Cause
The block sizes itself to its content, words already wrap, or per-line excludes every line ending in a forced break.
Solution
Check the used width, direction, and breaks. Compare none with a moderate limit; use per-line-all only if you want to include the last line and forced breaks too.

Text still overflows or becomes tiny

Expected result
The full content stays readable and accessible.
Cause
nowrap prevents wrapping and the minimum is not enough to fit, or shrinking has no useful minimum.
Solution
Restore white-space: normal, provide more space, or change the layout. Test long content and translations; unlimited shrinking does not solve layout constraints.

Zoom does not enlarge text as expected

Expected result
Users can enlarge text without losing information or functionality.
Cause
Fitting text back into the same apparent width can counteract enlargement. The CSS draft keeps this issue open.
Solution
Check actual zoom and text preferences. Keep a rem base, allow reflow, and use fitting selectively; if it prevents enlargement, disable it in that design. A percentage alone does not guarantee accessibility.

Support and compatibility

text-fit remains experimental with limited availability, defined in the CSS Text Level 5 draft. BCD records Chrome 150 and Edge 150 for the property, all three modes, and percentages; Firefox and Safari have no support at the consultation date below.

The examples use @supports for the syntax they apply. This checks acceptance, not fitting quality, zoom behavior, or universal compatibility. Without support, font-size and wrapping remain: a readable fallback, not a reproduction of scaling. No polyfill or JavaScript simulation is added.