【发布时间】:2016-12-31 06:41:34
【问题描述】:
我正在使用 display:table 和 display:table-cell 来显示一个 div,就好像它是一个表格一样。我希望一个 div (.cellcontents) 的内容像在表格单元格中一样垂直居中,问题是我有另一个 div (.cellhead) 具有相同的父级,我希望垂直对齐到顶部.
所以我想要的是这样的
| |length|
| | |
|[img]| 120m |
| | |
完成此任务的最佳方法是什么?我是不是完全走错了路?当前html:
<div class="table">
<div class="film row">
<div class="poster cell">[IMG]</div>
<div class="detail cell last">
<div class="cellhead">length</div>
<div class="cellcontents">120 minutes</div>
</div>
</div>
</div>
css 来了
.table {
display: table;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
border: 1px solid lightgrey;
border-right: none;
}
.film .cell.poster {
text-align: center;
vertical-align: middle;
width: 140px;
height: 5em;
}
.film .cell.detail {
vertical-align: top;
text-align: center;
width: 200px;
}
.film .cell div.cellhead {
background-color: #e5e5e5;
}
.film .cell.last {
border-right: 1px solid lightgrey;
}
【问题讨论】:
-
为什么要将表格数据放在 div 中?
-
不是最好的解决方案,也许你可以使用
position: absolutejsfiddle.net/Lg0wyt9u/964 -
因为它使稍后通过媒体屏幕更容易更改显示宽度。
-
@pg。好的,这是有道理的
标签: html css css-tables