html - Flexbox Hero 不会居中

标签 html css flexbox

我不知道为什么 justify-self 对我的 .hero-content 不起作用。

我试图让我的 .hero-content 水平和垂直居中,它水平居中但垂直它仍然位于顶部。不确定该怎么做。

.hero {
 height: 100vh;
 width: 100vw;
 background-image: url("../img/main.jpg");
 background-size: cover;
 color: #ffffff;
 display: flex;
 flex-direction: column;
}

header {
 display: flex;
 justify-content: space-between;
 align-items: center;
 padding: 25px 50px;
 opacity: 0;
 animation: fadeIn 1s .5s forwards;
}

.hero-content {
 display: flex;
 flex-direction: column;
 align-items: center;
 justify-content: center;
}
<section class="hero">
 <header>
   <a href="#" class="logotype">Mountain Travel</a>
   <nav>
     <ul class="top-nav__menu">
      <li class="top-nav__menu-item"><a href="#">Tours</a></li>
      <li class="top-nav__menu-item"><a href="#">About</a></li>
      <li class="top-nav__menu-item"><a href="#">Contact Us</a></li>
    </ul>
   </nav>
 </header>
 <div class="hero-content">
  <h2>Mountain Travel</h2>
  <h3 class="small-margin-top">Unmissable Adventure Tours Around the World</h3>
  <button class="cta">Contact Us Now</button>
 </div>
</section>

最佳答案

你需要在你的.hero-content中设置flex-grow: 1enter image description here

演示(查看完整页面)

.hero {
 height: 100vh;
 width: 100vw;
 background-image: url("../img/main.jpg");
 background-size: cover;
 color: #ffffff;
 display: flex;
 flex-direction: column;
}

header {
 display: flex;
 justify-content: space-between;
 align-items: center;
 padding: 25px 50px;
 opacity: 1;
 animation: fadeIn 1s .5s forwards;
}

.hero-content {
 display: flex;
 flex-direction: column;
 align-items: center;
 justify-content: center;
 flex-grow: 1;
}

/************** 
** DEMO ONLY **
**************/

.hero-content h2 {
  margin-top: 0;
}

.hero-content {
  background-color: #eee;
}

.hero-content * {
  color: #333;
}
<section class="hero">
 <header>
   <a href="#" class="logotype">Mountain Travel</a>
   <nav>
     <ul class="top-nav__menu">
      <li class="top-nav__menu-item"><a href="#">Tours</a></li>
      <li class="top-nav__menu-item"><a href="#">About</a></li>
      <li class="top-nav__menu-item"><a href="#">Contact Us</a></li>
    </ul>
   </nav>
 </header>
 <div class="hero-content">
  <h2>Mountain Travel</h2>
  <h3 class="small-margin-top">Unmissable Adventure Tours Around the World</h3>
  <button class="cta">Contact Us Now</button>
 </div>
</section>

关于html - Flexbox Hero 不会居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55212669/

相关文章:

javascript - 不用jQuery淡入(fadeIn)——CSS过渡不透明度和显示

javascript - 调整溢出大小以适应 div 内部

css - 如何修复 bulma css 在 768 点中断后制作单列布局,而平板设备应该是 2 列布局?

css - 哪些标签允许在 Foundation 中具有列类

html - 屏幕上的 Cordova CSS 缩放图像

javascript - 使用 Flexbox 和 Enyo 调整 Canvas 大小

html - 高度不同时如何获得紧凑的div?

html - CSS 用脉冲旋转动画

javascript - 文本不会填满 Div

javascript - jQuery 下拉插件 - 如何知道下拉列表是否打开?