Display element as preformatted text via CSS [duplicate]

人盡茶涼 提交于 2019-11-26 19:21:18

问题


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>

回答1:


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>



回答2:


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

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




回答3:


Yes:

div.preformatted {
    white-space: pre;
    }

Reference:

  • white-space.



回答4:


inorder to get <pre> you may use;

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


来源:https://stackoverflow.com/questions/9753597/display-element-as-preformatted-text-via-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!