<分区>
有没有办法通过 CSS 模拟 pre
元素的显示?
例如,我可以使用 CSS 规则在这个 div 中保持空格完整吗?
<div class="preformatted">
Please procure the following items:
- Soup
- Jam
- Hyphens
- Cantaloupe
</div>
标签 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/