What is the Indentation standard for HTML (Tab / Two spaces / etc)?

Html

Html Problem Overview


Many websites, with pre-generated HTML5-compliant boilerplate files, use two spaces. Visual Studio projects, such as ASP.NET MVC 3, default to using a tab.

Will one become the standard way of indenting HTML?

Html Solutions


Solution 1 - Html

As nobody mentioned it, the Google HTML/CSS Style Guide and the W3School HTML(5) Style Guide recommend 2 spaces.

This article also brings an analysis of the effect of tabs vs spaces in the resulting file size.

Solution 2 - Html

Do not use tabs; use two spaces. Tabs are problematic because they can be a different width in different editors (and terminals, for that matter). Since whitespace is removed when you tidy your HTML before deployment, there is no ultimate bandwidth benefit to using tabs; they simply introduce a wildcard into your development experience.

Two spaces are emerging as the standard because they are “just enough” indentation to make the HTML clearly indented to most people's eyes, but because HTML tends to nest very deeply — much more deeply than one would commonly nest when programming — and anything more than two spaces tends to start pushing HTML off the right edge of an 80-column screen pretty quickly.

Solution 3 - Html

There is no standard but...
Use Tabs, it's what they're for.
Advantages: fewer bytes, the user can set their preferred display width.

Warning: tabs used within lines to align columns will potentially be miss-aligned if the tab width of the creator differs from that of the viewer, which is why I use spaces for aligning columns.

Edit I'm getting older, and I just don't care so much anymore. Although I still consider tabs superior in their advantages listed above. You should also consider: the simplest (spaces), or widest adoption (in your community, possibly wider than just your workplace), what works for you.

Solution 4 - Html

I don't think there's a W3C specified standard. Since HTML strips out excess whitespace anyway, you should go with whatever you're most comfortable with.

Solution 5 - Html

A space is a space, no more, nor less!

A tab is a sign to jump to the next column, but "next column" depends on the interpretation of the currently used editor/program.

To answer the OP question: NO, I don't see any standard emerging, because there are at least as many arguments, as there are opinions about:

  1. whether to use tab or spaces
  2. what the correct amount of spaces are (2,4,8, ...)

If you want explicit spacing use Space (or Tab-To-Space conversion)! If you don't care about implicit interpretation of the editors, use raw tabs.

IMHO: TAB-conversion reduces typing, but TABs in a file makes you a candidate in the Implicit-Interpretation-Lottery. Therefore I personally use spaces, never tabs!

My suggestion: Use an editor which interprets tabs depending on the context:

  • HTML: 2 spaces
  • C: 8 spaces (Torvald's preference)
  • Python: 4 spaces

So I personally concur with Mr. Rhodes' reasoning. Most editors provide an option for tab to space conversion, and I strongly suggest to use it.


4th edit, after revisiting a 3rd time, because of a down-vote and trying to improve the quality of my (opinionated) answer.

Solution 6 - Html

I think that the day when indenting HTML becomes a standard practice, there will be much rejoicing, regardless of what the actual standard is.

I would suggest tabs, primarily because I use them and am most comfortable with them. But you also save a few bytes from being downloaded for each page, and with millions of page requests, they add up.

Solution 7 - Html

It sounds as though there is no absolute answer.
Tab has been a standard in actual program coding - and yes that comes from " the days of 80-character terminal screens," but that does not make it obsolete.

As someone said above, "Use Tabs, it's what they're for."

The argument that some editors display them differently is an admission that there is no standard in editors and that should not equate to forcing a particular standard in marking up - unless there is another valid reason to do so such as if tab caused a destructive result to the markup. If your editor forces you to make certain decisions try using an editor that lets you do things consistently, if it is reasonable to do so.

At the end of the day, until an actual standard emerges, use what works and is generally accepted. It seems like tab or two spaces are both generally accepted at the moment, but leaning away from tab because some editors will not display it consistently is just a flaw or oversight in the editor.

The "just one keystroke" argument can sound lazy, but actually its more efficient which is one point for that method.

Referring to C programing, Linus Torvalds, a recognized (but not absolute) authority, has stated: "Tabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4(or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3."

Yes, that is referring to C and not web markup, and yes it expresses the concept with an opinionated flavor, but the actual point made is valid. I am confident that someone could find many other less colorful expressions of the same concept, but I happened to have this example at hand.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDannyView Question on Stackoverflow
Solution 1 - HtmlJefferson LimaView Answer on Stackoverflow
Solution 2 - HtmlBrandon RhodesView Answer on Stackoverflow
Solution 3 - HtmlMysterView Answer on Stackoverflow
Solution 4 - HtmlJohnPView Answer on Stackoverflow
Solution 5 - HtmlDon QuestionView Answer on Stackoverflow
Solution 6 - Htmlphantom-99wView Answer on Stackoverflow
Solution 7 - HtmlJensKarlssonView Answer on Stackoverflow