【问题标题】:Flexbox container centering challengeFlexbox 容器居中挑战
【发布时间】:2021-05-18 03:25:42
【问题描述】:

早上好,

我在将 flexbox 容器本身居中时遇到了挑战。我设法将 flexbox 中的所有 5 个元素居中,但实际上无法让容器本身水平居中到页面中间。

我在被卡住之后看了一个教程,可以看到我的代码是相同的,除了容器的命名。为您可以看到他们的代码的确切秒提供的时间戳:https://youtu.be/fJc18fT4T3s?t=542。在他们的教程中,作为我的“图像容器”的“趋势容器”居中,而我的不是。我试过使用 margin: 0 auto;但这什么也没做,容器停留在页面的左侧。

我不得不添加 text-align: center 因为文本没有对齐。

如果有人能建议我做错了什么,不允许这个 flexbox 容器居中,那就太好了,因为我有点难过!

body {
  font-family: "helvetica", sans-serif;
  font-size: 22px;
  color: seashell;
  opacity: 0.9;
  background-color: black;
}

.mission h2,
#featured h2 {
  margin: 10px 0px 5px 0px;
  text-align: center;
}

.mission h4,
#featured h4 {
  margin: 15px 0px 10px 0px;
  text-align: center;
}


/* ---------------Navigation ----------------*/

.navbar {
  height: 69px;
  width: 100%;
  border-bottom: 1px seashell solid;
  background-color: black;
  position: fixed;
  z-index: 10;
  align-items: center;
  left: 0;
  top: 0;
}

.logo {
  height: 50px;
  margin-left: 0.45rem;
  position: relative;
  top: 5px;
}

.navlist {
  position: relative;
  float: right;
  margin-right: 0.45rem;
}

.navlist a {
  color: seashell;
}

.navbar .navlist ul {
  list-style: none;
  position: relative;
}

.navbar .navlist ul li {
  display: inline-block;
  text-decoration: underline;
  padding: 0px 10px;
}


/* ------------------Banner Section --------------*/

#mission {
  width: 1200px;
  height: 700px;
  background-image: url("https://content.codecademy.com/courses/freelance-1/unit-4/img-mission-background.jpg");
  margin: 70px auto;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

.mission {
  width: 100%;
  background-color: black;
  margin: 0 auto;
  text-align: center;
}


/*---------------Tea of the Month ------------------*/

.image-container {
  display: inline-flex;
  max-width: 1000px;
  height: auto;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.image img {
  height: 200px;
  width: 300px;
  padding: 10px 10px;
}
<html>

<head>
  <title>Tea Cozy</title>
  <link href="C:\Users\Jason\Desktop\Coding\Projects\teacosy\global.css" type="text/css" rel="stylesheet">
</head>

<body>


  <!-----------------Nav Bar--------------------->


  <nav class="navbar">
    <img class="logo" src="https://content.codecademy.com/courses/freelance-1/unit-4/img-tea-cozy-logo.png" alt="Tea Cozy logo">
    <div class="navlist">
      <ul>
        <li><a href="#mission">Mission</a></li>
        <li><a href="#featured">Featured Tea</a></li>
        <li><a href="">Locations</a></li>
      </ul>
    </div>
  </nav>


  <!-------------------Banner--------------------->


  <div id="mission">
    <div class="mission">
      <h2>Our Misson</h2>
      <h4>Handpicked, Artisanally Curated, Free Range, Sustainable, Small Batch, Fair Trade, Organic Tea</h4>
    </div>
  </div>


  <!-------------------Tea of the Month-------------------->


  <div id="featured">
    <h2>Tea of the Month</h2>
    <h4>What's Steeping at The Tea Cozy?</h4>
  </div>
  <div class="image-container">
    <div class="image">
      <img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-berryblitz.jpg">
      <h4>Fall Berry Blitz Tea</h4>
    </div>
    <div class="image">
      <img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-spiced-rum.jpg">
      <h4>Spiced Rum Tea</h4>
    </div>
    <div class="image">
      <img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-donut.jpg">
      <h4>Seasonal Donuts</h4>
    </div>
    <div class="image">
      <img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-myrtle-ave.jpg">
      <h4>Myrtle Ave Tea</h4>
    </div>
    <div class="image">
      <img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-bedford-bizarre.jpg">
      <h4>Bedford Bizarre Tea</h4>
    </div>
  </div>

</body>

</html>

【问题讨论】:

    标签: css flexbox center


    【解决方案1】:

    display: inline-flex 更改为display: flex 并将margin: 0 auto 添加到&lt;div class="image-container"&gt; 容器可修复页面该部分的中心对齐问题。

    body {
      font-family: "helvetica", sans-serif;
      font-size: 22px;
      color: seashell;
      opacity: 0.9;
      background-color: black;
    }
    
    .mission h2,
    #featured h2 {
      margin: 10px 0px 5px 0px;
      text-align: center;
    }
    
    .mission h4,
    #featured h4 {
      margin: 15px 0px 10px 0px;
      text-align: center;
    }
    
    
    /* ---------------Navigation ----------------*/
    
    .navbar {
      height: 69px;
      width: 100%;
      border-bottom: 1px seashell solid;
      background-color: black;
      position: fixed;
      z-index: 10;
      align-items: center;
      left: 0;
      top: 0;
    }
    
    .logo {
      height: 50px;
      margin-left: 0.45rem;
      position: relative;
      top: 5px;
    }
    
    .navlist {
      position: relative;
      float: right;
      margin-right: 0.45rem;
    }
    
    .navlist a {
      color: seashell;
    }
    
    .navbar .navlist ul {
      list-style: none;
      position: relative;
    }
    
    .navbar .navlist ul li {
      display: inline-block;
      text-decoration: underline;
      padding: 0px 10px;
    }
    
    
    /* ------------------Banner Section --------------*/
    
    #mission {
      width: 1200px;
      height: 700px;
      background-image: url("https://content.codecademy.com/courses/freelance-1/unit-4/img-mission-background.jpg");
      margin: 70px auto;
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    .mission {
      width: 100%;
      background-color: black;
      margin: 0 auto;
      text-align: center;
    }
    
    
    /*---------------Tea of the Month ------------------*/
    
    .image-container {
      display: flex;
      max-width: 1000px;
      height: auto;
      flex-wrap: wrap;
      align-items: center;
      justify-content: center;
      text-align: center;
      margin: 0 auto;
    }
    
    .image img {
      height: 200px;
      width: 300px;
      padding: 10px 10px;
    }
    <html>
    
    <head>
      <title>Tea Cozy</title>
      <link href="C:\Users\Jason\Desktop\Coding\Projects\teacosy\global.css" type="text/css" rel="stylesheet">
    </head>
    
    <body>
    
    
      <!-----------------Nav Bar--------------------->
    
    
      <nav class="navbar">
        <img class="logo" src="https://content.codecademy.com/courses/freelance-1/unit-4/img-tea-cozy-logo.png" alt="Tea Cozy logo">
        <div class="navlist">
          <ul>
            <li><a href="#mission">Mission</a></li>
            <li><a href="#featured">Featured Tea</a></li>
            <li><a href="">Locations</a></li>
          </ul>
        </div>
      </nav>
    
    
      <!-------------------Banner--------------------->
    
    
      <div id="mission">
        <div class="mission">
          <h2>Our Misson</h2>
          <h4>Handpicked, Artisanally Curated, Free Range, Sustainable, Small Batch, Fair Trade, Organic Tea</h4>
        </div>
      </div>
    
    
      <!-------------------Tea of the Month-------------------->
    
    
      <div id="featured">
        <h2>Tea of the Month</h2>
        <h4>What's Steeping at The Tea Cozy?</h4>
      </div>
      <div class="image-container">
        <div class="image">
          <img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-berryblitz.jpg">
          <h4>Fall Berry Blitz Tea</h4>
        </div>
        <div class="image">
          <img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-spiced-rum.jpg">
          <h4>Spiced Rum Tea</h4>
        </div>
        <div class="image">
          <img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-donut.jpg">
          <h4>Seasonal Donuts</h4>
        </div>
        <div class="image">
          <img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-myrtle-ave.jpg">
          <h4>Myrtle Ave Tea</h4>
        </div>
        <div class="image">
          <img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-bedford-bizarre.jpg">
          <h4>Bedford Bizarre Tea</h4>
        </div>
      </div>
    
    </body>
    
    </html>

    【讨论】:

    • @Jozzie 如果我的回答解决了您的问题,请随时将我的回答标记为已接受。
    【解决方案2】:

    对于“image-container”,“body”的父级不是 flex 或 grid 项目。快速修复设置:body 是一个 flex 容器和“flex-direction 到列”,然后为“image-container”设置“align-self:center;”,如下所示:

    body {
        font-family: "helvetica", sans-serif;
        font-size: 22px;
        color: seashell;
        opacity: 0.9;
        background-color: black;
        display: flex;
        flex-direction: column;
        }
    .image-container {
        display: inline-flex;
        max-width: 1000px;
        height: auto;
        flex-wrap: wrap;
        align-items: center;
        justify-content: center;
        text-align: center;
        align-self: center;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-11
      • 2021-03-10
      • 2011-04-30
      • 2016-12-18
      • 2014-12-21
      • 2018-01-21
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      相关资源
      最近更新 更多