html 缩进标准,制表符还是两个空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5430806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
html indenting standard, tab or two spaces?
提问by Danny
Many websites, for example html5 boilerplate use two spaces. Visual studio default projects such as ASP.NET MVC 3 use a tab. Will one become the standard way of indenting html?
许多网站,例如 html5 样板文件使用两个空格。Visual Studio 默认项目(例如 ASP.NET MVC 3)使用选项卡。会成为缩进html的标准方式吗?
采纳答案by JohnP
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.
我认为没有 W3C 指定的标准。由于 HTML 无论如何都会去除多余的空白,因此您应该使用最适合您的方式。
回答by Jefferson Lima
As nobody mentioned it, the Google HTML/CSS Style Guideand the W3School HTML(5) Style Guiderecommend 2 spaces.
正如没有人提到的那样,Google HTML/CSS 样式指南和 W3School HTML(5) 样式指南推荐了 2 个空格。
This articlealso brings an analysis of the effect of tabs vs spaces in the resulting file size.
该文章还带来了标签的VS在生成的文件大小空间的影响的分析。
回答by Brandon Rhodes
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.
不要使用标签;使用两个空格。标签是有问题的,因为它们在不同的编辑器(和终端,就此而言)可以有不同的宽度。由于在部署前整理 HTML 时会删除空格,因此使用选项卡没有最终的带宽优势;他们只是在您的开发体验中引入了一个通配符。
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.
两个空格正在成为标准,因为它们是“刚好”的缩进,可以使 HTML 在大多数人眼中清晰地缩进,但因为 HTML 往往嵌套得非常深——比编程时通常嵌套的要深得多——而且任何东西都超过两个空格往往会很快将 HTML 推离 80 列屏幕的右边缘。
回答by Myster
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.
警告:如果创建者的制表符宽度与查看者的制表符宽度不同,则行内用于对齐列的制表符可能会未对齐,这就是我使用空格来对齐列的原因。
EditI'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.
编辑我变老了,我不再那么在乎了。尽管我仍然认为标签在上面列出的优势方面更胜一筹。您还应该考虑:最简单(空间)或最广泛采用(在您的社区中,可能不仅仅是您的工作场所),什么对您有用。
回答by Don Question
A spaceis a space, no more, nor less!
一个空间就是一个空间,不多也不少!
A tabis 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:
要回答 OP 问题:NO,我没有看到任何标准出现,因为至少有与以下方面的意见一样多的论点:
- whether to use tab or spaces
- what the correct amount of spaces are (2,4,8, ...)
- 是否使用制表符或空格
- 正确的空格数是多少 (2,4,8, ...)
If you want explicit spacing use Space(or Tab-To-Space conversion)! If you don't care about implicit interpretationof the editors, use raw tabs.
如果您想要显式间距,请使用 Space(或 Tab-To-Space 转换)!如果您不关心编辑器的隐式解释,请使用 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!
恕我直言:TAB 转换减少了打字,但文件中的 TAB 使您成为隐式解释彩票中的候选人。因此我个人使用空格,从不使用制表符!
My suggestion: Use an editor which interprets tabs depending on the context:
我的建议:使用根据上下文解释选项卡的编辑器:
- HTML: 2 spaces
- C: 8 spaces (Torvald's preference)
- Python: 4 spaces
- HTML:2个空格
- C: 8 个空格(Torvald 的偏好)
- Python:4个空格
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.
第 4 次编辑,在第三次重访后,由于投反对票并试图提高我(有意见的)答案的质量。
回答by phantom-99w
I think that the day when indenting HTML becomes a standard practice, there will be much rejoicing, regardless of what the actual standard is.
我认为当缩进 HTML 成为一种标准做法的那一天,无论实际标准是什么,都会有很多欢乐。
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.
我会推荐标签,主要是因为我使用它们并且对它们最满意。但是,您还可以为每个页面节省一些下载字节,并且随着数百万个页面请求,它们加起来。
回答by JensKarlsson
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.
听起来好像没有绝对的答案。
Tab 一直是实际程序编码的标准——是的,它来自“80 字符终端屏幕的时代”,但这并没有让它过时。
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.
一些编辑器以不同方式显示它们的论点是承认编辑器中没有标准,这不应等同于在标记中强制使用特定标准 - 除非有另一个正当理由这样做,例如如果 tab 导致破坏性结果标记。如果您的编辑器强迫您做出某些决定,请尝试使用可以让您始终如一地做事的编辑器,如果这样做是合理的。
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.
归根结底,在出现实际标准之前,请使用有效且普遍接受的标准。目前似乎普遍接受 tab 或两个空格,但由于某些编辑器不会始终如一地显示它而远离 tab 只是编辑器中的一个缺陷或疏忽。
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."
在谈到 C 编程时,公认的(但不是绝对的)权威 Linus Torvalds 曾表示:“制表符是 8 个字符,因此缩进也是 8 个字符。有一些异端运动试图使缩进 4(甚至 2!)字符深,这类似于试图将 PI 的值定义为 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.
是的,那是指 C 而不是 Web 标记,是的,它以一种固执的风格表达了这个概念,但实际提出的观点是有效的。我相信有人可以找到相同概念的许多其他不太丰富多彩的表达方式,但我碰巧手头有这个例子。