Set the width of a tab character with CSS

Use the tab-size property in CSS to set the width of a tab character. This property determines how many spaces a tab character should take up when displaying text content inside <pre> elements or other elements with preserved whitespace.

Syntax

selector {
    tab-size: value;
}

Possible Values

Value Description
number Specifies the number of spaces for a tab character (default is 8)
length Specifies the tab width in units like px, em, rem

Example

The following example sets the tab character width to 12 spaces −

<!DOCTYPE html>
<html>
<head>
    <style>
        .normal-tab {
            tab-size: 8; /* Default tab size */
            background-color: #f0f0f0;
            padding: 10px;
            margin-bottom: 20px;
        }
        .custom-tab {
            tab-size: 12; /* Custom tab size */
            background-color: #e0f2ff;
            padding: 10px;
        }
        pre {
            border: 1px solid #ccc;
            font-family: 'Courier New', monospace;
        }
    </style>
</head>
<body>
    <h3>Default Tab Size (8 spaces)</h3>
    <pre class="normal-tab">Name:		John Doe
Age:		30
City:		New York</pre>
    
    <h3>Custom Tab Size (12 spaces)</h3>
    <pre class="custom-tab">Name:		John Doe
Age:		30
City:		New York</pre>
</body>
</html>
Two text blocks are displayed. The first shows tab characters with default 8-space width, and the second shows the same content with wider 12-space tabs, making the alignment more spread out.

Conclusion

The tab-size property controls the visual width of tab characters in preformatted text. It's particularly useful for code formatting and maintaining consistent indentation in text displays.

Updated on: 2026-03-15T13:20:52+05:30

226 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements