段落文本超出 CSS HTML 的解决方法

3

我有一个段落中的文本

<p style="width:200px;">Text goes here</p>

我的问题是当文本变得更长(宽度更大)时,它不会换行。相反,文本会溢出段落。如果文本增长,我该如何强制其在新行上继续。


这在IE,Firefox,Chrome中完美运行...可能有其他东西干扰了这个简单的代码。 - krtek
我正在使用Chrome进行测试。 - user594166
是的,就像我之前说的并且有些答案也提到了,肯定有某些东西干扰了你的段落元素。但是如果直接复制粘贴这里的代码,它可以正常工作。证明链接:http://jsfiddle.net/CPy5E/ - krtek
8个回答

6

添加

overflow-wrap: break-word;

到CSS


1
请将以下与编程有关的内容从英语翻译成中文。只返回翻译后的文本,不要将其解释。 - TheKitMurkit

5

如果您希望文本在设置的宽度样式元素内逐行换行,则正确的做法是使用他/她提到的word-wrap CSS属性。 - a.stgeorge
在这种情况下,我需要文本在溢出时换行,是否可能?@Infotekka http://jsfiddle.net/Vivekranth/Ah6fb/28/ - Vivek Vikranth

4

请移除p标签上的宽度,先生。

<p>Text goes here.</p>

如果您无法移除宽度,请尝试添加white-space属性。
<p style="width:200px;white-space:pre;>Lot's of text here that will be wider than 200px</p>

2

默认情况下,溢出是自动的,这应该会使它换行。也许在其他地方您设置了 overflow: hidden; 这就是为什么它会这样做的原因。尝试再次设置 overflow:auto


1
你需要使用CSS3:word-wrap: break-word; 因为如果段落中有长单词,它们将会超出元素范围。例如:尝试。
<p style="width:100px;"> loremipsumloremipsumloremipsumloremipsumloremipsumloremipsumloremipsumloremipsumloremipsumloremipsum </p> 
<p style="width:100px; word-wrap:break-word;"> loremipsumloremipsumloremipsumloremipsumloremipsumloremipsumloremipsumloremipsumloremipsumloremipsum </p> 

Overflow会使文本不可见。


0

在样式中添加

white-space:wrap;

就可以了。


这是一个无效的参数传递给 white-space。 - mcdonaldjosh

0
使用 CSS word-break 属性 是其中一种方法:

<p style="word-break:break-all; width:200px;">
This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.
</p>


-1

由于 HTML 中几乎所有的元素都是盒子,因此您可以将“p 标签”固定在其 div 中,如下所示

<p style="position:relative; width:95%; height:whatever%;" > content </p>

你也可以通过使用“CSS左侧或右侧”属性来定位盒子,从而实现文本缩进。

<p style="position:relative; width:95%; height:whatever%; left:20px;" > content </p>

注意:在定位任何盒子之前,不要忘记使用“CSS position属性”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接