Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 58 ยท Content

Text Utilities

Align, wrap, truncate and transform text with Bootstrap 5.3 utilities: text-center, text-md-end, text-truncate, text-break, text-uppercase, lh-*, font-monospace.

The Typography lesson covered headings, lists and sizes. This one covers the everyday adjustments you make to a piece of text after the font is chosen: align it left, center it or send it right; stop a long word from breaking the layout; cut a long title down to one line with an ellipsis; turn a label into capital letters; change the line spacing; or show code in a monospace font.

Each of these is a single utility class, and most have responsive versions. By the end of the lesson you will be able to read a string of classes like text-center text-md-start text-uppercase fw-semibold at a glance.

Text alignment

Three classes set the horizontal alignment of text inside a block:

Class Meaning
text-start aligned to the start of the line (left in English)
text-center centered
text-end aligned to the end of the line (right in English)

Bootstrap 5 uses start and end instead of left and right (Bootstrap 4 had text-left and text-right). The reason is right-to-left languages like Arabic and Hebrew: with dir="rtl" on the page, text-start follows the reading direction automatically.

These classes align the text and inline content inside an element; they do not move the element itself. A block-level <div> is already as wide as its parent, so text-center centers what is inside it.

Responsive alignment

Every alignment class takes a breakpoint, as the grid classes do. text-md-center means "centered from the md breakpoint (768 px) upward". Because Bootstrap is mobile-first, you write the small-screen alignment without a breakpoint and override it at larger sizes:

HTML
<h2 class="text-center text-md-start">Centered on phones, left-aligned from md</h2>
Responsive text alignment

Resize the window across 768 px and 992 px and watch the last two boxes switch. This is a very common pattern for footers: centered text on phones, and a left column plus a right column on desktops.

Wrapping and breaking

Normally a browser wraps text at spaces. Sometimes that is not what you want, and sometimes there are no spaces to wrap at.

  • text-wrap allows normal wrapping (white-space: normal). Use it to override text-nowrap at another breakpoint.
  • text-nowrap keeps text on one line, even if it overflows its box. Use it for labels and prices that must not split.
  • text-break lets very long words, such as a URL or a file path, break at any character instead of sticking out of the container.
Wrap, nowrap and break

In the third box the long URL sticks out of the dashed border, and in the fourth text-break folds it inside. Whenever users can enter or link long strings (comments, usernames, file names), add text-break to the container as a cheap protection against horizontal scrolling; see the overflow checklist in Sizing Utilities.

Truncating text

text-truncate cuts text that does not fit on one line and shows an ellipsis (โ€ฆ). It sets overflow: hidden, text-overflow: ellipsis and white-space: nowrap. There is one condition: the element must be a block or inline-block with a limited width. Otherwise it simply grows to fit its content and nothing is cut.

text-truncate

The first heading is cut at the width of its card. In the second example the span is d-inline-block with a max-width, which gives text-truncate something to cut against. Hovering to see the full text is not available on touch screens, so keep the complete text somewhere reachable, or avoid truncating important content.

Truncation covers one line only. Cutting text after two or three lines needs the CSS line-clamp property in your own stylesheet; Bootstrap has no utility for it.

Changing the case

Three classes change letter case visually, without touching the text in the HTML:

  • text-lowercase shows all letters in lowercase.
  • text-uppercase shows all letters in capitals.
  • text-capitalize capitalizes the first letter of each word. It does not lowercase the rest, so "hELLO" becomes "HELLO".

Uppercase is popular for small labels and buttons. Keep the HTML in normal sentence case: screen readers handle it better, and search results and copied text stay readable. Long passages in all capitals are hard to read, so use them sparingly.

Line height

Line height is the vertical space for each line of text. Bootstrap's body text has a line height of 1.5, which suits paragraphs. Four utilities override it:

Class Line height
lh-1 1 (tight, for big headings)
lh-sm 1.25
lh-base 1.5 (the default)
lh-lg 2 (very airy)

Display headings often look better with lh-1, while long articles read more easily with lh-base or slightly more.

Monospace and other helpers

font-monospace switches to the system's monospace font stack, for code-like text and numbers that should line up. For actual code in an article, prefer the <code> and <pre> elements, which are already monospace; see HTML Computer Code.

Other helpers are worth knowing:

  • text-decoration-none, text-decoration-underline and text-decoration-line-through control underlines. A typical use is removing the underline from a link that is styled as a button or card title.
  • text-reset makes a link take the text color of its parent. It is useful for a whole card that is clickable, where blue link text would look wrong.
  • text-break and text-wrap from earlier.
Case, line height, monospace and decoration

Compare the three paragraphs side by side: with the same text, lh-1 packs lines tightly and lh-lg leaves a full blank line between them.

flowchart LR
  A["Long text problem"] --> B{"What should happen?"}
  B -->|"Keep on one line, cut with ..."| C["text-truncate<br/>(needs a width)"]
  B -->|"Keep on one line, may overflow"| D["text-nowrap"]
  B -->|"Fold unbroken strings"| E["text-break"]
  B -->|"Wrap normally"| F["text-wrap"]

Which class to use when text is too long for its box.

Combine alignment with the grid. A form's labels often use text-md-end so that they sit against their inputs on wide screens and stay left-aligned on phones. You will build that layout in Form Layout and Validation.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does text-md-end do?

    Responsive utilities use min-width queries: the class applies at md (768 px) and every larger breakpoint.
  2. 2. Why does text-truncate show no ellipsis on a span with no width limit?

    text-truncate needs a block or inline-block element with a limited width, for example d-inline-block with a max-width.
  3. 3. A comment contains a 90-character URL with no spaces and pushes the page wider than the screen. Which class helps most?

    text-break lets long unbroken strings wrap at any character.
  4. 4. Why is it better to use text-uppercase than to type a label in capital letters?

    The class only changes how the text is displayed; the underlying text is unchanged.

Summary

  • text-start, text-center and text-end align text; add a breakpoint (text-md-end) to change it at larger screens. Bootstrap 5 uses start and end, not left and right.
  • text-wrap, text-nowrap and text-break control wrapping; text-truncate makes a single-line ellipsis on a width-limited block.
  • text-lowercase, text-uppercase and text-capitalize change the displayed case only.
  • lh-1, lh-sm, lh-base and lh-lg set the line height; font-monospace gives the monospace font stack.
  • text-decoration-* and text-reset tidy links and clickable blocks.

Next lesson: Colors and Backgrounds.