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.
pre { tab-size: 4; }
Try it
The tab characters are written as 	, 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
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-wraporbreak-spaces). Withpre-lineandnormal, 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
4chis similar, butpxstays fixed. - Inherited. Set it once on
preor onbodyfor all code blocks. - Spaces are not affected. If a code sample is indented with spaces,
tab-sizechanges nothing. - Textareas. Browsers treat
<textarea>aspre-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.
Related
white-spaceโ preserve tabs and line breakstext-indentโ indent the first lineword-breakโ control breaking in long lines- Lesson: Wrapping, Overflow and Hyphens