Html div内文本中的新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24963821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:20:47  来源:igfitidea点击:

New line in text inside div

htmlcss

提问by user3681138

In a textarea, there are new lines using for example 
, but how can I do this to a div element without using htmll (plain text like \n) ?

在 textarea 中,有使用例如的新行
,但是如何在不使用 htmll(纯文本如 \n)的情况下对 div 元素执行此操作?

回答by AstroCB

You can use CSS to force HTML to recognize new lines as line breaks.

您可以使用 CSS 强制 HTML 将新行识别为换行符。

Just set the white-spaceproperty to pre:

只需将white-space属性设置为pre

div {
    white-space: pre;
}

Demo

演示