我有以下布局:
<div class="next">
<div class="main">
<div class="actions">
<label><input type="checkbox" checked="">Label1</label>
<label><input type="checkbox" checked="">Label2</label>
<button>Button number 1</button>
<button>Button number 2</button>
</div>
<div class="target"></div>
</div>
</div>
及对应的样式:
.next {
position: relative;
min-height: 320px;
width: 98vw;
background: #fee;
}
.main {
display: inline;
float: left;
width: 280px;
margin: 2px 0 6px 4px;
background-color: #fff;
}
.target {
background: #ffa;
clear: both;
border: 1px solid black;
border-radius: 5px;
width: 280px;
height: 280px;
margin: 0 auto;
}
.actions {
width: 280px;
margin: 0 auto;
background: #cfc
}
.actions button {
width: 67px;
margin-right: 4px;
margin-bottom: 4px;
padding: 2px;
border-radius: 5px;
}
.actions button:last-child {
margin-right: 0;
}
.actions button:hover {
background-color: #eee;
}
.actions button:focus {
outline: 0;
background-color: #cce;
}
label {
font: 10pt sans-serif;
}
如您所见,“actions”div 的剩余高度是从整个容器的高度减去“target”正方形高度后剩下的高度。现在,我希望“actions”div 中的控件垂直对齐。
它会对齐,如果按钮有短的单行名称,但它们很长并且换成 2 行。定义行高的常用技巧不起作用。
有什么解决办法吗?
最佳答案
类似于 this ?只需使用 display table (wrapper div) + table-cell (inner div)
.actions {
display: table;
...
}
label {
display: table-cell;
vertical-align: middle;
...
}
关于html - 在计算高度的元素内垂直居中标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52336474/