html - 将绝对定位的 DIV 与 IE11 中 TD 元素内的左值垂直对齐
问题描述
我已经通过使用position: absolute 和 left值在 TD 元素内定位了一些 div 元素,但我需要在中间垂直对齐 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>
谢谢,
解决方案
您可以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不同的是,将显示 div 之间的inline-block
空格!因此,除非您对它们进行编码,否则您<div>...</div><div>...</div>
会看到一些不需要的额外空间。
推荐阅读
- javascript - 刽子手游戏相同的字母验证仅javascript
- r - R中矩阵的条件减法
- java - 字符串空白(空或空)检查:如果结构与 java 8 可选/过滤器
- javascript - 占据整个屏幕宽度的表格
- reactjs - 在 apollo Gql 和 REACT 中执行查询后如何等待钩子?
- java - com.esotericsoftware.kryo.KryoException: java.lang.UnsupportedOperationException: java.io.StringReader,这是一个可关闭的资源
- python - 为 pygame 运行 setup.py install ... 错误
- google-apps-script - 在两个谷歌表格之间共享数据
- layout - [Python][Kivy] 布局中的两个轴滚动视图
- flutter - Flutter 是否支持彩色字体?