html - 使用 flex 时需要堆叠元素

标签 html css flexbox

我有一个子 div 居中的 vert 和 horiz 在其父内部,内容居中以及使用 flex。但是,content 和 h2 并排居中,我希望它们堆叠。

.jumbotron {
  position: relative;
  min-height: 600px;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0;
  padding: 0;
}
#s-text {
  text-align: center;
  height: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 70%;
  margin: 0 auto;
}
#s-text h2 {
  color: #fff;
  margin-bottom: 20px;
  padding: 0;
}
<div class="jumbotron" style="...">
  <div id="s-text">
    <h2> the_secondary_title </h2>	
    <p>multiple lines of content ...</p>
  </div>
</div>

最佳答案

使用flex-direction: column;

.jumbotron { 
  position: relative;
  min-height: 400px; 
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0; padding: 0;
}

#s-text { 
  text-align: center;
  height: 100px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 70%;
  margin: 0 auto;
}

#s-text h2 {
  color: #000;
  margin-bottom: 20px; padding: 0;
}
<div class="jumbotron" style="...">
    <div id="s-text">
        <h2> Heading </h2>              
        <p>multiple lines of content ...</p>    
    </div>
</div> 

关于html - 使用 flex 时需要堆叠元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38067969/

相关文章:

javascript - 如何修改 JavaScript 函数以操作多个按钮?

javascript - 我在一个函数中有一个嵌套函数,但我的代码无法正确清除显示。我在控制台中没有收到错误消息?

javascript - jquery - 如何通过 html 正文中的脚本区分用户操作和操作?

html - 为什么此按钮上不显示文本阴影?

html - flex 基础 : auto doesn't work in IE11

html - 带有图像的 Flexbox 网格 - 保持纵横比/比例

javascript - 向服务器发送最小数据的最佳协议(protocol)是什么

html - 通过 HTML 和 CSS 使表格自动垂直滚动

html - 应用 CSS 除了 Internet Explorer

javascript - 禁用 flex 换行间距