Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display element as preformatted text via CSS [duplicate]

Is there any way to emulate the display of a pre element via CSS?

For example, can I keep the whitespace intact in this div by using a CSS rule?

<div class="preformatted">    Please procure the following items:      - Soup     - Jam     - Hyphens      - Cantaloupe </div> 
like image 779
Dagg Nabbit Avatar asked Mar 17 '12 20:03

Dagg Nabbit


People also ask

How do I show preformatted text in HTML?

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.

How do I wrap text in a pre tag?

HTML <pre> tag defines preformatted text. It is used to display code blocks since it preserves spaces and line breaks. If the line is large, then the <pre> tag won't wrap it by default. To wrap it, we need to use CSS.

Why pre tag is not working?

You need to make sure the code is formatted correctly, the pre tag tells the browser to show the text inside the pre "as is".


2 Answers

Use white-space: pre to preserve whitespace preformatting as in the pre element. To go a step further, also add font-family: monospace, as pre elements are typically set in a monospace font by default (e.g. for use with code blocks).

.preformatted {      font-family: monospace;      white-space: pre;  }
<div class="preformatted">      Please procure the following items:        - Soup      - Jam      - Hyphens       - Cantaloupe  </div>
like image 195
BoltClock Avatar answered Sep 18 '22 10:09

BoltClock


.preformatted {    white-space: pre-wrap; } 

I think "pre-wrap" is better. It can help with long length in line.

like image 34
Kondrashenkov Anton Avatar answered Sep 21 '22 10:09

Kondrashenkov Anton