html - 通过 CSS 将元素显示为预格式化文本

标签 html css whitespace pre

<分区>

有没有办法通过 CSS 模拟 pre 元素的显示?

例如,我可以使用 CSS 规则在这个 div 中保持空格完整吗?

<div class="preformatted">

  Please procure the following items:

    - Soup
    - Jam
    - Hyphens 
    - Cantaloupe
</div>

最佳答案

使用 white-space: pre 来保留 pre 元素中的空白预格式化。更进一步,还要添加 font-family: monospace,因为 pre 元素通常默认设置为等宽字体(例如用于代码块)。

.preformatted {
    font-family: monospace;
    white-space: pre;
}
<div class="preformatted">

  Please procure the following items:

    - Soup
    - Jam
    - Hyphens 
    - Cantaloupe
</div>

关于html - 通过 CSS 将元素显示为预格式化文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9753597/

相关文章:

css - HTML5 视频未加载

html - 如何为 CSS 类中的元素设置不同的标题规则

html - 将一个 div 放置到另一个 div 上

使用 SASS 进行 HTML 表单验证?

css - IE7 在 float 元素之间创建空文本注释

c++ - 是否内联这个函数?

javascript - jQuery 显示 div 加载 onclick

javascript - 如何为设备使用正确的 CSS

javascript - 使 div 中的文本可选

无法调用参数中有空格的系统