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 ----------------------------------
- 003 Everthing looks good so far ------
The text is “pre-formatted” and contains a lot of whit spaces and dashes and every line has the same length (all chars have the same width).
Is there a way to display the text in html without losing the format?
The <pre> tag defines preformatted text. Text in a <pre> element is displayed in a fixed-width font, and the text preserves both spaces and line breaks. The text will be displayed exactly as written in the HTML source code.
The <pre> HTML element represents preformatted text which is to be presented exactly as written in the HTML file. The text is typically rendered using a non-proportional, or monospaced, font.
HTML Formatting basically refers to the enhancing of text in order to increase the visual appeal. HTML provides a range of formatting tags that can be used to make the text attractive to the users. There are many options available that can be used for formatting, just like any other text editor.
HTML Formatting is a process of formatting text for better look and feel. HTML provides us ability to format text without using CSS. There are many formatting tags in HTML. These tags are used to make text bold, italicized, or underlined.
Wrap your text inside the <pre>
tag.
JSFiddle
<pre>
+-001 This is a Line 00:12:04
002 ----------------------------------
- 003 Everthing looks good so far ------
</pre>
The HTML way is to use the pre
element, which has been designed for such usage, but beware that
<pre>
tag right at the start of the first line and the </pre>
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.<
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):
<pre>+-001 This is a Line 00:12:04
002 ----------------------------------
- 003 Everthing looks good so far ------
- 004 Simple as 1 + 1 < 3 ------</pre>
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).
<xmp>+-001 This is a Line 00:12:04
002 ----------------------------------
- 003 Everthing looks good so far ------
- 004 Simple as 1 + 1 < 3 ------</xmp>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With