Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 139 ยท Text and Fonts

Styling Text

Style text with CSS color, text-align, text-decoration, text-transform, letter-spacing and word-spacing, including modern underline offset and thickness.

Text is the heart of almost every web page, so the properties that shape it are among the ones you will use most. With a handful of declarations you can color words, line them up, underline them with style, switch their case and adjust the space between letters.

This lesson covers the "everyday" text properties: color, text-align, the full text-decoration family, text-transform, letter-spacing and word-spacing. Choosing and loading typefaces comes next, in Fonts and Web Fonts.

Text color

The color property sets the color of an element's text. It also becomes the element's currentColor, which borders, outlines, SVG icons and text decorations use by default. You will learn every way to write a color in Colors; for now, hex codes and named colors are enough.

color is inherited, so setting it on body or a wrapper colors everything inside, unless a child sets its own value.

Color and inheritance

The heading and first paragraph inherit the dark blue-gray from .card, and so does the border. Only the warning paragraph overrides it.

Contrast matters. Light gray text on a white background looks elegant in a mockup and is unreadable on a phone in sunlight. Aim for a contrast ratio of at least 4.5:1 for body text. The DevTools color picker shows the ratio for you.

Aligning text

text-align controls how lines of inline content sit inside their block. The common values are:

  • start (the default) and end โ€“ the start or end of the line in the current writing direction. In English that means left and right; in Arabic or Hebrew it flips automatically.
  • left and right โ€“ physical sides, regardless of language.
  • center โ€“ centers each line.
  • justify โ€“ stretches the spaces so every line except the last touches both edges.
text-align values

Prefer start and end over left and right; they keep working when your page is translated into a right-to-left language. You will see more of these logical values in Writing Modes and Logical Properties.

text-align aligns the content inside a block, not the block itself. Writing text-align: center on an image's parent centers the image because images are inline. It will not center a <div> with a fixed width; for that, see Centering Anything.

Justified text looks tidy in print, but on narrow screens it often creates "rivers" of white space. If you use it, pair it with hyphenation, covered in Wrapping, Overflow and Hyphens.

Last lines, indents, justification and direction

text-align has a few companions that handle the details it leaves out.

The last line: text-align-last

With text-align: justify, the last line of a paragraph is left alone. text-align-last sets how that final line (and any line that ends with a forced break) is aligned. It accepts the same values as text-align: start, end, center, justify and so on. Center the last line of a justified quote, or justify a one-line label so it spans its box.

Choosing how to justify: text-justify

text-justify picks the method the browser uses when text-align: justify is active. inter-word adds the extra space between words, which suits English. inter-character spreads it between letters, which suits languages written without spaces, such as Japanese. auto lets the browser choose and none turns justification off. Browser support has been uneven over the years, so treat it as a refinement: the paragraph is still justified if the value is ignored.

Indenting: text-indent

text-indent indents the first line of a block, the way printed books start paragraphs. Use a length such as 1.5em, or a percentage of the box width. A negative indent pulls the first line outward; combined with matching padding-left, it creates a hanging indent, handy for bibliographies. text-indent is inherited, so setting it on a container indents the first line of every block inside that container's text.

Cutting off overflowing text: text-overflow

text-overflow: ellipsis replaces clipped text with an ellipsis, but only when the text really overflows and the box clips it (overflow other than visible, and white-space: nowrap for a single line). The full recipe, plus multi-line clamping, is in Wrapping, Overflow and Hyphens.

Writing direction: direction and bdo

direction: rtl makes inline content flow right to left, and text-align: start then follows it. For a whole page, prefer the HTML dir attribute: it carries meaning (screen readers and search engines can use it) while CSS direction is purely visual. The HTML <bdo dir="rtl"> element overrides the direction for one run of text, and the CSS equivalent is unicode-bidi: bidi-override. For vertical scripts and logical properties, see Writing Modes and Logical Properties.

Last line, indent and direction

text-indent accepts the keywords hanging and each-line in some browsers, but support is limited, so the negative-indent trick above is the dependable approach.

Text decoration

text-decoration draws lines on text. It is a shorthand for four properties:

Property What it controls Example values
text-decoration-line Which line(s) none, underline, overline, line-through
text-decoration-style The line's style solid, double, dotted, dashed, wavy
text-decoration-color The line's color any color; defaults to currentColor
text-decoration-thickness How thick the line is auto, from-font, 2px, 0.1em

You can write all of them in one declaration, in any order:

CSS
a {
  text-decoration: underline wavy #d0367a 2px;
}

Two related properties fine-tune underlines specifically:

  • text-underline-offset moves the underline further from (or closer to) the text.
  • text-decoration-skip-ink decides whether the line breaks around descenders such as g, p and y. The default, auto, skips them.
Decoration playground

Be careful removing link underlines. Underlines are the most widely recognized signal that text is clickable. If you remove them, make links stand out some other way (a clearly different color and weight, for example), and keep an underline on hover and focus. Styling Buttons and Links covers this in depth.

A common modern link style keeps the underline but makes it thinner, offset and softer in color, then strengthens it on hover:

CSS
a {
  text-decoration-thickness: 1px;
  text-underline-offset: 0.2em;
  text-decoration-color: color-mix(in srgb, currentColor 40%, transparent);
}
a:hover {
  text-decoration-color: currentColor;
}

Changing case with text-transform

text-transform changes how letters are displayed without changing the text in the HTML:

  • uppercase โ€“ ALL CAPITALS.
  • lowercase โ€“ all small letters.
  • capitalize โ€“ The First Letter Of Each Word.
  • none โ€“ show the text as written (useful to undo an inherited value).

This keeps your content and your design separate. A navigation label written as "Contact us" in the HTML can appear as "CONTACT US" on the page, and screen readers still read "Contact us" naturally instead of spelling out what might look like an acronym.

text-transform

The console shows the difference: textContent returns the source text, while innerText reflects the rendered, transformed text.

capitalize is naive. It uppercases the first letter of every word, including "a" and "of", and it never lowercases anything. It is fine for short labels but not a replacement for proper title case in your content.

Letter and word spacing

letter-spacing adds (or removes) space between characters, and word-spacing adds space between words. Both accept lengths; em is the best unit because the spacing then scales with the font size.

CSS
.eyebrow  { letter-spacing: 0.1em; }    /* airy uppercase labels */
.headline { letter-spacing: -0.02em; }  /* tighten big display text */

Some guidelines that hold up well:

  • Uppercase text usually needs extra spacing. Capitals were designed to sit next to lowercase letters; in a row they feel cramped. Around 0.05emโ€“0.1em helps.
  • Large headings often look better slightly tighter. A small negative value such as -0.01em to -0.03em closes the gaps that appear at large sizes.
  • Leave body text alone. The font's designer already spaced it for reading.
  • Avoid big word-spacing changes. They break up the rhythm of a sentence quickly.
Spacing in practice

Try setting the heading's letter-spacing to 0 and then to 0.1em to see how much the mood changes.

Putting it together

Here is a small article header that uses every property from this lesson. Edit freely: change the accent color in one place and watch the decorations follow, thanks to currentColor.

Article header

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is text-align: start usually better than text-align: left?

    start is a logical value: left in English, right in Arabic or Hebrew. left always means the physical left side.
  2. 2. Which declaration draws a thick, red, wavy underline?

    The text-decoration shorthand accepts the line, style, color and thickness in any order. The other properties don't exist or only accept one kind of value.
  3. 3. You write "Contact us" in the HTML and style it with text-transform: uppercase. What does a screen reader typically read?

    text-transform only changes the display. The underlying text stays as written, which is exactly why it's better than typing capitals into your HTML.
  4. 4. Which unit is the best choice for letter-spacing?

    Spacing in em stays proportional when the font size changes, so the same rule works for small labels and big headings.
  5. 5. A <div class="box"> with a fixed width sits inside a parent that has text-align: center. What happens?

    text-align is inherited and aligns inline content inside each block. A block-level box isn't inline content, so it doesn't move; use auto margins or flexbox to center it.

Summary

  • color sets the text color, is inherited, and becomes currentColor for borders and decorations.
  • text-align aligns inline content inside a block; prefer the logical start and end values.
  • text-align-last, text-indent, text-justify and direction refine alignment: last line, first-line indent, justification method and writing direction.
  • text-decoration is a shorthand for line, style, color and thickness; text-underline-offset moves underlines away from the text.
  • Keep links recognizable if you restyle or remove their underline.
  • text-transform changes case visually while the HTML text stays the same.
  • letter-spacing (in em) opens up uppercase labels and tightens big headings; leave body text alone.

Next: Fonts, where you choose typefaces, sizes and weights and learn the font shorthand.