是否可以在HTML中插入一个制表符,而不必键入 四次吗?


当前回答

尽管在HTML3中引入了<tab>标签,但它从未进入浏览器。我一直认为这是一种遗憾,因为如果我们拥有它,在许多情况下生活会轻松得多。但是你可以很容易地纠正这个错误,给你一个假的<tab>标签。在你的HTML头部或CSS中添加以下内容(没有样式标签):

<style>
    tab { padding-left: 4em; }
</style>

从那时起,当你需要在你的文档中添加一个选项卡时,把<tab>放在那里。它不是一个真正的制表符,因为它不与制表符标记对齐,但它可以满足大多数需求,而不必为笨拙的字符实体或跨度而犹豫不决。它使检查源代码变得非常容易,并且在你不想使用表格或其他更复杂的“解决方案”的地方格式化简单的东西。

这个解决方案的一个优点是,您可以快速搜索/替换文本文档,用<tab>标记替换所有tab。

你可以定义一堆真正的绝对位置制表符,然后在需要的地方使用适当的制表符(例如<tab2>或<tab5>或其他),但我还没有找到一种方法来做到这一点,而不缩进后续行。

其他回答

p {background-color: #FFEEEE;保证金:0;填充:0.5em 0;} < span style=" font - family:宋体;tab-size: 4 " > & # 9 # 9 & # 9←←一个选项卡两个标签# 9 & # 9 & # 9←三个标签# 9 & # 9 & # 9 & # 9←四个选项卡。< / p > < span style=" font - family:宋体;tab-size: 4">←这是一个字符 < span style=" font - family:宋体;</p> . tab-size: 42">&#9←This one has come out too far.</p> . < span style=" font - family:宋体;tab-size: 8">&#9←说!</p>

太懒了;没有“运行代码”):

通过输入&#9在文本中插入制表符,或者(如果您确定文档是UTF-8编码的)按下制表键! 添加空白:预换行;到文本元素的CSS规则,以阻止空格字符(包括制表符)折叠。 添加tab-size: 4;到你的文本元素的CSS规则,以确保你的制表符宽度等于四个标准空格字符。

(在众多的回答和评论中,每一件事都有一些片段和线索,但没有一个像它的措辞那样把它们放在一起来回答这个问题。)

编辑:不幸的是,堆栈代码段将我的Unicode制表符转换为空格!因此,演示的这一部分(第2行)不起作用。如果通过代码格式化器运行代码,可能会遇到类似的问题。&#9避免了这些问题。

如果你正在使用CSS,我建议如下:

p:first-letter  {
   text-indent:1em;
}

这将像传统出版物一样缩进第一行。

在段落中插入多个空格(或在段落中间)确实没有什么简单的方法。那些建议你使用CSS的人没有抓住重点。您可能不总是试图从一个侧面缩进一个段落,但实际上,尝试在它的特定位置放置额外的空格。

In essence, in this case, the spaces become the content and not the style. I don't know why so many people don't see that. I guess the rigidity with which they try to enforce the separation of style and content rule (HTML was designed to do both from the beginning - there is nothing wrong with occasionally defining style of an unique element using appropriate tags without having to spend a lot more time on creating CSS style sheets and there is absolutely nothing unreadable about it when it's used in moderation. There is also something to be said for being able to do something quickly.) translates to how they can only consider whitespace characters as being used only for style and indentation.

当没有优雅的方式插入空格,而不必依赖&emsp;和,标签,我认为,如果有一个适当命名的标签,允许您快速插入大量的空格(或者如果,您知道,空格一开始就没有被不必要地消耗),那么结果代码将变得更加不可读。

尽管如此,正如上面所说,您最好的选择是使用&emsp;插入:插入正确的位置

您可以使用一个表并将width属性应用于第一个<td>。

<table>
    <tr>
        <td width="100">Content1</td>
        <td>Content2</td>
    </tr>
    <tr>
        <td>Content3</td>
        <td>Content4</td>
    </tr>
</table>

结果

Content1       Content2
Content3       Content4

我们可以像这样使用style="white-space:pre":

<p>Text<tab style="white-space:pre">        </tab>: value</p>
<p>Text2<tab style="white-space:pre">   </tab>: value2</p>
<p>Text32<tab style="white-space:pre">  </tab>: value32</p>

里面的空白处填满了制表符,制表符的数量取决于文本。

它看起来是这样的:

Text    : value
Text2   : value2
Text32  : value32