Html 如何获得制表符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9660987/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:03:37  来源:igfitidea点击:

How to get a tab character?

html

提问by Abbey Graebner

In HTML, there is no character for a tab, but I am confused as to why I can copy and paste one here: . (You can't see the full width of it, but if you click to edit my question, you will see the character.) If I can copy and paste a tab character, there should be a unicode equivalent that can be coded into html. I know it doesn't exist, but this is a mystery I've never been able to grasp.

在 HTML 中,标签没有字符,但我很困惑为什么我可以在这里复制和粘贴一个: . (你看不到它的全宽,但如果你点击编辑我的问题,你会看到这个字符。)如果我可以复制和粘贴制表符,应该有一个可以编码成 html 的 unicode 等效项. 我知道它不存在,但这是一个我一直无法理解的谜。

So my question is: why is there not a unicode character for a tab even if I can copy and paste it?

所以我的问题是:为什么即使我可以复制和粘贴选项卡,也没有 unicode 字符?

回答by josh3736

Sure there's an entity for tabs:

当然有一个标签实体:

	

(The tab is ASCII character 9, or Unicode U+0009.)

(制表符是 ASCII 字符 9,或 Unicode U+0009。)

However, just like literal tabs (ones you type in to your text editor), all tab characters are treated as whitespace by HTML parsers and collapsed into a single spaceexcept those within a <pre>block, where literal tabs will be rendered as 8 spacesin a monospace font.

但是,就像文字制表符(您在文本编辑器中输入的那些)一样,所有制表符都被 HTML 解析器视为空格并折叠成一个空格,除了<pre>块内的那些,文字制表符将在一个块中呈现为8 个空格。等宽字体。

回答by Abhishek Goel

Try &emsp;

尝试 &emsp;

as per the docs :

根据文档:

The character entities &ensp;and &emsp;denote an en space and an em space respectively, where an en space is half the point size and an em space is equal to the point size of the current font. For fixed pitch fonts, the user agent can treat the en space as being equivalent to A space character, and the em space as being equuivalent to two space characters.

字符实体&ensp;&emsp;分别表示一个 en 空格和一个 em 空格,其中一个 en 空格是点大小的一半,一个 em 空格等于当前字体的点大小。对于固定间距字体,用户代理可以将 en 空格视为等价于 A 空格字符,将 em 空格视为等价于两个空格字符。

Docs link : https://www.w3.org/MarkUp/html3/specialchars.html

文档链接:https: //www.w3.org/MarkUp/html3/specialchars.html

回答by Raymund

put it in between <pre></pre>tags then use this characters &#9;

把它放在<pre></pre>标签之间然后使用这个字符&#9;

it would not work without the <pre></pre>tags

没有<pre></pre>标签它就无法工作

回答by crokusek

Posting another alternative to be more complete. When I tried the "pre" based answers, they added extra vertical line breaks as well.

发布另一个替代方案以更完整。当我尝试基于“pre”的答案时,他们还添加了额外的垂直换行符。

Each tab can be converted to a sequence non-breaking spaces which require no wrapping.

每个选项卡都可以转换为不需要换行的序列不间断空格。

"&nbsp;&nbsp;&nbsp;&nbsp;" 

This is not recommended for repeated/extensive use within a page. A div margin/padding approach would appear much cleaner.

不建议在页面内重复/广泛使用。div 边距/填充方法会显得更干净。

回答by GrayFace

I use <span style="display: inline-block; width: 2ch;">&#9;</span>for a two characters wide tab.

<span style="display: inline-block; width: 2ch;">&#9;</span>用于两个字符宽的制表符。

回答by Duane Lortie

As mentioned, for efficiency reasons sequential spaces are consolidated into one space the browser actually displays. Remember what the ML in HTML stand for. It's a Mark-up Language, designed to control how text is displayed.. not whitespace :p

如前所述,出于效率原因,顺序空间被合并为浏览器实际显示的一个空间。记住 HTML 中的 ML 代表什么。它是一种标记语言,旨在控制文本的显示方式......不是空格:p

Still, you can pretend the browser respects tabs since all the TAB does is prepend 4 spaces, and that's easy with CSS. either in line like ...

不过,您可以假装浏览器尊重制表符,因为制表符所做的只是在前面加上 4 个空格,这对 CSS 来说很容易。要么像......

 <div style="padding-left:4.00em;">Indenented text </div>

Or as a regular class in a style sheet

或者作为样式表中的常规类

.tabbed {padding-left:4.00em;}

Then the HTML might look like

那么 HTML 可能看起来像

<p>regular paragraph regular paragraph regular paragraph</p>
<p class="tabbed">Indented text Indented text Indented text</p>
<p>regular paragraph regular paragraph regular paragraph</p>

回答by DanRedux

Tab is [HT], or character number 9, in the unicode library.

Tab 是 [HT],或字符编号 9,在 unicode 库中。