简体   繁体   English

带有溢出的表格单元:隐藏且绝对定位的元素

[英]Table cells with overflow: hidden and absolutely positioned elements

I have this classic <td> which has the style attribute overflow: hidden set. 我有一个经典的<td> ,它的样式属性为overflow: hidden设置。 Inside the cell, I've put a <div> with position: relative , and inside the block there are absolutely positioned large images that I want to be clipped to the boundaries of the cell. 在单元格内部,我放置了一个带有position: relative<div> ,并且在块内,有绝对定位的大图像,我想将其剪切到单元格的边界。 Meaningful example: 有意义的例子:

...
<td style="overflow: hidden;">
    <div style="position: relative;">
        <img style="position: absolute; left: 0px; top: 50%;" src="verylargeimage.png" />
    </div>
</td>
...

It seems to work fine in IE8, IE9, Firefox and Opera, but it fails on Chrome: the image spans over the boundaries of the cell. 在IE8,IE9,Firefox和Opera中,它似乎可以正常工作,但在Chrome上却无法正常工作:图像跨越单元格的边界。

I have the same behaviour when I print the page in IE. 在IE中打印页面时,我有相同的行为。

I'm aware that the overflow attribute on table cells isn't well defined. 我知道表单元格上的overflow属性定义不正确。 Is there any way to obtain the desired effect without using background images? 有什么方法可以在使用背景图像的情况下获得所需的效果? The result must be printable. 结果必须是可打印的。

UPDATE I don't want to use Javascript either. 更新我也不想使用Javascript。 I want it to be my last resort. 我希望它是我的最后选择。

overflow : hidden has a meaning - which is hide everything which is going out of "defined" area. 溢出:隐藏具有含义-隐藏所有超出“定义”区域的内容。 which means you will need to define what will be your width / height or both will be and it will hide anything which des not fit there. 这意味着您将需要定义宽度/高度或两者都将是什么,并且它将隐藏不适合此处的任何内容。

Try using table-layout:fixed on the table element and defined width/height on the td . 尝试使用table-layout:fixed在table元素上,并在td上定义宽度/高度。 It stops the layout engine from trying to adjust the table cells based on their contents. 它阻止布局引擎尝试根据其内容调整表格单元格。 It's still not gaurenteed to work but it's your best shot. 它仍然无法正常工作,但这是您的最佳选择。 Frankly though I don't think it can really be done. 坦率地说,尽管我认为这确实无法做到。

I should point out that overflow on table cells is actually well-defined - the standard specifically doesn't support it. 我应该指出,表单元格上的overflow实际上是定义明确的-标准明确不支持它。 What you're doing is a hack. 您正在执行的操作是入侵。

You may want to make use of the CSS Clip property and it is not even CSS3. 您可能要使用CSS Clip属性,而CSS3都不是。 Looks like it fits in your kind of situation. 看起来很适合您的情况。

The clip property is like a mask. clip属性就像一个蒙版。 It allows you to mask the content of an element in a rectangle shape. 它允许您将元素的内容屏蔽为矩形。

Check this link below and read the CSS Clip part. 在下面查看此链接,并阅读CSS Clip部分。 http://webdesignerwall.com/tutorials/5-simple-but-useful-css-properties http://webdesignerwall.com/tutorials/5-simple-but-useful-css-properties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM