To fit text in a div in CSS, first decide what "fit" means: keep the box at a fixed size, or let it grow to hold the text. For a fixed box, you control wrapping with overflow-wrap and word-break, clipping or scrolling with overflow, and shrinking type only as far as it stays readable. For a flexible box, remove fixed heights and let normal flow do the work. The right tool depends on whether the text is a short heading, a labelled button, a paragraph, or user-generated content of unknown length.

That first decision prevents most of the frustration. People often reach for font-size tricks before checking whether a stray height, line-height, or padding value is really the culprit. The order below moves from the simplest, most reversible checks to the heavier techniques, so you can stop as soon as the text fits.

Start by Naming the Fit Problem and the Container's Job

There are two fundamentally different jobs a text container can do. In a fixed container, the element must stay a particular width and height because it sits in a grid, a card, a button, or a tightly designed layout. In a growing container, the element is a wrapper whose size should follow its content. Each job has its own fix, and mixing them is where layouts break.

Fixed sizes come from width, height, min-height, max-height, aspect-ratio, absolute positioning, or a grid/flex parent that stretches the child. If a div is 200 pixels tall and holds a paragraph, the paragraph has 200 pixels to work with; extra text must either clip, scroll, wrap more tightly, or force the parent to change.

A growing container is usually the easier answer. If nothing pins the height, a block-level div expands vertically by default: its height is determined by the line boxes inside it. Horizontal growth is different because block elements fill the available width and wrap their text, while inline formatting and long unbroken strings can overflow.

Before touching any property, inspect the element and answer three questions. Is the height fixed, or is line-height inflating each line so fewer fit than expected? Is the text one line or many, and does it contain long unbreakable tokens like URLs or IDs? Is the box allowed to scroll or clip, or must everything stay visible? Write down the answers; they decide which of the following techniques you actually need.

The Default That Already Works: Normal Flow and Line Heights

A div with no height constraint already fits ordinary text. Browsers lay text into lines, split at spaces, and expand the box's height to hold every line. The usual reasons this "stops working" are constraints you added elsewhere, not a flaw in text layout.

line-height is the first thing to audit. If font-size is 16 pixels and line-height is 1.5, each line occupies 24 pixels. A 120-pixel-tall box fits five such lines and clips or overflows the sixth. Tiny line-height values look cramped; huge values make fewer lines fit and can visually push text past borders. Keep line-height as a unitless multiplier so it scales with font size, and check it against the box height in pixels.

Padding and borders count inside the box's height when box-sizing: border-box is set; without it, height applies to the content area only. A box declared 100 pixels tall with 20 pixels of top and bottom padding has only 60 pixels of content space under border-box. That subtlety explains plenty of "the text overflows even though the box looks big enough" cases.

Also confirm the element's display value. A div is block-level, which means it takes the full available width and wraps text. If someone sets display: flex on a text container and leaves the default align-items: stretch, the child text may behave differently than expected, and if you set display: inline-block with a fixed width, wrapping rules still apply but the box shrinks to content. Knowing which formatting context you are in tells you which properties below will take effect.

Keep the Box Fixed: Control Wrapping, Clipping, and Scrolling

When the box must stay a specific size, the goal is to make the text behave inside it. Wrapping is the first lever. Place a test line of text that includes an obvious natural break and see whether it wraps. If it doesn't, check for white-space: nowrap or a flex/grid parent that gives the text item a minimum content width.

Long unbroken strings are a classic failure. A URL or an identifier with no spaces will overflow past the box edge because normal wrapping only breaks at whitespace. overflow-wrap: break-word lets the browser break a long word at the end of a line if it would otherwise overflow, while still preferring natural breaks. Use overflow-wrap: anywhere when you need to break anywhere to avoid overflow, accepting that it can split words in mixed scripts. The older word-break: break-all is harsher; it breaks at cell boundaries, which is useful for dense tables but can look odd in prose. word-break: break-word is a deprecated alias whose behavior varies; prefer overflow-wrap.

Once wrapping is handled, decide what happens to anything that still doesn't fit. overflow: hidden clips it silently, which suits decorative teasers but can hide meaningful content, so add a visible cue if users need the full text. overflow: auto scrolls when needed. overflow-y: auto with a fixed height is the standard pattern for sidebars of filter labels or changelog entries: the scrollbar appears only when content exceeds the box. If you want the text to reveal on hover, combine a fixed height with a transition, but keep the base state accessible to keyboard and touch users, who cannot hover.

A practical success check: put the longest realistic string (a 60-character URL or a long German compound word) in the box, then the shortest. If neither overflows the edge and a scrollbar only appears for genuinely long content, the fixed-box setup is sound. If a horizontal scrollbar appears, the culprit is usually a non-wrapping token, not the width itself.

Let the Box Grow: Min, Max, and Intrinsic Sizing

If the container does not need a fixed size, the simplest fit is to remove the height and let the box expand. Use min-height instead of height when you need a minimum visual click target or card size: the box will never be smaller than the minimum but can grow to hold every line. This single swap fixes many truncated paragraphs in cards.

For limitable growth, pair max-height with overflow: auto or an expandable disclosure. That gives the box a sensible ceiling in dense feeds while preserving all content. width has a similar shape: max-width: 65ch keeps a paragraph readable by capping its measure around 65 characters, and the box still shrinks on narrow screens. Note that ch is based on the width of the "0" glyph, so it is an approximate measure for proportional fonts, not a guarantee of exactly 65 characters.

Intrinsic keyword sizing is useful when the box should hug its content without explicit measurements. width: fit-content sizes the box to the content's preferred width, clamped by any max-width. width: min-content shrinks to the narrowest the content can wrap, and width: max-content expands to the widest unwrapped layout. These are helpful for pill labels and auto-width tags, but be careful with max-content on long paragraphs: it can overflow its parent horizontally because it refuses to wrap.

For images and text side by side, avoid fixed heights altogether; set images to max-width: 100% and give the text a flexible column. Grid and flex layouts can then distribute the space. If a grid cell uses 1fr for both columns, long text in one can push the layout unless you add min-width: 0 to the text item, because the default minimum size of a grid/flex item is its content's size. That line, min-width: 0, is one of the most useful fixes for overflowing text next to fixed elements.

Fit Single-Line Text: Truncation, Wrapping Alternatives, and Balanced Lines

A single-line label, a title, or a navigation item has different constraints than a paragraph. If the text must occupy one line and never wrap, the classic truncation pattern is white-space: nowrap combined with overflow: hidden and text-overflow: ellipsis. The browser keeps the text on one line, hides anything past the box, and adds an ellipsis. Add max-width or a flex basis so the box has a defined limit; otherwise the text simply overflows the box instead of being truncated.

This pattern is appropriate for previews and short labels, but it hides information. For navigation, prefer shortening the label or allowing wrapping. If wrapping is acceptable, remove white-space: nowrap and use overflow-wrap: break-word for safety.

For multi-line headings, text-wrap: balance asks the browser to distribute lines so the last line isn't a lone word. Support is good in current versions of major browsers, but older browsers ignore it and fall back to normal wrapping, which is harmless. For longer paragraphs, text-wrap: pretty has a similar intent with different trade-offs, but check the behavior at the time you ship. Neither replaces setting a sensible max-width; they refine wrapping, they don't create space.

For hyphenation, hyphens: auto on the appropriate element lets the browser break words at syllable boundaries when a line would otherwise overflow. It helps narrow columns, but it depends on the language attribute; set correctly, and verify the result in the language actually used, since dictionaries differ by locale.

The check for this group is visual and quick: resize the viewport from wide to very narrow and watch for horizontal scrollbars, clipped letters, and unexpected single-word last lines. Horizontal scrollbars are the symptom to treat first.

Responsive Type: Clamp, Truly Fluid Text, and Shrink-to-Fit

Fluid type is the most misused technique for "fitting text." font-size: clamp(1rem, 2.5vw, 1.5rem) sets a minimum, a preferred fluid value, and a maximum. This isn't a shrink-to-fit tool for arbitrary overflowing strings; it adjusts type to the viewport, and inside a narrow card it can still overflow if the card is much narrower than the viewport suggests.

The genuine shrink-to-fit mechanism is font-size based on a container query unit, cqw, where the font scales to the width of the container rather than the viewport. Combined with clamp() for a floor, it can keep a short headline inside a fixed-width card. It's still limited: it scales continuously, ignores the character count, and breaks down for long text. For paragraphs, don't shrink; let them wrap.

Truly fitting an arbitrary string to a fixed rectangle requires either JavaScript measurement or a browser feature that is not available as a general text-fitting CSS property. The honest answer is that CSS has no universal "shrink until it fits" switch for arbitrary text; wrapper units plus a minimum size cover the simple cases, and script-based measurement covers the rest. If you find a library that claims otherwise, check how it handles variable fonts, font loading (a late font swap can change widths), and zoom levels before depending on it.

Non-negotiable accessibility rule: text that shrinks must remain readable. A layout that shrinks a label to 9 pixels to win space is broken even if it "fits." Keep a floor and change the layout instead.

Flex and Grid: Make Interactive Elements Adaptable

Buttons and flex item labels are where fitting fails most often, because the button's width is determined by the label but its parent may constrain it. Start with min-width: 0 on the flex item that holds text; this lets it shrink below its content width so wrapping can happen. Without it, the item refuses to shrink and pushes siblings out of the row.

For buttons, prefer padding and min-height over fixed height, so two-line labels expand the button. Use align-items: center on the flex container for vertical centering, and leave a max-width plus wrapping as the safety valve. If a button's text truly must stay on one line, that requirement should drive a shorter label, not a font size that requires squinting.

In CSS Grid, grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)) is a common card layout. If a card holds long unbroken text, it can overflow its track unless the text has overflow-wrap. Grid also has minmax(0, ...) to allow tracks to shrink; when columns overlap or overflow, the missing minmax(0, 1fr) is often the reason.

Top-to-bottom wrapping and per-word control are not reliably available across browsers as of late 2026, so don't build a product around them; use them only as progressive enhancement. The durable pattern is the same: define the box, allow wrapping, and handle overflow deliberately.

Test Across Text, Browsers, and Users

Fitting checklists fail when they are tested with "lorem ipsum" on a desktop monitor at default zoom. Test with the longest realistic strings your product produces: long names, URLs, non-English words, uppercase labels, and mixed script content. Check at 200% browser zoom and at a 320-pixel viewport width; zoomed layouts often reveal clipped text and unreachable controls.

Test font loading too. A web font can swap in after first paint and be wider than the fallback, causing truncation that wasn't visible at load time. Options include font-display: swap for readability over perfect layout, or font-display: optional if you accept a fallback for the first visit to protect layout stability. Neither removes the need to test the fallback metrics.

Check that overflow is signaled. Keyboard users need visible focus that isn't clipped by overflow: hidden. Screen reader users are not harmed by visual truncation, but sighted users with low vision can be. If the content matters, offer an expanded view rather than an ellipsis.

Troubleshooting: Symptom-to-Cause Checks

Work from the top of this list and stop when the symptom resolves; each step is cheap and reversible.

1: If content overflows vertically, inspect computed height, line-height, padding, and border. Replace height with min-height temporarily and see if the text fits. If it does, the fixed height is the cause. 2: If a single long word or URL overflows horizontally, add overflow-wrap: break-word (and keep the box's width constrained). Confirm the horizontal scrollbar disappears at the narrowest supported width. 3: If a flex or grid row overflows, add min-width: 0 to the text-bearing item or use minmax(0, 1fr) for the track. Recheck at 200% zoom. 4: If text is cut off with an ellipsis unintentionally, remove white-space: nowrap and text-overflow: ellipsis, then decide whether one-line truncation is actually desired. 5: If a scroll container clips focus outlines or dropdowns, switch from overflow: hidden to overflow: auto or move the interactive content outside the clipped wrapper. 6: If the layout is fine on desktop and clipped on mobile, look for a fixed width in pixels on the text container or its parent; prefer max-width and percentages.

Stop and escalate to a design or front-end owner when the fix would require changing page behavior (a required one-line label that no longer fits a 320-pixel screen), when truncation hides legally required or safety-relevant text, or when a third-party embedded widget's text cannot be restyled. Those are product decisions, not CSS tweaks.

A Short Order of Operations to Reuse

Reach for this sequence on any new fitting task. Confirm the element's job: fixed box or growing box. For growing boxes, remove fixed heights, prefer min-height, and cap width with max-width for readability. For fixed boxes, enable wrapping with overflow-wrap: break-word, choose deliberate overflow behavior, and reserve truncation for genuinely expendable text. Use clamp() for headline sizes where viewport or container scaling is appropriate, and keep a readable minimum. Add min-width: 0 to flex and grid text items, and test with long strings at narrow widths and 200% zoom.

Two honest limits are worth remembering. CSS cannot reliably know how many characters fit; layout is determined by font metrics, and those change with fonts, languages, and zoom. And any shrink-to-fit design that ignores readability trades a visible overflow for an invisible accessibility problem. Designing for wrapping and adaptive growth usually beats forcing text into a strict box.