我已经通过使用 position:absolute和left 值将一些div元素定位在TD元素的左侧,但我需要将div垂直对齐在中间,而没有固定的边距和填充值,因为TD高度可以是任何值。我已添加示例以供参考,有帮助吗?
.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>
谢谢
答案 0 :(得分:0)
您可以将display: inline-block
用于div,并将vertical-align:middle
用于单元格。然后,无论表格行有多高,它都将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>
注意,与float
不同的是,在div之间使用inline-block
空白将会显示!因此,除非您将其编码为<div>...</div><div>...</div>
,否则您会看到一些多余的多余空间。