Html display formatted text

后端 未结 3 521
暖寄归人
暖寄归人 2021-02-13 04:21

I’ve to display a bunch of text on an html page. The text looks something like this:

+-001 This is a Line            00:12:04
  002 -----------------------------         


        
3条回答
  •  -上瘾入骨i
    2021-02-13 04:58

    The HTML way is to use the pre element, which has been designed for such usage, but beware that

    • To be on the safe side in formatting, put the
       tag right at the start of the first line and the 
      tag right at the end of the last line. Otherwise some browsers may behave as if there were an empty line at the start or at the end of the element.
    • You still need to escape occurrences of the characters < and & in the content (there are some cases where this is not needed, but it is simplest to ignore that.

    Example (where I have added a line containing the expression 1 + 1 < 3):

    +-001 This is a Line            00:12:04
      002 ----------------------------------
    - 003 Everthing looks good so far ------
    - 004 Simple as 1 + 1 < 3         ------

    You can avoid escaping < and & by using xmp instead of pre, but xmp has been declared deprecated, obsolete, and whatever (but it works nicely in practice).

    +-001 This is a Line            00:12:04
      002 ----------------------------------
    - 003 Everthing looks good so far ------
    - 004 Simple as 1 + 1 < 3         ------
    

提交回复
热议问题