如何防止右浮动内容与主要内容重叠?
我有以下 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;
}
当我调整浏览器大小以截断文本时,它会在 的边缘截断,而不是在
之前,这使得
与
内容重叠。我尝试了各种
padding
和 margin
,但似乎没有任何效果。这可能吗?
注意:在此处添加仅包含 的
非常困难。如果这很容易,我就会这么做:)
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 margin
s, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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?尝试一下:
它会将图像放在
的最右侧,并且
上的右侧填充将防止图像重叠文本。
中的position:relative的原因是为了使其成为position:absolute
的坐标系。您还需要一个位置类型(相对、绝对或固定)才能使 z-index 发挥作用。
如果这不起作用,是否可以选择将图像作为背景图像放在
上,如下所示:
Give this a try:
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: