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.
Fundamentals
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.
Foundation
📝 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.
overflow — handles long code lines in scrollable panels
Defaults
🎯 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.
Cheat Sheet
⚡ Quick Reference
Question
Answer
Default value
8
Common values
2, 4, 2rem
Works with
white-space: pre and tab characters in content
Accepted values
Positive integer or length
Set on
pre, code, and other preformatted text
Inherited
Yes
Animatable
No
Reference
💎 Property Values
Value
Example
Description
Integer
tab-size: 4;
A positive integer representing the number of spaces a tab character should occupy.
Length
tab-size: 2rem;
A length value (px, em, rem, etc.) that sets the tab width directly.
8 default422rem
Context
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.
Preview
👀 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;
}
Hands-On
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>
All documentation snippets share consistent 4-space tab stops with readable monospace styling.
How It Works
Because tab-size is inherited, one class on a wrapper can style many nested pre and code elements.
Companion
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.
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.
Compatibility
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 Chrome21+ · Desktop & Mobile
Full support
Mozilla Firefox4+ · Desktop & Mobile
Full support
Apple Safari7+ · macOS & iOS
Full support
Microsoft Edge79+ · Chromium
Full support
Opera15+ · 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 property97% supported
Bottom line:tab-size is safe to use in modern projects for preformatted text and code blocks.
Wrap Up
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.