Skip

Tab stops on the web

Changelog
  • Improved writing and added a link to the Technology Connections video about typewriters to the “What are tab stops?” section. I urge you to watch it, it’s fantastic!

Somehow the word ā€œtabā€ is extremely overloaded in tech. We have browser tabs, tabbed interfaces in general, tab keys, we tab between form fields, drink tab water but what I want to tap into talk about in this article are tab stops and how they can be used on the web.

What are tab stops?

Tab stops, the way you’d use them in a word processor. Their name originating from the admittedly uninspiredly named ā€œtabsā€ in typewriters that would stop the carriage at certain positions.

While doing my research I stumbled over this incredible video by Technology Connections that focuses on a completely different feature, but also explains the carriage and tab stops in the best way I’ve seen. Did you know many typewriters didn’t have a ā€œ1ā€ key so people would just use a lowercase ā€œlā€ instead and type exclamation marks with an apostrophe and a period? My web accessibility heart is crying.

Anyways, back to the topic of tab stops on the web.

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, they actually work.

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

🤯

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.