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

标签 html css tablecell

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

此外,包含两个 div 的包装应垂直居中对齐。

试过这样的东西

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

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

.box {
  display: inline-block;
  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>

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

最佳答案

只需在 .innerWrapper 中使用 display: flex;

.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;
}
<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>

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

.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;
}
<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>

关于html - 垂直对齐彼此相邻的两个 div 到相同的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50390623/

相关文章:

javascript - 如何使用 JQuery 找到最短的 div

html - 在父 div 中居中 div 标签

html - 如何删除表格单元格中的空白空间?

ios:自定义表格单元格中的按钮在滚动时不响应

html - 中间有双倍尺寸边框的 CSS 表格元素

javascript - 导航菜单中的重置开关

html - 使用 jQuery toggle() 后如何响应式地隐藏菜单?

javascript - ng-click 不会在文本框内的 span 中触发

jquery - 如何获得正确/预期的不透明度以应用于在 IE8 中使用 $().fadeIn() 显示的元素?

css - 如何将Highchart的字体系列更改为Bootstrap css默认字体系列