css - 使用 Flexbox 垂直居中文本并保持容器高度相同

标签 css flexbox

我需要使用flexbox进行以下设计。由于编程要求,我无法使用 tabledispay:table

enter image description here

我无法将文本垂直居中并在所有容器中保持相同的高度:

https://jsfiddle.net/kurtko/2fq6kn6m/

我正在尝试使用以下元素:

align-self:center;

或者到容器:

align-items:center;

但是我在所有容器中失去了相同的高度。

有什么想法吗?

最佳答案

您可以在 .one 上添加 display: flex align-items: center justify-content: center .small .medium

.item {
  display: flex;
  text-align: center;
}
.one,
.small,
.medium {
  display: flex;
  align-items: center;
  justify-content: center;
}
.one {
  align-items: flex-start;
}
.one {
  width: 39%;
  margin-right: 1%;
  background-color: black;
  color: white;
}
.two {
  width: 29%;
  margin-right: 1%;
}
.three {
  width: 40%;
}
.medium {
  background-color: grey;
  color: white;
  margin-bottom: 3px;
  height: 200px;
}
.small {
  background-color: #c0b55a;
  color: white;
  margin-bottom: 3px;
  height: 100px;
}
<div class="item">

  <div class="one">
    Top
  </div>

  <div class="two">
    <div class="medium">Center</div>
    <div class="medium">Center</div>
  </div>

  <div class="three">
    <div class="small">Center</div>
    <div class="small">Center</div>
    <div class="small">Center</div>
    <div class="small">Center</div>
  </div>

</div>

关于css - 使用 Flexbox 垂直居中文本并保持容器高度相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36597705/

相关文章:

jquery - 实现 jQuery 照片幻灯片时遇到问题

html - 图片下方带有文本的 Flexbox/Bootstrap 元素

html - IE11 - 如果上面内容的高度大于窗口高度,页面底部的 "footer"会被下推

html - 为什么我的 div 没有占据其父容器的 100% 高度?

html <hr> 标签奇怪显示

css - Web 组件 : Adjacent sibling selectors in children

html - 导航栏文本延伸到空间之外

css - 水印video.js播放器的最聪明方法

html - CSS - 保持图像纵横比 flexbox

html - 等宽 Flexbox 行,无论列数或列内容如何