防止内联块换行,但允许内容换行

发布于 2024-12-13 02:18:06 字数 447 浏览 5 评论 0原文

我有这样的布局:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橪书 2024-12-20 02:18:06
li {display:table;}

是你的朋友。另外,不要忘记删除内联样式!

li {display:table;}

is your friend. Also, don't forget to remove inline-styles!

已下线请稍等 2024-12-20 02:18:06

li 元素上尝试 white-space: normal

white-space 默认情况下是继承的,因此它们从 ul 接收 nowrap

我开始认为您正在使用 ul 进行布局,而 div 可能更适合:

<div class="Item">
 <div class="ImageContainer"><img src="" alt="/></div>
 <div class="TextContainer">Text text text text text</div>
</div>

.Item {
 width: 200px;
 overflow: auto;
}

.ImageContainer {
 float: left;
 width: 40%;
}

.TextContainer {
 float: left;
 width: 60%;
}

Try white-space: normal on the li elements.

white-space is inherited by default so they received nowrap from the ul.

I'm starting to think that you are using an ul for layout purposes which div might be better suited for:

<div class="Item">
 <div class="ImageContainer"><img src="" alt="/></div>
 <div class="TextContainer">Text text text text text</div>
</div>

.Item {
 width: 200px;
 overflow: auto;
}

.ImageContainer {
 float: left;
 width: 40%;
}

.TextContainer {
 float: left;
 width: 60%;
}
や三分注定 2024-12-20 02:18:06

听起来您可能实际上想使用表格。

否则,如果您知道图像的宽度,请将其向左浮动,并为下一个元素提供大于或等于图像宽度的左边距。

例如:

article > img {
  float: left;
  height: 80px;
  width: 80px;
}
article > div {
  margin-left: 90px;
}
<article>
  <img src="http://www.gravatar.com/avatar/7e6e0e2b73358e47e0b7f83f8111f75b">
  <div>
    <h4>Matt Di Pasquale</h3>
    <p>I know the width of the image is 80px, so I floated it left and gave the <code>div</code> a 90px left margin. That way, everything gets layed out perfectly, and this paragraph's text wraps.</p>
  </div>
</article>

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:

article > img {
  float: left;
  height: 80px;
  width: 80px;
}
article > div {
  margin-left: 90px;
}
<article>
  <img src="http://www.gravatar.com/avatar/7e6e0e2b73358e47e0b7f83f8111f75b">
  <div>
    <h4>Matt Di Pasquale</h3>
    <p>I know the width of the image is 80px, so I floated it left and gave the <code>div</code> a 90px left margin. That way, everything gets layed out perfectly, and this paragraph's text wraps.</p>
  </div>
</article>

↘紸啶 2024-12-20 02:18:06

这是 CSS 网格布局 的实际用例:

ul {
  display: grid;
  grid-template-columns: 200px 1fr;
  column-gap: 10px;
}
li {
  display: unset; /* reset user agent list-style */
}
img {
  background: #00bcd4; /* style image background */
}
<ul>
<li><img width="200" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20/%3E%0A">
<li>long text content next to the image long text content next to the image long text content next to the image long text content next to the image
</ul>

创建具有 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:

ul {
  display: grid;
  grid-template-columns: 200px 1fr;
  column-gap: 10px;
}
li {
  display: unset; /* reset user agent list-style */
}
img {
  background: #00bcd4; /* style image background */
}
<ul>
<li><img width="200" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20/%3E%0A">
<li>long text content next to the image long text content next to the image long text content next to the image long text content next to the image
</ul>

Creates two-column grid with 10px column gap. First grid item has 200px 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 second li using the :last-of-type pseudo-class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文