在可变高度的父级中垂直对齐div

时间:2015-12-25 07:10:45

标签: css vertical-alignment

我有一个包装器,其高度是可变的(取决于左侧框中的内容)。我希望这个包装器中的button在右侧框内垂直对齐。

我无法让它工作,尽管我已经尝试了很多东西,包括css-tricks' post on centering,我已将其置于评论之下。我也尝试修补flexbox,但没有用。

.wrapper {
    width: 20em;
    margin: 1em;
    border: 1px solid #999;
    overflow: hidden;
}
.left {
    float: left;
    width: 70%;
}
.right {
    position: relative;
    overflow: hidden; /* to induce block formatting context */
}
.button {
    width: 3em;
    border: 1px solid #999;
    margin: 0 auto;

    /*
    // The below didn't work! the `button` vanishes.
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    */
}
<div class="wrapper">
  <div class="left">Every person is worth just as much as the things they busy themselves with.</div>
  <div class="right">
    <div class="button">button</div>
  </div>
</div>

4 个答案:

答案 0 :(得分:3)

您可以使用display:inline-block;代替float left;,并且不要忘记display:inline-block;发生的remove extra spaces

.wrapper {
    width: 20em;
    margin: 1em;
    border: 1px solid #999;
    overflow: hidden;
}
.left,
.right
{
  display:inline-block;
  vertical-align:middle;
  }
.left {
    width: 70%;
}
.right {
    position: relative;
    overflow: hidden; /* to induce block formatting context */
  text-align:center;
  width:30%;
}
.button {
    width: 3em;
    border: 1px solid #999;
    margin: 0 auto;

    /*
    // The below didn't work! the `button` vanishes.
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    */
}
<div class="wrapper">
  <div class="left">Every person is worth just as much as the things they busy themselves with.</div><div class="right">
    <div class="button">button</div>
  </div>
</div>

答案 1 :(得分:3)

这里的主要问题是你需要左右div以自动具有相同的高度。除了javascript之外,只有flexbox和css表允许这样做。所以......

Flexbox 可以这样做。

.wrapper {
  width: 20em;
  margin: 1em;
  border: 1px solid #999;
  overflow: hidden;
  display: flex;
}
.left {
  flex: 0 0 70%;
}
.right {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}
.button {
  width: 3em;
  border: 1px solid #999;
  text-align: center;
}
<div class="wrapper">
  <div class="left">Every person is worth just as much as the things they busy themselves with.</div>
  <div class="right">
    <div class="button">button</div>
  </div>
</div>

或... CSS表格

.wrapper {
  width: 20em;
  margin: 1em;
  border: 1px solid #999;
  overflow: hidden;
  display: table;
  table-layout: fixed;
}

.left {
  display: table-cell;
  width: 70%;
}

.right {
  display: table-cell;
  width: 30%;
  vertical-align: middle;
  text-align: center;
}

.button {
  width: 3em;
  border: 1px solid #999;
  margin: auto;
}
<div class="wrapper">
  <div class="left">Every person is worth just as much as the things they busy themselves with.</div>
  <div class="right">
    <div class="button">button</div>
  </div>
</div>

答案 2 :(得分:1)

如果您愿意使用calc

,这是有效的
.button {
    width: 3em;
    border: 1px solid #999;
    margin: calc(50% - 0.5em) 0 calc(50% - 0.5em) 0;

虽然有一个问题:calc不会接受其他参数,例如按钮的高度;所以我认为它是1em(因为你没有定义高度)。

答案 3 :(得分:1)

查看这个样式锻炼,

.wrapper{
  width: 20em;
  margin: 1em;
  border: 1px solid #999;
}
.left{
  float: left;
  width: 15em;
}
.right{
  width: 5em;
  float: right;
}

.button {
  width: 3em;
  border: 1px solid #999;
  margin: 1em;
}