Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Typography

The tab-size property

Reference for the CSS tab-size property: set the width of a tab character with a number of spaces or a length. Live example, usage notes and browser support.

The tab-size property sets how wide a tab character (U+0009) is drawn. Browsers use a tab width of 8 spaces by default, which is wide for code. Setting tab-size: 2 or 4 makes indented code compact and easier to read.

A tab only appears if the whitespace is preserved, so the property matters inside elements such as <pre> or elements with white-space: pre or pre-wrap. In normally collapsing text, a tab is turned into a single space.

CSS
pre {
  tab-size: 4;
}

Try it

Tab widths of 8, 4 and 2

The tab characters are written as &#9;, which the browser turns into real tabs inside <pre>. The indentation shrinks from 8 to 4 to 2 columns while the text stays the same.

Syntax

CSS
tab-size: <number> | <length>;
Initial value 8
Applies to block containers
Inherited yes
Animatable yes, as a number or length, but animating it is rarely useful
Computed value the specified number, or the absolute length

Values

Value Description
<number> A tab is as wide as this many space characters (the width of U+0020, plus any letter-spacing and word-spacing). Must not be negative. 0 is allowed.
<length> A tab is this wide, for example 2ch, 24px or 1.5em. Must not be negative.

Usage notes

  • Tabs must survive. The property has no effect unless tab characters are preserved by white-space (pre, pre-wrap or break-spaces). With pre-line and normal, tabs collapse.
  • Tabs align to tab stops. A tab moves the text to the next multiple of the tab width measured from the start of the line, so a tab after three characters is narrower than a tab at the start of a line. With a monospace font, this gives the same columns as in your editor.
  • Number versus length. A number depends on the font of the element, so it adapts to font size. A length such as 4ch is similar, but px stays fixed.
  • Inherited. Set it once on pre or on body for all code blocks.
  • Spaces are not affected. If a code sample is indented with spaces, tab-size changes nothing.
  • Textareas. Browsers treat <textarea> as pre-wrap, so tab-size applies to it too, although pressing Tab moves focus rather than inserting a tab unless a script handles it.

Browser support

Supported in all current browsers, using the unprefixed property. Very old versions of Firefox and Safari needed prefixed names, which you no longer need.