简体   繁体   English

在绝对定位的桌子中不考虑高度

[英]Height not being respected in absolutely positioned table

If an absolutely positioned element with display: table has height explicitly set, if content height is larger than provided height value, then the element will shrink wrap the content and not consider the height value. 如果绝对定位的带有display: table元素具有明确设置的高度,则如果内容高度大于提供的高度值,则该元素将收缩包装内容,而不考虑该高度值。

 #main { width: 100px; height: 29px; position: absolute; top: 20px; left: 200px; border: 1px red dashed; display: table; table-layout: fixed; border-spacing: 0; border-collapse: separate; } #child { display: table-cell; } 
 <div id="main"> <div id="child">Custom Text With Validation:</div> </div> 

It is the table cell that doesn't honor its parent's height for the same reason i doesn't honor max-height , hence pushing the table's size. 正是由于我不遵守max-height的相同原因,该表单元格不遵守其父级的max-height ,因此推动了表的大小。

Here is 2 ways to make this to work, either set position: absolute on the child 设置position: absolute方法有两种,一种可以设置position: absolutechild

 #main { width: 100px; height: 29px; position: absolute; top: 20px; left: 200px; border: 1px red dashed; display: table; table-layout: fixed; border-spacing: 0; border-collapse: separate; } #child { position: absolute; display: table-cell; } 
 <div id="main"> <div id="child">Custom Text With Validation: </div> </div> 

or add an extra element inside the child and set the height on that. 或在child内部添加一个额外的元素,然后在其上设置height

 #main { width: 100px; height: 29px; position: absolute; top: 20px; left: 200px; border: 1px red dashed; display: table; table-layout: fixed; border-spacing: 0; border-collapse: separate; } #child { display: table-cell; } #child div { height: 29px; } 
 <div id="main"> <div id="child"> <div>Custom Text With Validation:</div> </div> </div> 


Adding overflow: hidden on the table will also properly "cut" of the excess. 增加overflow: hidden在表上也会适当地“削减”多余的部分。

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

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