Skip to main content

You can change the size of tabs on web pages with the tab-size property

I’m going to be writing more Go soon, and that means I’ll want to write posts about Go. I’ll want to include snippets of Go code, and my Go code will probably be indented with tabs.

That causes an issue with the default browser stylesheet, which uses 8 spaces for tabs – I think that looks quite wide. I prefer 4 space indents (which is also used by the Go documentation).

package main

import "fmt"

func main() {
	fmt.Println("hello world")
}

I could replace the tabs with spaces, but that means the Go code in my posts won’t copy cleanly into a working Go program. That feels icky.

I found a better fix: there’s a tab-size CSS property which lets me customise the width of tabs. I can specify an exact width (e.g. tab-size: 10px) or as a number of spaces (e.g. tab-size: 4).

This reduces the indentation in my code blocks, and makes them more readable:

package main

import "fmt"

func main() {
	fmt.Println("hello world")
}

(And a quick bit of spelunking reveals that’s the same approach used by the Go documentation.)