CSS 在具有定义宽度的跨度中换行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12691387/
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 20:20:43  来源:igfitidea点击:

Line break in a span with defined width?

cssline-breaks

提问by user1638055

How can I break a line in a span once the width of the span is reached? I tried max-width, but this did not work, the span is always as long as the text.

一旦达到跨度的宽度,如何在跨度中断开一条线?我试过最大宽度,但这不起作用,跨度总是和文本一样长。

Ideas?

想法?

Thanks!

谢谢!

回答by chipcullen

By default, <span>elements are 'inline', and will always grow to the size of their content. You need to explicitly declare the <span>to be display: block;or display: inline-block;.

默认情况下,<span>元素是“内联”的,并且总是会增长到其内容的大小。您需要显式声明<span>display: block;or display: inline-block;

回答by Qaisar Nadeem

span { 
    display:block;
    width:150px;
    word-wrap:break-word;
}

回答by Madara's Ghost

spanis an inlineelement, meaning, it is wrapped around text (or other inline elements). It is not subject to widthor height.

span是一个内联元素,意思是它包裹在文本(或其他内联元素)周围。它不受width或 的约束height

You want a blockelement, like a div, or maybe an inline-blockelement (achieved by setting display: inline-blockon an element).

您需要一个元素,例如 a div,或者可能是一个内联块元素(通过display: inline-block在元素上设置来实现)。