Display element as preformatted text via CSS

HtmlCssWhitespacePre

Html Problem Overview


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>

Html Solutions


Solution 1 - Html

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>

Solution 2 - Html

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

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

Solution 3 - Html

Yes:

div.preformatted {
    white-space: pre;
    }

Reference:

Solution 4 - Html

inorder to get <pre> you may use;

div.preformatted{ white-space: pre ; display: block; }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDagg NabbitView Question on Stackoverflow
Solution 1 - HtmlBoltClockView Answer on Stackoverflow
Solution 2 - HtmlKondrashenkov AntonView Answer on Stackoverflow
Solution 3 - HtmlDavid ThomasView Answer on Stackoverflow
Solution 4 - HtmlGovinnage Rasika PereraView Answer on Stackoverflow