CSS

🎯 CSS tab-size Property (How Tab Width Works in CSS)

The tab-size property in CSS defines how wide a tab character appears inside an HTML element.

It controls how many spaces a tab represents when using preformatted text or code blocks.

This property is especially useful for improving code readability and maintaining consistent indentation in <pre> elements.

🔹 What Is It Used For?

The tab-size property is mainly used in code blocks and preformatted text.

It helps developers control indentation width for better readability and structure.

🔹 Basic Usage

CSS

pre {
  tab-size: 4;
}
                

➡️ In this example, each tab character is displayed as the width of 4 spaces.

🔸 Values

1. number

Defines how many spaces a tab character represents.

2. length

Sets tab width using units like px, em, or rem.

CSS

pre {
  tab-size: 20px;
}
                

➡️ This makes indentation visually wider using fixed pixel spacing.

3. initial

Resets the property to default value (usually 8 spaces).

4. inherit

The element inherits the tab-size value from its parent.

👀 Visual Example

tab-size: 2;

if (true) {
  console.log("Hi");
}
                
tab-size: 8;

if (true) {
        console.log("Hi");
}
                

💡 Summary Table

Value Description
number Defines how many spaces a tab equals
length Sets tab width using px, em, rem
initial Resets to default (usually 8 spaces)
inherit Inherits value from parent element
  • 🧠 Summary

  • The tab-size property controls the width of tab characters in text.
  • It is mainly used in code blocks and preformatted content.
  • Developers usually prefer values like 2 or 4 for better readability.