Skip to main content

CSS Text Spacing

CSS has several properties for controlling the spacing of text.

In this chapter you will learn about: text-indent, letter-spacing, line-height, word-spacing, and white-space.


CSS Text Indentation

The text-indent property sets the indentation of the first line in a block of text.

Tip: Negative values are allowed. A negative value indents the first line to the left.

Example

The first line of the paragraph is indented by 50px.

Prompt
An HTML page that shows the text-indent property. In an internal <style> block, indent the first line of a <p> by 50px. Add a paragraph with a few sentences.
Output: complete HTML page

CSS Letter Spacing

The letter-spacing property sets the space between the characters in a text.

Tip: Negative values are allowed.

Example

One heading has wider letter spacing and the other has tighter spacing.

Prompt
An HTML page that shows the letter-spacing property. In an internal <style> block, give the <h1> 5px letter-spacing and the <h2> -2px letter-spacing. Add a heading of each level.
Output: complete HTML page

CSS Line Height

The line-height property sets the space between lines.

Note: Negative values are not allowed.

Example

One paragraph has a small line height and the other a large one.

Prompt
An HTML page that shows the line-height property. In an internal <style> block, make a paragraph class with line-height 0.8 (tight) and one with line-height 1.8 (loose). Give each paragraph several lines of text so the spacing difference is visible.
Output: complete HTML page

CSS Word Spacing

The word-spacing property sets the space between the words in a text.

Tip: Negative values are allowed.

Example

One paragraph has wider word spacing and the other has tighter spacing.

Prompt
An HTML page that shows the word-spacing property. In an internal <style> block, make a paragraph class with word-spacing 10px (wider) and one with word-spacing -2px (tighter). Add a paragraph of text for each.
Output: complete HTML page

CSS White Space

The white-space property sets how the white space inside an element is handled. It can take these values: normal, nowrap, pre, pre-line, and pre-wrap.

Example

With nowrap, the text stays on one line instead of wrapping.

Prompt
An HTML page that shows the white-space property used to stop text wrapping. In an internal <style> block, set a <p> to white-space: nowrap so the text stays on one line instead of wrapping. Add a long paragraph of text.
Output: complete HTML page

The CSS Text Spacing Properties

A summary of the spacing properties:

  • letter-spacing — sets the space between characters.
  • line-height — sets the line height.
  • text-indent — sets the indentation of the first line.
  • white-space — sets how white space inside an element is handled.
  • word-spacing — sets the space between words.