Skip

Tab stops on the web

Did you know that you can use tab stops in HTML? In this article I want to explore this ancient typographic concept and see how it can be used on the web.

What are tab stops?

Somehow the word ā€œtabā€ is extremely overloaded in tech. We have browser tabs, tabbed interfaces in general, tab keys, we tab between form fields… but what I want to talk about are tab stops.

Tab stops. The way you’d use them in a word processor; its name originating from mechanical gears in typewriters that would stop the carriage at certain positions when the tab key is pressed.

Screenshot of Pages on macOS showing a tab stops in the ruler.
How tab stops look in a word processor (Pages on macOS)
Mechanical typewriter with a red tab key in the top right corner.
A tab key on a mechanical typewriter (photo by Nik)

Making tab stops visible

Insert a tab character (U+0009) in your HTML, and it looks just like a space. In fact, that’s a tab character between ā€œaā€ and ā€œspaceā€. Boring!

But wait, if you prevent collapsing white-space with the white-space property, tabs become visible. By default, HTML collapses all white-space characters. Setting white-space: pre preserves these characters, including tabs.

Now we can use them
and see how they behave.

And wow, they are not just wide spaces! The width of a single tab stop depends on the text before it.

Interesting!

Interactive demo

Here’s an interactive demo where you can play around with tab stops and their size:

This text has tab stops.
The width adjusts depending on the text length.
It can be huge when the text before is short, and it doesn’t care about text breaking to a new line.
Interesting!

Two CSS properties make this work. The first is the aforementioned white-space to preserve the tab characters:

white-space: pre;

You could set it on the parent element, but this will preserve all kinds of white space in there. Not want we want. The trick is wrapping just the tab character in a span and applying the magic there. This way the rest of the text behaves normally.

The second property is tab-size, which defines how wide a tab stop is:

tab-size: 8; /* default (8 spaces) */
tab-size: 2rem; /* okay */
tab-size: 50cqw; /* huh? */

The tab-size property accepts any length unit, and here I used cqw which is relative to the container’s width:

.tab-stops-demo {
  container-type: inline-size;
}

tab-stop {
  white-space: pre;
  tab-size: 50cqw; /* Tab jumps to 50% of container width */
}

For this article I’ve built a tiny web component that inserts a tab character and applies all of these styles:

This text <tab-stop></tab-stop> has tab stops.
This <tab-stop size="12"></tab-stop> is a tab stop with custom size.

Ancient technology meets modern CSS!

Don’t try this at home

Don’t get me wrong, this is extremely experimental. Tabs aren’t responsive in any traditional sense and the alignment can break on smaller screens. I’d have to test how different screen readers treat tab stops. And how do users even input tabs in a CMS when the tab key navigates between fields? Maybe a separate button?

For actual layouts flex or grid are obviously a better pick. For tabular (ā€œconsisting of or presented in columns or tablesā€) data, use a <table>, duh. But maybe there’s a use case for this, who knows?

If I’ll ever find a use case, I’ll document it here:

šŸ¦—chirp
šŸ¦—
chirp

Replies

  • Thomas Günther

    I've also added a replies section to my articles. Replies to my Mastodon posts are displayed automatically šŸ¤—

  • Artem R šŸ‡ŗšŸ‡¦

    @medienbaecker curious what kind of solution is used to link Mastodon replies to post? Is it open source

  • Thomas Günther

    @asci Itā€˜s actually just a very simple version of blog.thms.uk/2023/02/mastodon-
    In the backend I add the Mastodon post URL to articles, everything else is in handled in the frontend. I can share the details in a future article if you’re interested šŸ‘

  • Philipp

    @medienbaecker Maybe there’s some use case for this in typesetting poetry?

  • Thomas Günther

    @pb That’s a great idea, Philipp! I’ll have to reach out to an old client of mine for this use case; a perfect example: praeposition.com/text/archiv/s

  • Nathan Manceaux-Panot

    @medienbaecker Love the little things—the space in the sharing image, the ā€œsurprise, the previous sentence contained a tabā€, and also the live demo.

    Really wonder what the intent was in adding these to the standard—if they just felt like it just had to be there, of it it made more sense back in the day!

  • Thomas Günther

    @Cykelero Thanks for the kind feedback, Nathan! Yeah, I think it’s definitely one of these features reminding us of the ā€žtext document with hyperlinksā€œ past of the web.