html - 在计算高度的元素内垂直居中标签

标签 html css

我有以下布局:

<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 行。定义行高的常用技巧不起作用。
有什么解决办法吗?

http://jsfiddle.net/KgqJS/570/

最佳答案

类似于 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/

相关文章:

css - 为什么转换顺序很重要?旋转/缩放不会给出与缩放/旋转相同的结果

html - 混淆了 Html 图片的高度和宽度

css - 试图让 Magento 调整产品图片的大小。

javascript - 同时编辑多个 HTML 文本输入

php - 50% 宽度的 block 布局间距不正确,不知道为什么

html - 如何摆脱动态断点期间的超链接中断?

javascript - 在 html5 移动设备中检测晃动

javascript - 如何使用 jQuery 使某些单选按钮强制使用文本框?

javascript - 根据浏览器宽度只显示有限的 div

css - 仅将 dropShadow 添加到网格 Pane JavaFx 2.2 的边框