🔠 CSS tab-size
— Controlling the Width of Tabs in Text
Make your code blocks, text files, and preformatted content look cleaner and more consistent.
🔹 What is tab-size
?
The CSS tab-size
property controls the width of tab characters (\t
) in text. It’s especially useful when displaying:
- Code blocks
<pre>
formatted text- Monospaced fonts
By default, tabs may appear too wide or inconsistent across browsers. tab-size
lets you standardize and control that appearance.
📘 Syntax
selector {
tab-size: <number> | <length>;
}
Accepts:
- Number: Number of space characters (e.g.
2
,4
,8
) - Length: Custom length (e.g.
4ch
,2em
)
✍️ Example: Formatting Code Snippets
<pre class="code-block">
function hello() {
let name = "world";
console.log("Hello, " + name);
}
</pre>
.code-block {
font-family: monospace;
tab-size: 2;
}
🧩 This will render each \t
(tab) as 2 spaces wide, making the code block tighter and easier to read.
🖼️ Image 1: Visualizing Tab Sizes
Description:
- Three stacked
<pre>
blocks showing identical code withtab-size: 2
,4
, and8
. - The difference in indentation width is clearly shown.
🎯 Purpose: Show how tab-size
affects visual spacing in text content.
🧠 Why Use tab-size
?
Use Case | Benefit |
---|---|
Code editors or blog posts | Make indentation consistent and readable |
<pre> content or ASCII art | Maintain visual structure |
Markdown renderers | Display tabs uniformly across browsers |
🔄 Default Behavior vs Custom tab-size
Tab Width | Looks Like (Indented) |
---|---|
tab-size: 8 | →→→→→→→→ (wide tabs, default in many browsers) |
tab-size: 4 | →→→→ |
tab-size: 2 | →→ |
✅ Smaller tab-size
values improve readability in code-heavy pages.
✍️ Using tab-size
with length
pre {
tab-size: 3ch; /* Each tab equals 3 character widths */
}
You can use relative sizing based on font size or character width (em
, ch
, etc.).
🔄 Cross-Browser Support
- ✅ Supported in all modern browsers: Chrome, Firefox, Edge, Safari.
- 🛑 Not supported in very old versions of Internet Explorer.
✅ Summary
Feature | Description |
---|---|
Property | tab-size |
Applies to | Elements with tab characters (usually <pre> , code) |
Common values | Numbers like 2 , 4 , 8 |
Best used with | Monospaced fonts and code content |
🔚 Final Thought
The tab-size
property may be small, but it plays a big role in creating readable, neat, and accessible text—especially in technical and educational content. A few tweaks can make your content feel clean and intentional.