简体   繁体   English

相对定位元素内部的绝对定位元素被裁剪

[英]Absolute positioned element inside relative positioned element gets clipped

My question is simple. 我的问题很简单。 A relative positioned div has an absolute positioned div as child. 相对定位div具有绝对定位div作为子级。 This absolute positioned child element gets clipped by the relative positioned element. 该绝对定位的子元素被相对定位的元素剪裁。

I should not make any changes to the relative positioned element, because it is a third party grid control. 我不应该对相对定位的元素进行任何更改,因为它是第三方网格控件。 Changing it breaks the grid layout. 更改它会破坏网格布局。

I tried setting overflow:visible on both the relative positioned div element and absolute positioned div element but nothing works. 我尝试在相对定位的div元素和绝对定位的div元素上设置overflow:visible ,但是没有任何效果。

Can anyone please suggest an idea to overcome this clipping problem? 任何人都可以提出一个解决此裁剪问题的想法吗?

 <div style="width:300px;height:300px;border:2px solid black;"> <div class="parent" style="position:relative;left:200px;width:300px;height:300px;border:2px solid black;overflow:hidden;"> Relative Parent <div class="child" style="position:absolute;left:-100px;border:2px solid black;"> <table> <tbody> <tr> <td> Absolute positioned div element </td> </tr> </tbody> </table> </div> </div> </div> 

EDIT: I've edited the snippet to show a way to wrap the text in the td element in case this is what you were trying to accomplish. 编辑:我已经编辑了代码段,以显示一种将文本包装在td元素中的方法,以防万一这是您要完成的工作。


If you can't edit the HTML of the child div directly, you can change its attributes with JavaScript. 如果您不能直接编辑子div的HTML,则可以使用JavaScript更改其属性。 The style attribute's left property having a value of -100px is what's causing your pain. 值为-100pxstyle属性的left属性是造成您痛苦的原因。

If you negate the default style attribute entirely, your CSS will be allowed to take over the styling, and the div will be at your mercy. 如果您完全否定默认样式属性,则将允许您的CSS接管样式,而div将由您决定。

Try something like: 尝试类似:

 const childDiv = document.getElementsByClassName("child")[0]; childDiv.style = ""; 
 .parent { position: relative; border: 2px solid black; width: 300px; height: 300px; overflow: visible; } .child { position: absolute; border: 2px solid black; overflow: visible; top: 100px; } .child td{ width: 100px; white-space: normal; } 
 <div style="width:300px;height:300px;border:2px solid black;"> <div class="parent" style="position:relative;left:200px;width:300px;height:300px;border:2px solid black;overflow:hidden;"> Relative Parent <div class="child" style="position:absolute;left:-100px;border:2px solid black;"> <table> <tbody> <tr> <td> Absolute positioned div element </td> </tr> </tbody> </table> </div> </div> </div> 

(Alternatively, you could set the style imperatively in the JavaScript (like childDiv.style="position: absolute; top: 100px;" , but relying on CSS for this is a best practice unless you have a good reason to do otherwise.) (或者,您可以在JavaScript中强制设置样式(例如childDiv.style="position: absolute; top: 100px;" ,但是除非有充分的理由,否则依赖CSS是一种最佳做法。)

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

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