防止内联块换行,但允许内容换行
我有这样的布局:
<ul style="white-space:nowrap;">
<li style="width:200px; display:inline-block;"></li>
<li style="display:inline-block; vertical-align:top; padding-left:10px;"></li>
</ul>
我已经设法阻止 ul
换行,这是一个开始。然而,第二个 li
中的内容仍会在屏幕外继续显示。重叠其父元素等。
与第一个 li
不同,我需要第二个 li
来弥补松弛并在宽度上保持动态。我需要将文本包装在第二个 li
内。
I have this layout:
<ul style="white-space:nowrap;">
<li style="width:200px; display:inline-block;"></li>
<li style="display:inline-block; vertical-align:top; padding-left:10px;"></li>
</ul>
I have managed to stop the ul
from wrapping which is a start. However, the content in the 2nd li
continues off screen. Overlapping its parent elements etc.
I need the 2nd li
to take up the slack and be dynamic in width unlike the first li
. And I need the text to wrap inside the 2nd li
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是你的朋友。另外,不要忘记删除内联样式!
is your friend. Also, don't forget to remove inline-styles!
在
li
元素上尝试white-space: normal
。white-space
默认情况下是继承的,因此它们从ul
接收nowrap
。我开始认为您正在使用
ul
进行布局,而div
可能更适合:Try
white-space: normal
on theli
elements.white-space
is inherited by default so they receivednowrap
from theul
.I'm starting to think that you are using an
ul
for layout purposes whichdiv
might be better suited for:听起来您可能实际上想使用表格。
否则,如果您知道图像的宽度,请将其向左浮动,并为下一个元素提供大于或等于图像宽度的左边距。
例如:
Sounds like you might actually want to use a table.
Otherwise, if you know the width of the image, float it left and give the next element a left margin greater than or equal to the width of the image.
For example:
这是 CSS 网格布局 的实际用例:
创建具有
10px
列间隙的两列网格。第一个网格项的宽度为200px
,以匹配您的图像和第二个换行文本。如果您尝试换行的内容可能包含长字符串,例如绝对 URL 或科学/医学术语,例如 pneumonoultramicroscopesilicovolcanoconiosis,则添加
overflow-wrap
使用 到第二个li
href="https://devdocs.io/css/:last-of-type" rel="nofollow noreferrer">:last-of-type
伪类。This is a practical use case for CSS Grid Layout:
Creates two-column grid with
10px
column gap. First grid item has200px
width to match your image and the second wrapping text.If if content you're trying to wrap may contain long strings such as an absolute URL or scientific/medical terms like pneumonoultramicroscopicsilicovolcanoconiosis add
overflow-wrap
to the secondli
using the:last-of-type
pseudo-class.