【问题标题】:Positioning headers with flexbox使用 flexbox 定位标题
【发布时间】:2018-03-30 06:43:33
【问题描述】:

在 .52 行上,我希望在 MIDDLE .address div 顶部的 <h2>Locations</h2> 像这样:

但是 <h2> 的 atm 只是像这样坐在左边: 而且我不知道如何将它放在中间 div 之上。

这是我的 git 存储库:https://github.com/itsolidude/Tea_Cozy

纯代码:

html {
  font-family: Helvetica;
  font-size: 22px;
  color: seashell;
  background-color: black;
  opacity: 0.9;
  text-align: center;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 69px;
  border-bottom: 1px solid seashell;
  position: fixed;
  width: 100%;
  z-index: 2;
  background-color: black;
  top: 0;
}

img {
 height: 50px;
 padding-left: 10px;
}

nav span {
  color: seashell;
  padding-right: 30px;
}

.mission-banner {
  background-color: black;
}

.mission-banner h4 {
  padding-bottom: 10px;
}

a {
  cursor: pointer;
  text-decoration-color: seashell;
}

#mission {
  background-image: url(../images/img-mission-background.jpg);
  position: relative;
  margin: 70px auto 0;
  width: 1200px;
  height: 700px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

#tea-of-month {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  width: 1000px;
  margin: 0 auto 70px;
}

#tea-of-month img {
  height: 200px;
  width: 300px;
  margin-bottom: 10px;
}

.item {
  display: flex;
  flex-direction: column;
  padding: 10px;
}
#locations {
  height: 500px;
  width: 1200px;
  margin: 0 auto;
  background-image: url(../images/img-locations-background.jpg);
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.address {
  background-color: black;
  width: 300px;
  height: 300px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Tea Cozy | Home</title>
    <link rel="stylesheet" href="./resources/css/style.css">
  </head>
  <body>
    <header>
      <img src="./resources/images/img-tea-cozy-logo.png" alt="our logo">
      <nav>
        <a href="#mission"><span>Mission</span></a>
        <a href="#tea-of-month"><span>Featured Tea</span></a>
        <a href="#locations"><span>Locations</span></a>
      </nav>
        </header>
  <!-- main-content,our mission -->
        <div id="mission">
          <div class="mission-banner">
            <h2>Our Mission</h2>
            <h4>Handpicked, Artisanally Curated, Free Range, Sustainable, Small Batch, Fair Trade, Organic Tea</h4>
          </div>
        </div>
<!-- tea of the month -->
      <h2>Tea of the Month</h2>  <!--ERROR HERE, ITS HIDING BEHIND THE .MISSION DIV -->
      <h4>What's Steeping at The Tea Cozy?</h4>
      <div id="tea-of-month">
        <div class="item">
          <img src="./resources/images/img-berryblitz.jpg" alt="A picture of Fall Berry Blitz Tea">
          <span>Fall Berry Blitz Tea</span>
        </div>
        <div class="item">
          <img src="./resources/images/img-spiced-rum.jpg" alt="A picture of Spiced Rum Tea">
          <span>Spiced Rum Tea</span>
        </div>
        <div class="item">
          <img src="./resources/images/img-donut.jpg" alt="A picture of Seasonal Donuts">
          <span>Seasonal Donuts</span>
        </div>
        <div class="item">
          <img src="./resources/images/img-myrtle-ave.jpg" alt="A picture of Myrtle Ave Tea">
          <span>Myrtle Ave Tea</span>
        </div>
        <div class="item">
          <img src="./resources/images/img-bedford-bizarre.jpg" alt="A picture of Bedford Bizarre Tea">
          <span>Bedford Bizarre Tea</span>
        </div>
      </div>

      <!-- locations section -->
      <div id="locations">
        <h2>Locations</h2>
        <div class="address">
          <h3>Downtown</h3>
          <p>384 West 4th St</p>
          <p>Suite 108</p>
          <p>Portland, Maine</p>
        </div>
        <div class="address">
          <h3>East Bayside</h3>
          <p>3433 Phisherman's Avenue</p>
          <p>(Northwest Corner)</p>
          <p>Portland, Maine</p>
        </div>
        <div class="address">
          <h3>Oakdale</h3>
          <p>515 Crescent Avenue</p>
          <p>Second Floor</p>
          <p>Portland, Maine</p>
        </div>
      </div>

  </body>
</html>

您能解释一下您这样做的原因和原因吗?

【问题讨论】:

  • 太宽泛 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。避免一次问多个不同的问题。有关澄清此问题的帮助,请参阅如何提问页面。另外:minimal reproducible example
  • @Rob 更新了它
  • 你的例子不是Minimal。请将标记和 CSS 限制在您所询问的问题上。由于您询问的是#locations,请删除其他所有内容。如果问题清晰易懂,人们可能会帮助您,因为它可能会帮助其他未来的访问者。如果它只对您有帮助,那么可能没有人会打扰。如果你不想帮助别人,我为什么要帮助你?

标签: html css flexbox positioning


【解决方案1】:

1) 你的 'header' 标签是一个 flex 并且有两个项目 'img' 和 'nav'。为此,您已将 justify-content 设置为 space-between。这就是为什么“img”不在中心。

这可以解决。的方式。 下面显示了一种快速方法,它将在 div 中居中。

<header>
  <div style="margin:auto;">
    <img src="./resources/images/img-tea-cozy-logo.png" alt="our logo">
  </div>
  <nav>
    <a href="#mission"><span>Mission</span></a>
    <a href="#tea-of-month"><span>Featured Tea</span></a>
    <a href="#locations"><span>Locations</span></a>
  </nav>
</header>

【讨论】:

    【解决方案2】:

    如果你想让一个 flexbox 子元素占据一整行,你需要给它flex-shrink:0; flex-basis: 100% 给它的父元素:flex-wrap: wrap

    #locations h2 {
      flex: 1 0 100%; /* shorthand for: flex-grow:1;
                                        flex-shrink: 0;
                                        flex-basis: 100%; */
      text-align: center;
    }
    
    #locations {
      min-height: 500px;
      max-width: 1200px;
      margin: 0 auto;
      display: flex;
      justify-content: space-around;
      align-items: center;
    
      flex-wrap: wrap; /* this makes it wrap */
      background-color: #f5f5f5; /* just for SO */      
    }
    <div id="locations">
      <h2>Locations</h2>
      <div class="address">
        <h3>Downtown</h3>
        <p>384 West 4th St</p>
        <p>Suite 108</p>
        <p>Portland, Maine</p>
      </div>
      <div class="address">
        <h3>East Bayside</h3>
        <p>3433 Phisherman's Avenue</p>
        <p>(Northwest Corner)</p>
        <p>Portland, Maine</p>
      </div>
      <div class="address">
        <h3>Oakdale</h3>
        <p>515 Crescent Avenue</p>
        <p>Second Floor</p>
        <p>Portland, Maine</p>
      </div>
    </div>

    &lt;h2&gt; 的内容居中是通过 text-align: center; 完成的

    请注意,我还更改了 min-height 中的 height(您需要在移动设备上使用它)和 max-width 中的 width(您只想限制它,而不是限制它)。

    【讨论】:

    • ty 帮助很大。有没有办法让我移动到 h2,使其距离 div 10px?
    • @itsolidude 您可以在h2 上使用marginpadding。边距可以是负数,而填充不能。如果您的样式似乎不适用,请检查元素并查看适用的内容(并使您的选择器稍微强一些)。一个好的技巧是使用与其他规则相同的选择器并最后加载样式表。这样您就不会增加 CSS 的特异性,从长远来看这很有帮助。
    • 你真的很有帮助,谢谢! .. 如果我想减少这 3 个 div 之间的水平间距(比如说 40 px 的空间),我会给每个 div 自己的 id 并设置左右填充(每个 20px),这似乎相当不切实际。有没有更简单的方法?
    • 另外,当我使用 flex-shrink:0; flex-basis: 100% 并给了父级: flex-wrap: wrap

      将 div 向下推而不使它们居中?有没有办法让 div 不受影响?

    • 只有当 justify-contents 在父节点上设置为 centerspace-betweenspace-around 并且它们的 widths 相等时,它们才会居中。这是默认的 flexbox 行为。请注意,您不应该在同一个问题中提出后续问题。在 cmets 中回答它们只会对您有所帮助,而 SO 原则是回答问题也应该对未来的访问者有所帮助。请单独提问,并确保您research before提问。
    猜你喜欢
    • 2017-07-25
    • 1970-01-01
    • 2020-02-24
    • 2023-03-30
    • 1970-01-01
    • 2017-01-02
    • 2020-06-28
    • 2017-12-17
    • 2022-11-17
    相关资源
    最近更新 更多