How to get a tab character?

后端 未结 7 1632
有刺的猬
有刺的猬 2020-11-29 22:49

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 q

相关标签:
7条回答
  • 2020-11-29 23:08

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

    0 讨论(0)
  • 2020-11-29 23:10

    Sure there's an entity for tabs:

    	
    

    (The tab is ASCII character 9, or 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 space except those within a <pre> block, where literal tabs will be rendered as 8 spaces in a monospace font.

    0 讨论(0)
  • 2020-11-29 23:14

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

    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.

    0 讨论(0)
  • 2020-11-29 23:18

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

    0 讨论(0)
  • 2020-11-29 23:21

    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

    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 ...

     <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

    <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>
    
    0 讨论(0)
  • 2020-11-29 23:22

    Try &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.

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

    0 讨论(0)
提交回复
热议问题