In technical documentation websites, developer blogs, online IDEs, and code review interfaces, presenting formatted source code with readable indentation is critical. When rendering code snippets inside HTML <pre> and <code> elements, whitespace preservation is typically achieved using the CSS property white-space: pre or pre-wrap. However, by default, web browsers treat tab characters (\t) with an antiquated legacy default width: 8 character spaces.
An 8-space tab width forces nested code lines, JSON trees, and YAML files to stretch far beyond the viewport, causing unnecessary horizontal scrollbars and breaking responsive layouts on mobile devices. Through the CSS Text Module Level 3 property tab-size (and its legacy WebKit prefix -moz-tab-size / -webkit-tab-size), frontend developers can customize tab indentation to standard modern widths, such as 2 or 4 spaces, or explicit typographic lengths. This guide explains how tab-size functions in Apple’s WebKit rendering engine, how to inspect and edit tab rendering in Safari Web Inspector, and how to optimize code presentation across desktop and mobile browsers.
Understanding the CSS tab-size Property in WebKit
The tab-size property dictates how many space characters or physical typographic units the rendering engine allocates to render a single tab character (Unicode U+0009). Key syntax rules include:
- Integer Numbers (Default Recommendation): A unitless positive integer (e.g.
tab-size: 2ortab-size: 4). This instructs the browser to render each tab character with a width equal to that number of space characters (U+0020) in the current font face. For example,tab-size: 4aligns code indentation to four spaces. - Length Values: Explicit typographic lengths such as pixels, rems, or ch units (e.g.
tab-size: 2ch,tab-size: 1.5rem). Length values allow fixed typographic grid alignment regardless of character glyph metrics. - Inheritance and Preformatted Containers:
tab-sizeis an inherited property. However, it only affects elements where white-space formatting preserves tab characters—specifically elements styled withwhite-space: pre,pre-wrap, orbreak-spaces. In normal paragraphs where whitespace is collapsed (white-space: normal), tabs are automatically collapsed into single spaces regardless oftab-size.
Apple’s Safari supports both modern un-prefixed tab-size as well as legacy -webkit-tab-size. In modern Safari on macOS, iOS, and iPadOS, standard syntax is fully operational.
Enabling and Opening Safari Web Inspector
To inspect and fine-tune tab rendering in Safari, ensure developer features are active:
- Launch Safari on macOS.
- Open Safari > Settings (or press
Cmd + ,) and navigate to the Advanced tab. - Check the box labelled “Show features for web developers”.
- Navigate to the web page featuring code blocks or preformatted text, right-click the code snippet, and select Inspect Element.
Web Inspector will dock to your screen, highlighting the selected <pre> or <code> element in the DOM tree while displaying computed styles and matched CSS rules in the Styles panel.
Inspecting tab-size Declarations in the Styles Sidebar
Select the target preformatted code element in the DOM tree. In the right-hand Styles sidebar, locate the matching CSS rule:
pre, code {
font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
white-space: pre-wrap;
tab-size: 4;
-webkit-tab-size: 4;
}
When inspecting this rule in Safari Web Inspector:
- Computed Value Verification: Switch to the Computed tab in the sidebar and search for
tab-size. If the computed value displays8, the element has inherited the browser user-agent default, meaning custom rules have failed to apply due to CSS selector specificity issues. - Overridden Rule Detection: If
tab-size: 4appears with a strikethrough line in the Styles tab, another CSS stylesheet (such as a CSS reset or syntax highlighter theme) is overriding the declaration with higher specificity.
Testing Different Tab Indentation Widths Live
Safari Web Inspector allows real-time editing of CSS properties without modifying server-side stylesheets. Click inside the declaration block in the Styles tab to test alternative tab widths.
Switching from 4-Space to 2-Space Indentation
In modern web development, JavaScript, TypeScript, HTML, and JSON frequently standardise on 2-space indentation. Double-click the tab-size value in the Styles panel and change it to 2:
tab-size: 2;
Observe how deeply nested code blocks instantly contract across the horizontal plane. This reduces the risk of horizontal scrollbars cutting off comments or forcing line wraps on narrow mobile screens.
Using Typographic ch Units for Monospace Alignment
While integer values calculate widths based on the font’s space glyph, using ch units explicitly ties tab widths to the width of the zero glyph (0). In monospace fonts, ch guarantees exact character column grid alignment:
tab-size: 4ch;
In Web Inspector, you can click into the numeric value and use the Up Arrow and Down Arrow keys to increment or decrement the unit, observing character column alignment in real time.
Inspecting Tab Indentation on Responsive Mobile Viewports
Code snippets that display cleanly on large 27-inch desktop monitors often break on mobile screens. Safari provides a built-in Responsive Design Mode to test tab formatting across iPhone and iPad viewports.
- In Safari’s top menu bar, select Develop > Enter Responsive Design Mode (or press
Cmd + Option + R). - Select an iPhone device preset (such as iPhone 15 Pro or iPhone SE).
- Inspect the rendered code container. If
white-space: preis used, observe whether lines overflow the screen boundaries. - Combine
tab-size: 2withwhite-space: pre-wrapandword-break: break-wordto verify that tabs indent neatly without forcing awkward character wrapping.
Troubleshooting Common Code Block Formatting Gotchas
When tab formatting fails to display accurately in Safari, check for these common frontend issues:
- Soft Spaces vs Hard Tabs: The
tab-sizeproperty only controls literal tab characters (\t/	). If your markdown parser or syntax highlighter converts tabs into hard space characters ( or four space characters) during HTML compilation,tab-sizehas no effect. Verify the raw source code in the Elements tab. - Missing white-space Declaration: If an element lacks
white-space: pre,pre-wrap, orbreak-spaces, WebKit automatically collapses tab characters into a single standard space character. Always ensurewhite-spaceis explicitly declared on parent code containers. - Vendor Prefix Fallbacks for Older Browsers: While modern Safari, Chrome, and Firefox support un-prefixed
tab-size, providing vendor prefixes ensures backward compatibility across legacy browsers:-moz-tab-size: 2; -webkit-tab-size: 2; tab-size: 2;