How to Inspect and Control CSS tab-size for Code Blocks in Safari Web Inspector

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: 2 or tab-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: 4 aligns 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-size is an inherited property. However, it only affects elements where white-space formatting preserves tab characters—specifically elements styled with white-space: pre, pre-wrap, or break-spaces. In normal paragraphs where whitespace is collapsed (white-space: normal), tabs are automatically collapsed into single spaces regardless of tab-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:

  1. Launch Safari on macOS.
  2. Open Safari > Settings (or press Cmd + ,) and navigate to the Advanced tab.
  3. Check the box labelled “Show features for web developers”.
  4. 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:

  1. Computed Value Verification: Switch to the Computed tab in the sidebar and search for tab-size. If the computed value displays 8, the element has inherited the browser user-agent default, meaning custom rules have failed to apply due to CSS selector specificity issues.
  2. Overridden Rule Detection: If tab-size: 4 appears 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.

  1. In Safari’s top menu bar, select Develop > Enter Responsive Design Mode (or press Cmd + Option + R).
  2. Select an iPhone device preset (such as iPhone 15 Pro or iPhone SE).
  3. Inspect the rendered code container. If white-space: pre is used, observe whether lines overflow the screen boundaries.
  4. Combine tab-size: 2 with white-space: pre-wrap and word-break: break-word to 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-size property only controls literal tab characters (\t / &#9;). If your markdown parser or syntax highlighter converts tabs into hard space characters (&nbsp; or four space characters) during HTML compilation, tab-size has no effect. Verify the raw source code in the Elements tab.
  • Missing white-space Declaration: If an element lacks white-space: pre, pre-wrap, or break-spaces, WebKit automatically collapses tab characters into a single standard space character. Always ensure white-space is 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;

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.