CSS tab-size Property

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Text & Code

What You’ll Learn

The tab-size property lets you control how wide tab characters appear in preformatted text. It is a simple way to make code blocks easier to read on documentation and tutorial pages.

01

Tab width

Control spacing.

02

Default 8

Browser baseline.

03

Integer

Space count.

04

Length

px, em, rem.

05

pre / code

Common targets.

06

white-space

Pair with it.

Introduction

The tab-size property in CSS allows developers to control the width of tab characters within an element.

By default, the width of a tab character is equivalent to 8 spaces, but this property lets you customize it to better fit the design and readability requirements of your web pages.

Definition and Usage

Apply tab-size to elements that preserve whitespace and contain real tab characters — most often <pre> and <code> blocks in tutorials, docs, and code snippets.

The value can be a positive integer (number of space widths) or a CSS length such as 2rem.

💡
Beginner Tip

tab-size only affects tab characters (\t), not regular spaces. Many code editors indent with tabs, which makes this property especially useful for displaying that code on the web.

📝 Syntax

The syntax for the tab-size property is simple and can be applied to block-level or inline-level elements that display tab characters:

syntax.css
element {
  tab-size: value;
}

Here, value can be a positive integer or a length value that defines the width of the tab character.

Basic Example

pre-tab-size.css
pre {
  tab-size: 4;
}

Syntax Rules

  • Use a positive integer to match common editor settings like 2 or 4 spaces per tab.
  • Use a length value (px, em, rem) for precise control.
  • Pair with white-space: pre or pre-wrap so tabs are preserved.
  • The property is inherited, so you can set it once on a parent container.

Related Properties

  • white-space — preserves spaces and tab characters
  • font-family — monospace fonts improve code readability
  • overflow — handles long code lines in scrollable panels

🎯 Default Value

The default value of the tab-size property is 8, which means the width of a tab character is equivalent to 8 spaces.

⚡ Quick Reference

QuestionAnswer
Default value8
Common values2, 4, 2rem
Works withwhite-space: pre and tab characters in content
Accepted valuesPositive integer or length
Set onpre, code, and other preformatted text
InheritedYes
AnimatableNo

💎 Property Values

ValueExampleDescription
Integertab-size: 4;A positive integer representing the number of spaces a tab character should occupy.
Lengthtab-size: 2rem;A length value (px, em, rem, etc.) that sets the tab width directly.
8 default 4 2 2rem

When to Use tab-size

tab-size helps whenever preformatted text includes tab characters:

  • Documentation sites — Match the tab width used in your source code examples.
  • Tutorial pages — Keep indented JavaScript, Python, or HTML samples aligned and readable.
  • Code playgrounds — Display pasted code with predictable indentation.
  • Terminal output — Preserve tab-aligned columns in log or CLI snippets.

If your HTML uses spaces instead of tabs for indentation, convert the content or keep using spaces — tab-size will not change spacing made with space characters.

👀 Live Preview

Compare the default tab width (8) with a custom setting of tab-size: 4 on the same sample code:

Default (tab-size: 8)
function demo() {
	let a = 1;
	let b = 2;
	return a + b;
}
Custom (tab-size: 4)
function demo() {
	let a = 1;
	let b = 2;
	return a + b;
}

Examples Gallery

Start with the reference 4-space tab setting, try compact 2-space tabs, explore length values, and style a documentation code block.

📜 Code Block Tab Width

Change tab width inside preformatted elements — matching the reference example.

Example 1 — Four-space tabs in a pre element

In this example, we’ll change the tab size to 4 spaces within a <pre> element to improve the readability of code blocks.

index.html
<style>
  pre {
    tab-size: 4;
  }
</style>

<pre>
function example() {
    let x = 1;
    let y = 2;
    return x + y;
}
</pre>
Try It Yourself

How It Works

Each tab character inside the pre element renders with the width of 4 spaces instead of the default 8.

Example 2 — Compact two-space tabs

Use tab-size: 2 when your project follows a 2-space indentation style.

compact-tabs.css
.compact-code {
  white-space: pre;
  tab-size: 2;
  font-family: monospace;
}
Try It Yourself

How It Works

Smaller tab widths reduce horizontal scrolling in narrow layouts while keeping tab-based indentation intact.

📄 Advanced Tab Sizing

Use length values and shared documentation styles for consistent code presentation.

Example 3 — Fixed tab width with rem

Use a length value when you want tab stops to scale with font size.

length-tab-size.css
pre {
  font-size: 0.95rem;
  tab-size: 2rem;
}
Try It Yourself

How It Works

Length values give precise control when integer space counts are not flexible enough for responsive layouts.

Example 4 — Documentation code block styling

Set tab-size on a shared class so every code sample on a docs page uses the same indentation width.

docs-code.css
.docs-code {
  white-space: pre-wrap;
  tab-size: 4;
  font-family: ui-monospace, monospace;
  background: #0f172a;
  color: #e2e8f0;
  padding: 1rem;
  border-radius: 0.5rem;
}
Try It Yourself

How It Works

Because tab-size is inherited, one class on a wrapper can style many nested pre and code elements.

tab-size in the family

tab-size controls tab character width, but the content must preserve whitespace. Pair it with white-space: pre or pre-wrap and a monospace font so code blocks stay aligned and readable.

code-block.css
pre {
  white-space: pre;
  tab-size: 4;
  font-family: ui-monospace, monospace;
}

🧠 How tab-size Works

1

Content contains tab characters

Your text or code includes real tab characters, often pasted from an editor or saved in a pre block.

Content
2

Whitespace is preserved

Use white-space: pre or pre-wrap so the browser keeps tabs instead of collapsing them.

Layout
3

tab-size sets the stop width

CSS tells the browser how wide each tab stop should be using an integer or length value.

CSS rule
=

Readable indented code

Code blocks display with predictable indentation that matches your preferred tab width.

Browser Compatibility

The tab-size property is supported in all modern browsers, including current versions of Chrome, Firefox, Safari, Edge, and Opera. It is always a good practice to test your website across different browsers to ensure compatibility.

Baseline · Modern browsers

Reliable tab-size support

Chrome, Firefox, Safari, Edge, and Opera all support tab-size for preformatted text in current versions.

97% Modern browser support
Google Chrome 21+ · Desktop & Mobile
Full support
Mozilla Firefox 4+ · Desktop & Mobile
Full support
Apple Safari 7+ · macOS & iOS
Full support
Microsoft Edge 79+ · Chromium
Full support
Opera 15+ · Modern versions
Full support

Testing tip

Compare the same code sample with tab-size: 8 and tab-size: 4 in your target browsers to confirm indentation looks right.

tab-size property 97% supported

Bottom line: tab-size is safe to use in modern projects for preformatted text and code blocks.

Conclusion

The tab-size property is a valuable tool for web developers looking to enhance the readability of content that includes tab characters, such as code blocks.

By customizing the width of tab characters, you can make your content more readable and visually appealing. Experiment with different values to see how this property can improve the presentation of your web projects.

💡 Best Practices

✅ Do

  • Match tab-size to your team’s editor indentation setting (2 or 4 spaces)
  • Use white-space: pre or pre-wrap on code containers
  • Apply a monospace font for code snippets
  • Set tab-size on a parent wrapper for shared docs styling
  • Test pasted code that contains real tab characters

❌ Don’t

  • Expect tab-size to change indentation made with regular spaces
  • Forget that default tab width is 8 unless you override it
  • Use negative or zero values — only positive integers and lengths are valid
  • Rely on HTML source indentation alone if the browser collapses whitespace

Key Takeaways

Knowledge Unlocked

Five things to remember about tab-size

Use these points when formatting code on your next tutorial page.

5
Core concepts
02

Tab characters

Not spaces.

Scope
03

Integer or length

Two value types.

Values
04

pre / code

Common targets.

Elements
🔄 05

white-space

Pair with pre.

Companion

❓ Frequently Asked Questions

tab-size sets how wide each tab character (U+0009) appears inside an element. It is especially useful for preformatted text and code blocks.
The default value is 8, which means each tab character is rendered with the width of 8 space characters.
Use it on elements that display tab characters, such as pre, code, or other elements with white-space set to pre or pre-wrap.
Yes. tab-size accepts a positive integer (number of spaces) or a length value such as px, em, or rem.
No. tab-size only affects actual tab characters in the content. Indentation made with regular spaces is not changed by this property.

Practice in the Live Editor

Open the HTML editor and try tab-size: 4; on a pre element that contains tab-indented code.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful