[英]Vertically align absolute positioned DIV with left value inside TD element in IE11
I have positioned some div element inside the TD element with left value by using position: absolute and left value but i need to align the div vertically in middle without fixed margin and padding value, because my TD height can be anything . 我已经通过使用position:absolute和left值将一些div元素定位在TD元素的左侧,但是我需要将div垂直对齐在中间而没有固定的边距和填充值,因为我的TD高度可以是任何值。 I have added sample for reference, any help?
我已添加示例以供参考,有什么帮助吗?
.content { position: absolute; left: 20px; background: grey; width: 100px; height: 20px; } td { border: 1px solid grey; border-collapse: collapse; /* Works in chrome, Firefox, Edge but not in IE 11*/ display: flex; align-items: center; } .content2 { position: absolute; left: 140px; background: grey; width: 100px; height: 20px; }
<table> <tr> <td style='width:300px;height:30px;'> <div class='content'> </div> <div class='content2'> </div> </td> </tr> </table>
Thanks, 谢谢,
You could use display: inline-block
for the div's and vertical-align:middle
for the cell. 您可以将
display: inline-block
用于div,将vertical-align:middle
用于单元格。 It then aligns the div's vertical in the middle, no matter how high the table row is. 然后,无论表格行有多高,它都会将div的垂直方向居中对齐。
.content {
display: inline-block;
background: grey;
width: 100px;
height: 20px;
vertical-align: middle;
}
tr{
height: 90px;
}
td {
border: 1px solid grey;
border-collapse: collapse;
}
.div1{
margin-left: 20px;
}
.div2{
margin-left: 40px;
}
<table>
<tr>
<td style='width:300px;height:30px;'>
<div class='content div1'>
</div>
<div class='content div2'>
</div>
</td>
</tr>
</table>
Note that, unlike float
, with inline-block
whitespace between the div's will be displayed! 请注意 ,与
float
不同,将显示div之间带有inline-block
空格 ! So unless you code them as <div>...</div><div>...</div>
you can see some unwanted extra space. 因此,除非您将它们编码为
<div>...</div><div>...</div>
否则您会看到一些多余的多余空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.