HTML formatted text

Ive been 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 ---------------------------------- - 003 Everthing looks good so far ------ 

The text is “pre-formatted” and contains many spaces and dashes, and each line has the same length (all characters have the same width).

Is there a way to display text in html without losing format?

+6
source share
3 answers

Wrap the text inside the <pre> .

Jsfiddle

 <pre> +-001 This is a Line 00:12:04 002 ---------------------------------- - 003 Everthing looks good so far ------ </pre> 
+15
source

The HTML way is to use a pre element that was designed for such use, but be careful that

  • To stay clear of formatting, place the <pre> at the beginning of the first line and the </pre> at the end of the last line. Otherwise, some browsers may behave as if there was an empty line at the beginning or end of the element.
  • You still need to avoid the occurrence of the characters < and & in the content (there are times when this is not necessary, but the simplest to ignore it.

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

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

You can avoid escaping < and & with xmp instead of pre , but xmp been declared deprecated, deprecated and whatever (but it works well in practice).

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

Try wrapping the content with a PRE tag. http://www.w3schools.com/tags/tag_pre.asp

+1
source

Source: https://habr.com/ru/post/952085/


All Articles