CSS tab-size Property

The CSS tab-size property specifies the width of tab characters when whitespace is preserved in an HTML element.

It is commonly used with <pre> elements, code examples, and other preformatted text. The property controls how horizontal tab characters (U+0009) are displayed without changing the actual text or replacing tabs with spaces.

tab-size Syntax

The tab-size property accepts a non-negative number or length.

Basic Syntax

pre {
  tab-size: 4;
}

Number Values

pre {
  tab-size: 0;
}
pre {
  tab-size: 2;
}
pre {
  tab-size: 4;
}
pre {
  tab-size: 8;
}

Number values represent tab-stop intervals measured in multiples of the space character's advance width.

Length Values

pre {
  tab-size: 20px;
}
pre {
  tab-size: 2em;
}
pre {
  tab-size: 1.5rem;
}
pre {
  tab-size: 3ch;
}

Length values specify a physical tab-stop interval in CSS length units rather than a multiple of the space character's width.

CSS-Wide Keywords

pre {
  tab-size: initial;
}
pre {
  tab-size: inherit;
}
pre {
  tab-size: unset;
}
pre {
  tab-size: revert;
}
pre {
  tab-size: revert-layer;
}

The initial value is 8, meaning tab stops are spaced at intervals based on eight space-character widths.

Property Values

Value Description
<number> A non-negative number specifying tab-stop spacing as a multiple of the space character's advance width.
<length> A non-negative CSS length specifying the interval between tab stops.
initial Resets the property to 8.
inherit Uses the computed tab size from the parent element.
unset Uses the inherited value because the property is inherited.
revert Reverts the property according to CSS cascade origin rules.
revert-layer Reverts the property according to CSS cascade layer rules.

Property Characteristics

Characteristic Value
Initial value 8
Inherited Yes
Applies to All elements, with a visible effect when tab characters are preserved
Computed value Specified number or computed length
Animation type By computed value type
Percentages Not accepted
Related properties white-space, white-space-collapse, font-family
Specification CSS Text Module Level 3

Valid Examples

pre {
  tab-size: 2;
}
code {
  tab-size: 4;
}
pre {
  tab-size: 0;
}
pre {
  tab-size: 24px;
}
pre {
  tab-size: 3ch;
}

Invalid Examples

pre {
  tab-size: -2;
}
pre {
  tab-size: -10px;
}
pre {
  tab-size: 50%;
}
pre {
  tab-size: auto;
}
pre {
  tab-size: 4 8;
}

Negative values, percentages, unsupported keywords, and multiple values are invalid.

How It Works

A tab character advances the text position to the next tab stop. The tab-size property determines the spacing between those stops.

Step 1: Create Preformatted Text

The following HTML contains tab characters between the words. In the source code, each gap is an actual tab character rather than a sequence of spaces.

<pre>Name	Role
Alex	Designer
Sam	Developer</pre>

Step 2: Set the Tab Size

pre {
  tab-size: 4;
}

Tab stops are spaced at intervals based on four space-character widths.

Step 3: Change the Tab Size

pre {
  tab-size: 8;
}

Increasing the value spreads the tab stops farther apart.

Important Consideration

A tab character does not always occupy exactly four or eight visible spaces. It advances to the next tab stop, so the distance depends on the text that appears before it.

Understanding Tab Stops

Tab stops are regularly spaced positions along a line of text. When the browser encounters a preserved tab character, it advances to the next tab stop.

Tab Size of Two

.tabs-two {
  tab-size: 2;
}

Tab stops are spaced at intervals of two space-character widths.

Tab Size of Four

.tabs-four {
  tab-size: 4;
}

Tab stops are spaced at intervals of four space-character widths.

Tab Size of Eight

.tabs-eight {
  tab-size: 8;
}

Tab stops are spaced at intervals of eight space-character widths.

Tab Size of Zero

.tabs-zero {
  tab-size: 0;
}

A value of 0 causes tab characters to occupy no horizontal advance.

Comparing Tab Sizes

Value Tab-Stop Interval
0 No horizontal tab advance
2 Two space-character widths
4 Four space-character widths
8 Eight space-character widths
20px 20 CSS pixels

Tab Stops Are Not Fixed Spaces

For example, with tab-size: 4, a tab following one character advances farther than a tab following three characters, assuming a monospace font and ordinary character widths.

Important Consideration

Tab stops are measured from the line's starting position. Their behavior is different from inserting a fixed number of space characters after every word.

tab-size and white-space

The tab-size property is useful only when tab characters are preserved during whitespace processing.

Default Whitespace Processing

.normal-text {
  white-space: normal;
  tab-size: 4;
}

With white-space: normal, tabs are collapsed as part of normal whitespace processing, so the specified tab size does not produce visible tab spacing.

Preserved Whitespace

.preformatted-text {
  white-space: pre;
  tab-size: 4;
}

With white-space: pre, tabs and other whitespace are preserved.

Preformatted Text With Wrapping

.wrapped-code {
  white-space: pre-wrap;
  tab-size: 4;
}

Tabs remain preserved, while long lines may wrap when necessary.

Preserve Line Breaks Without Tabs

.line-breaks-only {
  white-space: pre-line;
  tab-size: 4;
}

The pre-line value preserves line breaks but collapses tabs and ordinary spaces, so tab size has no visible effect.

white-space Comparison

white-space Value Tabs Preserved? tab-size Visible?
normal No No
nowrap No No
pre Yes Yes
pre-wrap Yes Yes
pre-line No No
break-spaces Yes Yes

Using white-space-collapse

.preserved-tabs {
  white-space-collapse: preserve;
  tab-size: 4;
}

The white-space-collapse property can independently control whitespace preservation in browsers that support the relevant CSS Text features.

Important Consideration

The <pre> element normally preserves whitespace through its browser default styling, making it a convenient choice for demonstrating tab-size.

Numbers vs. Length Values

Number and length values define tab-stop spacing differently.

Number Values

.number-tabs {
  tab-size: 4;
}

A number uses the advance width of the space character in the element's font. This means the spacing can change when the font changes.

Pixel Values

.pixel-tabs {
  tab-size: 32px;
}

A pixel value specifies tab-stop intervals using CSS pixels.

Em Values

.em-tabs {
  tab-size: 2em;
}

An em value is based on the element's font size.

Ch Values

.ch-tabs {
  tab-size: 4ch;
}

The ch unit is based on the advance measure of the 0 character in the font. It does not necessarily equal the width of a space character.

Value Comparison

Value Measurement
4 Four space-character widths
32px 32 CSS pixels
2em Twice the element's font size
4ch Four times the advance measure of the 0 character

Choosing a Value Type

Use number values for conventional code indentation. Use length values when a specific visual interval is required.

Important Consideration

Number and length values are not automatically equivalent. Their rendered spacing depends on font metrics and the selected units.

Code Indentation

The tab-size property is especially useful for displaying source code containing actual tab characters.

Two-Column Indentation

pre code {
  tab-size: 2;
}

Creates compact tab-stop spacing for code examples.

Four-Column Indentation

pre code {
  tab-size: 4;
}

Provides a common tab-stop interval for source code.

Eight-Column Indentation

pre code {
  tab-size: 8;
}

Uses the initial tab-stop interval.

Monospace Fonts

pre code {
  font-family: Consolas, Monaco, monospace;
  tab-size: 4;
}

A monospace font gives characters consistent advance widths, making code indentation easier to follow.

Tabs vs. Spaces

The property affects actual tab characters only. It does not change the width of ordinary spaces used for indentation.

Important Consideration

Changing tab-size changes the visual presentation of tab characters, not the indentation characters stored in the source file.

Practical Examples

Compact Code Block

.compact-code {
  white-space: pre;
  tab-size: 2;
}

Displays preserved tab characters using compact tab-stop spacing.

Standard Code Block

.standard-code {
  white-space: pre;
  tab-size: 4;
}

Provides a conventional four-space-width tab-stop interval.

Wide Code Indentation

.wide-code {
  white-space: pre;
  tab-size: 8;
}

Uses wider tab-stop spacing for preformatted text.

Responsive Code Block

.responsive-code {
  white-space: pre;
  tab-size: 4;
  overflow-x: auto;
  max-width: 100%;
}

Preserves code indentation while allowing horizontal scrolling when lines are too long.

Wrapped Preformatted Text

.wrapped-pre {
  white-space: pre-wrap;
  tab-size: 4;
  overflow-wrap: anywhere;
}

Preserves tabs while allowing long text to wrap.

Fixed-Length Tab Stops

.fixed-tabs {
  white-space: pre;
  tab-size: 24px;
}

Sets tab stops at intervals of 24 CSS pixels.

Inherited Tab Size

.code-container {
  tab-size: 4;
}

Descendant elements inherit the tab size unless they specify another value.

Reset Tab Size

.default-tabs {
  tab-size: initial;
}

Restores the initial value of 8.

tab-size Example

This example compares three code blocks containing the same tab characters. The only difference is the tab-size value.

HTML Example

Each indentation character in the example below is an actual tab character.

<div class="tab-size-demo">
  <div class="tab-example">
    <h4>tab-size: 2</h4>
    <pre class="tabs-2">function example() {
	console.log("Hello");
}</pre>
  </div>
  <div class="tab-example">
    <h4>tab-size: 4</h4>
    <pre class="tabs-4">function example() {
	console.log("Hello");
}</pre>
  </div>
  <div class="tab-example">
    <h4>tab-size: 8</h4>
    <pre class="tabs-8">function example() {
	console.log("Hello");
}</pre>
  </div>
</div>

CSS Example

.tab-size-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}
.tab-size-demo .tab-example {
  min-width: 0;
  padding: 12px;
  border: 1px solid #cbd5e1;
  background: #f8fafc;
}
.tab-size-demo h4 {
  margin: 0 0 10px;
  font-size: 14px;
}
.tab-size-demo pre {
  margin: 0;
  padding: 10px;
  font-family: Consolas, Monaco, monospace;
  font-size: 12px;
  white-space: pre;
  overflow-x: auto;
  background: #fff;
}
.tab-size-demo .tabs-2 {
  tab-size: 2;
}
.tab-size-demo .tabs-4 {
  tab-size: 4;
}
.tab-size-demo .tabs-8 {
  tab-size: 8;
}
Browser Result

tab-size: 2

function example() {
	console.log("Hello");
}

tab-size: 4

function example() {
	console.log("Hello");
}

tab-size: 8

function example() {
	console.log("Hello");
}

The first code block uses compact indentation, the second uses moderate indentation, and the third uses wider indentation. The source text is otherwise identical.

Comparing the Results

Code Block tab-size Appearance
First 2 Compact tab spacing
Second 4 Moderate tab spacing
Third 8 Wide tab spacing

Changing to a Length Value

.tab-size-demo .tabs-4 {
  tab-size: 24px;
}

The second code block now uses tab stops spaced 24 CSS pixels apart.

Removing Tab Spacing

.tab-size-demo .tabs-4 {
  tab-size: 0;
}

The tab characters in the second code block no longer create horizontal advance.

Collapsing Whitespace

.tab-size-demo pre {
  white-space: normal;
}

Tab characters are collapsed into ordinary whitespace, so the different tab-size values no longer produce visible indentation differences.

Changing the Font

.tab-size-demo pre {
  font-family: Arial, sans-serif;
}

The width of the space character changes with the font, which can affect tab-stop spacing when number values are used.

Accessibility Considerations

Tab spacing affects the visual organization of preformatted text and code examples.

Keep Code Readable

Choose tab sizes that make nested code structures easy to follow without creating excessive horizontal scrolling.

Support Zoom and Small Screens

Ensure preformatted text remains usable when users zoom the page or view it on narrow screens.

Use Appropriate Overflow Handling

pre {
  tab-size: 4;
  overflow-x: auto;
  max-width: 100%;
}

Horizontal scrolling can preserve code formatting when long lines do not fit within the available width.

Avoid Tabs for Page Layout

Do not use tab characters to align unrelated page content or create multi-column layouts. Use semantic HTML and CSS layout systems instead.

Preserve Source Meaning

Changing tab-size affects presentation only. It does not alter the text content that assistive technologies or copy operations receive.

Use Semantic Markup

Use <pre> and <code> for preformatted code examples rather than relying on spacing alone to identify source code.

Common Mistakes

Using Negative Values

pre {
  tab-size: -4;
}

Tab sizes must be non-negative.

Using Percentages

pre {
  tab-size: 50%;
}

The property accepts numbers and lengths, not percentages.

Using Unsupported Keywords

pre {
  tab-size: auto;
}

The auto keyword is not a valid value.

Expecting Tabs to Work With Collapsed Whitespace

When tabs are collapsed by whitespace processing, changing tab-size has no visible effect.

Confusing Tabs With Spaces

The property affects tab characters, not ordinary space characters.

Assuming Every Tab Equals a Fixed Number of Spaces

Tab characters advance to the next tab stop. The distance depends on the current position in the line.

Assuming All Fonts Produce Identical Spacing

Number values depend on the space character's advance width, which varies between fonts.

Using Tabs for Complex Alignment

Tab stops are not a replacement for CSS Grid, Flexbox, or HTML tables when creating structured layouts.

Ignoring Horizontal Overflow

Wide tab spacing can cause preformatted text to overflow narrow containers. Use appropriate wrapping or scrolling behavior.

Expecting Source Code to Change

The property changes visual tab spacing only. It does not convert tabs to spaces or modify the underlying text.

Important Considerations

Keep the following points in mind when using tab-size:

  • It controls the spacing between tab stops used by preserved tab characters.
  • The initial value is 8.
  • The property is inherited.
  • It accepts non-negative numbers and lengths.
  • Negative values are invalid.
  • Percentages are not accepted.
  • Number values are based on the space character's advance width.
  • Length values use CSS length units.
  • A value of 0 produces no horizontal tab advance.
  • Tab characters advance to the next tab stop.
  • A tab does not always occupy the same number of visible spaces.
  • It affects actual tab characters, not ordinary spaces.
  • Whitespace must be preserved for the property to have a visible effect.
  • The <pre> element normally preserves whitespace.
  • The white-space property affects whether tabs are preserved.
  • Monospace fonts are generally preferable for code examples.
  • Different fonts can produce different tab spacing.
  • The property does not modify the underlying text content.
  • It does not convert tabs into spaces.
  • It does not control indentation created with CSS padding or margins.
  • Large tab sizes may cause horizontal overflow.
  • It is useful for code blocks and preformatted text.
  • It should not be used as a replacement for CSS layout systems.
  • Test tab spacing across fonts, browsers, and responsive sizes.

Browser Support

The CSS tab-size property is widely supported in modern browsers, including Chrome, Edge, Firefox, and Safari.

Number values have broad support. Length values may have different compatibility histories and should be tested when supporting older browsers.

For current compatibility information, visit Can I Use? and search for tab-size.

Feature Detection

@supports (tab-size: 4) {
  pre {
    tab-size: 4;
  }
}

Feature queries can test whether a browser recognizes the property and value syntax.

Legacy Firefox Prefix

pre {
  -moz-tab-size: 4;
  tab-size: 4;
}

Older Firefox versions used the -moz-tab-size prefix. Modern Firefox supports the unprefixed property.

Fallback Behavior

If a declaration is invalid or unsupported, the browser ignores it. The element uses another applicable value from the cascade or its inherited value, falling back to 8 when no other value applies.

Testing Recommendations

Test number and length values, different fonts, preserved whitespace, code indentation, and horizontal overflow in the browsers you support.

Specifications

The tab-size property is defined in the CSS Text Module Level 3 specification.

CSS Text Module Level 3 - tab-size Property

The specification defines accepted values, tab-stop calculations, inheritance, and how tab characters are rendered when whitespace is preserved.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: tab-size