【问题标题】:Having both stretch and vertical align center with Bootstrap and flex?使用 Bootstrap 和 flex 同时拉伸和垂直对齐中心?
【发布时间】:2019-10-17 22:56:11
【问题描述】:

我有两个 div。一个具有顶部和底部填充,另一个需要继承该填充/高度。我知道 flex 有一个选项可以拉伸。但我还需要将第二个 div 中的项目垂直居中。但是,我无法让它工作。我正在使用 Bootstrap 4,这是我的 HTML 的粗略轮廓:

.row {
  border: 1px solid black;
}

.first {
  background: red;
  padding-top: 25px;
  padding-bottom: 25px;
}

.second {
  background: green;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div class="container">
  <div class="row no-gutters align-items-stretch">
    <div class="col-md-6 first align-items-center">
      <h1 class="mb-0">Text goes here</h1>
    </div>
    <div class="col-md-6 second align-items-center">
      <h5 class="mb-0">Text goes here</h5>
    </div>
  </div>
</div>

如您所见,我在row 上使用了align-items-stretch,并尝试使用align-items-center 垂直对齐项目,但它显然什么也没做。我错过了什么/做错了什么?有人可以看看吗?

谢谢!

【问题讨论】:

  • 如果我理解正确,您希望第二个(绿色)元素与第一个(红色)元素的文本高度、填充和外观完全相同?据我所知tell,Flexbox 的拉伸属性主要用于在元素水平放置时垂直拉伸元素。
  • 没错,我希望它的高度完全相同,并且它的内容垂直居中!有什么方法可以实现吗?

标签: css bootstrap-4 flexbox


【解决方案1】:

您的第二个 div 似乎没有使其工作的“flex”行为。或者,您可以添加:“align-items”来强制它。

.second {
    display: flex;
    align-items: center; // if not working by itself
}

【讨论】:

猜你喜欢
  • 2019-10-08
  • 1970-01-01
  • 2020-04-02
  • 1970-01-01
  • 2017-05-11
  • 2013-10-04
  • 1970-01-01
  • 1970-01-01
  • 2018-08-23
相关资源
最近更新 更多