垂直对齐两个div彼此相邻到相同的高度?

时间:2018-05-17 11:43:51

标签: html css tablecell

我想将两个div(或类似的东西)彼此相邻对齐到相同的高度。

包含两个div的包装器也应该在中心垂直对齐。

尝试过这样的事情



<div class="wrapper">
    <div class="innerWrapper">
      <div class="box">This is a box</div>
      <div class="box">
        <div>This is a box</div>
        <div>This is a box</div>
      </div>
    </div>
</div>
&#13;
{{1}}
&#13;
&#13;
&#13;

我希望两个div的高度相同,与每个div内的内容无关。

6 个答案:

答案 0 :(得分:0)

只需在display: flex;

中使用.innerWrapper即可

&#13;
&#13;
.wrapper {
  background-color: #f1f1f1;
  height: 300px;
  width: 50%;
  margin: 0 auto;
  display: table;
}

.innerWrapper {
  display: flex;
  text-align: center;
  width: fit-content;
}

.box {

  padding: 25px 25px;
  opacity: 0.8;
  background-color: #32CD32;
}
&#13;
<div class="wrapper">
    <div class="innerWrapper">
      <div class="box">This is a box</div>
      <div class="box">
        <div>This is a box</div>
        <div>This is a box</div>
      </div>
    </div>
</div>
&#13;
&#13;
&#13;

或者如果你想要中间的盒子。

&#13;
&#13;
.wrapper {
  background-color: #f1f1f1;
  height: 300px;
  width: 50%;
  margin: 0 auto;
  display: flex;
  align-items:center;
  justify-content:center;
}

.innerWrapper {
  display: flex;
  text-align: center;
  width: fit-content;
}

.box {
  display: inline-block;
  padding: 25px 25px;
  opacity: 0.8;
  background-color: #32CD32;
}
&#13;
<div class="wrapper">
    <div class="innerWrapper">
      <div class="box">This is a box</div>
      <div class="box">
        <div>This is a box</div>
        <div>This is a box</div>
      </div>
    </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用 Flexbox 执行此操作:

.wrapper {
  background-color: #f1f1f1;
  height: 300px;
  width: 50%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.innerWrapper {
  display: flex;
  text-align: center;
}

.box {
  padding: 25px 25px;
  opacity: 0.8;
  background-color: #32CD32;
}
<div class="wrapper">
    <div class="innerWrapper">
      <div class="box">This is a box</div>
      <div class="box">
        <div>This is a box</div>
        <div>This is a box</div>
      </div>
    </div>
</div>

答案 2 :(得分:0)

flex添加到.innerWrapper,将margin-right添加到.box

&#13;
&#13;
.wrapper {
  background-color: #f1f1f1;
  height: 300px;
  width: 50%;
  margin: 0 auto;
  display: table;
}

.innerWrapper {
  display: flex;
  vertical-align: middle;
  text-align: center;
  width: fit-content;
}

.box {
 margin-right: 15px;
  padding: 25px 25px;
  opacity: 0.8;
  background-color: #32CD32;
}
&#13;
<div class="wrapper">
    <div class="innerWrapper">
      <div class="box">This is a box</div>
      <div class="box">
        <div>This is a box</div>
        <div>This is a box</div>
      </div>
    </div>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

我有一个使用.wrapper { background-color: #f1f1f1; height: 300px; width: 50%; margin: 0 auto; display: table; } .innerWrapper { vertical-align: middle; text-align: center; width: fit-content; display:table-row; } .box { padding: 25px 25px; opacity: 0.8; background-color: #32CD32; display:table-cell; }的解决方案。您也可以在容器内放置最大高度或宽度,现在它已经满了。

&#13;
&#13;
<div class="wrapper">
    <div class="innerWrapper">
      <div class="box">This is a box</div>
      <div class="box">
        <div>This is a box</div>
        <div>This is a box</div>
      </div>
    </div>
</div>
&#13;
{{1}}
&#13;
&#13;
&#13;

无论如何,你可以使用flex,flexbox,grid ....

答案 4 :(得分:0)

没有flexbox

您需要调整表类的使用,如下所示:

.wrapper {
  background-color: #f1f1f1;
  height: 300px;
  width: 50%;
  margin: 0 auto;
  display: table;
  border-spacing:10px;
}
/* Added this for centring*/
.wrapper:before,.wrapper:after {
  content:"";
  display: table-row;
}
/**/

.innerWrapper {
  display: table-row; /*modified*/
  text-align: center;
}

.box {
  display: table-cell; /*modified*/
  padding: 25px 25px;
  opacity: 0.8;
  background-color: #32CD32;
}
<div class="wrapper">
    <div class="innerWrapper">
      <div class="box">This is a box</div>
      <div class="box">
        <div>This is a box</div>
        <div>This is a box</div>
      </div>
    </div>
</div>

答案 5 :(得分:0)

是的,你也可以使用display: table实现这一点,但你必须稍微更改另一个css。这是一个例子:

<div class="wrapper">
    <div class="innerWrapper">
      <div class="box">This is a box</div>
      <div class="box">
        <div>This is a box</div>
        <div>This is a box</div>
      </div>
    </div>
</div>

&#13;
&#13;
.wrapper {
  background-color: #f1f1f1;
  height: 300px;
  width: 50%;
  margin: 0 auto;
  display: table;
  overflow: hidden;
  position: relative;
}

.innerWrapper {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.box {
  display: table-cell;
  padding: 25px 25px;
  opacity: 0.8;
  background-color: #32CD32;
}
&#13;
&#13;
&#13;

以下是codepen

相关问题