CSS text wrapping, truncation, and alignment

Quick reference for CSS text-align, wrapping, overflow-wrap versus word-break, and the conditions required for text-overflow.

リファレンスページの本文は現在英語です。

This page covers how text is aligned, wrapped, and truncated. It is not a typography (font) page, and it does not redo Flexbox shrinking or Grid track minimums; those stay on their own pages.

text-overflow: ellipsis is only the truncation glyph. It has no effect unless the text actually overflows in the inline direction, overflow is not visible (usually hidden or clip), and wrapping is prevented (usually white-space: nowrap) with a definite inline size. Writing only text-overflow: ellipsis is not sufficient.

overflow-wrap and word-break are also not interchangeable. overflow-wrap can break a long word when it would otherwise overflow. word-break: break-all may break between letters even when the word could have moved to the next line intact.

Examples were parsed for syntax and structure. Line breaks and ellipsis painting were not measured in a browser.

Official sources: MDN and the CSS specification. Fragments were checked with PostCSS 8 for syntax and structure. That is not a CSS conformance suite, and this is not a browser render or visual test.

CSS reference catalog → · CSS tutorial overview →

Alignment, wrapping, and breaking

text-align affects inline content in a block container. It is inherited. start and end follow writing direction and are preferable to left and right when the document direction can change. text-indent only affects the first line.

Syntax or propertyInitial / defaultInheritedApplies toNotes
text-alignstart (in current CSS; historically left in some user agents)yesblock containersCommon values: start, end, left, right, center, justify. Use start/end when direction may be RTL. justify affects spacing and can change wrapping; it is not a substitute for a typography measure.
text-indent0yesblock containersIndents the first line. Percentages are relative to the containing block’s inline size. A negative value can hang the first line.
white-spacenormalyesall elementsCommon values: normal, nowrap, pre, pre-wrap, pre-line, break-spaces. nowrap prevents wrapping and is usually required for a single-line ellipsis. pre and pre-wrap preserve sequences of spaces and line breaks in the source.
overflow-wrapnormalyesall elementsAlso historically called word-wrap. Common values: normal, anywhere, break-word. Breaks a long word only when needed to prevent overflow. anywhere lets the wrap opportunity affect min-content size; break-word is the older value and does not.
word-breaknormalyesall elementsCommon values: normal, break-all, keep-all. break-all may split typographic letter units even when the word could wrap as a whole. keep-all is often used for CJK to avoid mid-word breaks. Do not treat break-all as a synonym for overflow-wrap.
hyphensmanualyesall elementsmanual, none, or auto. auto hyphenation depends on language tagging and dictionary support. It was not tested here and should not be assumed in every browser or language.
letter-spacingnormalyesall elementsAdditional spacing between typographic character units. A length is added to the font’s own spacing. Large positive or negative values can harm readability.
text-transformnoneyesall elementsCommon values: none, uppercase, lowercase, capitalize. This changes how text is shown, not the characters in the DOM. Use it for presentation, not as the only source of a short heading’s meaning.
text-decorationnone on most elementsnoall elementsShorthand for line, style, and color (and thickness in current CSS). Common values: underline, none. It is not inherited as a shorthand in the same way color is; descendants do not pick up a parent underline automatically in the usual model.

overflow-wrap versus word-break

overflow-wrap: anywhere;
word-break: break-all;

overflow-wrap can break a long token to avoid overflow. word-break: break-all may break earlier, even when a whole-word wrap was possible.

  • The two boxes share a narrow width so a long path can overflow. Breaking was not photographed in a browser.
html
<p class="wrap">Notes: docs/garden/week-01/planting-notes</p>
<p class="all">Notes: docs/garden/week-01/planting-notes</p>
<style>
.wrap, .all { width: 12rem; font-family: sans-serif; }
.wrap { overflow-wrap: anywhere; }
.all { word-break: break-all; }
</style>

What this markup means

Both paragraphs contain the same long path. .wrap uses overflow-wrap: anywhere, which allows a break if the path would overflow the 12rem box. .all uses word-break: break-all, which permits breaks between letters even where a line could have started with a whole word such as “Notes:”. The path is documentation text, not a file read.

Related: Typography, Sizing and overflow

MDN documentation → · CSS specification →

text-overflow is not just ellipsis

The initial text-overflow is clip, which cuts overflowing inline content without a hint character. ellipsis can insert a visual cue, and some engines support a two-value form for the start and end of the line. Those extra values are a compatibility limit and were not tested here.

For a typical single-line ellipsis you need all of the following: a definite inline size (width or max-width, or a flex/grid item that can overflow), overflow other than visible (usually hidden or clip), and no wrapping (usually white-space: nowrap). Scrollable overflow (auto or scroll) can also clip visually, but it changes interaction. Multi-line clamping is often done with a non-standard -webkit-line-clamp pattern and is not a substitute for the standard single-line conditions.

Syntax or propertyInitial / defaultInheritedApplies toNotes
text-overflowclipnoblock containersCommon values: clip, ellipsis. It only affects overflow in the inline direction and only when the box actually overflows. It is not inherited, and it does not by itself create a width, hide overflow, or disable wrapping.

Ellipsis needs overflow, nowrap, and a width

text-overflow: ellipsis; /* not enough */
overflow: hidden;
white-space: nowrap;
max-width: 12rem;

A rule that only sets text-overflow: ellipsis is incomplete. The truncated line also needs a constrained inline size, hidden or clipped overflow, and nowrap.

  • The first rule is the insufficient declaration. The second rule gathers the usual conditions. Painting of the ellipsis was not checked in a browser.
html
<p class="hint-only">docs/garden/week-01/planting-notes</p>
<p class="truncate">docs/garden/week-01/planting-notes</p>
<style>
.hint-only, .truncate { font-family: sans-serif; }
.hint-only { text-overflow: ellipsis; }
.truncate {
  max-width: 12rem;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>

What this markup means

.hint-only sets only text-overflow: ellipsis, so the path can still wrap or extend in visible overflow and no ellipsis is required to appear. .truncate sets max-width, overflow: hidden, and white-space: nowrap with the ellipsis, which is the usual single-line combination. The path is documentation text, not a file read.

Related: Sizing and overflow, Typography

MDN documentation → · CSS specification →