如何防止右浮动内容与主要内容重叠?

发布于 2024-09-12 01:58:50 字数 787 浏览 7 评论 0原文

我有以下 HTML:

<td class='a'>
  <img src='/images/some_icon.png' alt='Some Icon' />
  <span>Some content that's waaaaaaaaay too long to fit in the allotted space, but which can get cut off.</span>
</td>

它应该显示如下:

[Some content that's wa [ICON]]

我有以下 CSS:

td.a span {
  overflow: hidden;
  white-space: nowrap;
  z-index: 1;
}

td.a img {
  display: block;
  float: right;
  z-index: 2;
}

当我调整浏览器大小以截断文本时,它会在 的边缘截断,而不是在 之前,这使得 内容重叠。我尝试了各种 paddingmargin,但似乎没有任何效果。这可能吗?

注意:在此处添加仅包含 非常困难。如果这很容易,我就会这么做:)

I have the following HTML:

<td class='a'>
  <img src='/images/some_icon.png' alt='Some Icon' />
  <span>Some content that's waaaaaaaaay too long to fit in the allotted space, but which can get cut off.</span>
</td>

It should display as follows:

[Some content that's wa [ICON]]

I have the following CSS:

td.a span {
  overflow: hidden;
  white-space: nowrap;
  z-index: 1;
}

td.a img {
  display: block;
  float: right;
  z-index: 2;
}

When I resize the browser to cut off the text, it cuts off at the edge of the <td> rather than before the <img>, which leaves the <img> overlapping the <span> content. I've tried various padding and margins, but nothing seemed to work. Is this possible?

NB: It's very difficult to add a <td> that just contains the <img> here. If it were easy, I'd just do that :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

关于从前 2024-09-19 01:58:50

overflow:hidden 可能不起作用,因为您将其应用于内联元素。

一种解决方案可能是将该跨度放置在 div 内,使该 div overflow:hidden。或者将 span 更改为 div。您可能仍然需要为 div 提供图像宽度的右边距。

如果没有 white-space: nowrap 属性,事情会容易得多。有没有什么可能的方法可以重新设计您的应用程序,以便它不需要使用它?

It's possible that overflow: hidden isn't working because you are applying it to an inline element.

One solution might be to place that span inside a div, giving that div overflow: hidden. Or change the span to a div. You may still need to mess around with giving the div a right margin the width of the image.

It would be a lot easier without the white-space: nowrap property. Is there any possible way you could re-design your application so it doesn't need to use this?

清晨说晚安 2024-09-19 01:58:50

尝试一下:

td.a {
   position: relative;
}

td.a span {       
   display: block;
   overflow: hidden;
   white-space: nowrap; 

   position: relative;
   z-index: 1;

   padding-right: 20px; /* This should be the width of your image */
}

td.a img {
   position: absolute;
   z-index: 2;
   top: 0;
   right: 0;       
}

它会将图像放在 的最右侧,并且 上的右侧填充将防止图像重叠文本。

中的position:relative的原因是为了使其成为position:absolute的坐标系。您还需要一个位置类型(相对、绝对或固定)才能使 z-index 发挥作用。

如果这不起作用,是否可以选择将图像作为背景图像放在 上,如下所示:

td.a span {       
   display: block;
   overflow: hidden;
   white-space: nowrap; 

   padding-right: 20px; /* This should be the width of your image */
   background: url(your-img.png) no-repeat 100% 0; /* right-top aligned background */
}

Give this a try:

td.a {
   position: relative;
}

td.a span {       
   display: block;
   overflow: hidden;
   white-space: nowrap; 

   position: relative;
   z-index: 1;

   padding-right: 20px; /* This should be the width of your image */
}

td.a img {
   position: absolute;
   z-index: 2;
   top: 0;
   right: 0;       
}

It will put the image to the far right of your <td> and the right padding on the <span> will keep the image from overlapping the text.

The reason for the position: relative in the <td class="a"> is so that it becomes the coordinate system for the position: absolute <img>. You also need a position type (relative, absolute or fixed) for z-index to work.

If that doesn't work, would it be an option to put the image as a background image on the <span> like so:

td.a span {       
   display: block;
   overflow: hidden;
   white-space: nowrap; 

   padding-right: 20px; /* This should be the width of your image */
   background: url(your-img.png) no-repeat 100% 0; /* right-top aligned background */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文