【问题标题】:Why is the header section so far up than it should be when I justify-content?为什么当我证明内容时,标题部分比它应该的要高得多?
【发布时间】:2022-02-04 05:55:13
【问题描述】:

为什么标题部分比我证明内容时应有的高度高?

当我将 header__container 的 justify 内容设置为空格时,标题开始超出其边距。请帮忙。

header {
  height: calc(100vh - 80px);
}

.header__container {
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}
<header>
  <div class="header__container">
    <div class="header__description">
      <h1>Lebanon's most awarded online library platform</h1>
      <h2>Find your dream book with <span class="purple">Library</span></h2>
    </div>
    <figure class="header__img--wrapper">
      <img src="https://via.placeholder.com/200" alt="">
    </figure>
  </div>
</header>

【问题讨论】:

  • 告诉我你正在等待的结果

标签: html css flexbox


【解决方案1】:

您已将容器高度设置为 100%,但有时该高度不足以容纳您的内容。因为您已将其居中,所以顶部的某些部分被切掉了,底部也一样。

改为设置min-height

header {
  height: calc(100vh - 80px);
}

.header__container {
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}
<header>
  <div class="header__container">
    <div class="header__description">
      <h1>Lebanon's most awarded online library platform</h1>
      <h2>Find your dream book with <span class="purple">Library</span></h2>
    </div>
    <figure class="header__img--wrapper">
      <img src="https://via.placeholder.com/200" alt="">
    </figure>
  </div>
</header>

【讨论】:

  • 它不起作用但是当我将标题高度更改为 200vh 而不是 100vh 时它起作用了。这能给我们任何解释吗? @isherwood
  • @oissa 为什么你觉得有必要在标题上设置高度 - 因为 200vh 会强制滚动,这对于标题来说感觉很奇怪。
  • @MarkSchultheiss 我只想让它占据一页,我知道 200vh 不是有效的方法,但我想知道为什么 200vh 是给我想要的答案的高度。
【解决方案2】:

现在我可以看到,当您使用 flex-direction: column; 时,您的 justify-content 会进行垂直对齐。

在这种情况下,您不需要使用 flex-direction: column,因为这是 display: block 元素的常见行为

【讨论】:

  • 是的,我知道。这就是我想要做的。我希望 h1 和 h2 以及 img 占据整个部分空间。
  • 好的,等一下
  • 好吧,我还是不明白,现在你的标题占据了所有页面大小
【解决方案3】:

尝试删除justify-content: space-around;,它在小屏幕上执行您所说的操作,因为您在header 上强制设置高度(为什么?)但其中包含“更大”的项目(您的图像)。请记住,“过多的 CSS”通常是它的祸根——少即是多。

注意,我设置了一个边框,以便我们可以直观地看到这些块。

header {
  height: calc(100vh - 80px);
}

.header__container {
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  flex-direction: column;
  /*justify-content: space-around;*/
}

.header__container>* {
  border: solid lime 1px;
}
<header>
  <div class="header__container">
    <div class="header__description">
      <h1>Lebanon's most awarded online library platform</h1>
      <h2>Find your dream book with <span class="purple">Library</span></h2>
    </div>
    <figure class="header__img--wrapper">
      <img src="https://via.placeholder.com/200" alt="">
    </figure>
  </div>
</header>

替代选项:停止做高度,让“流动”流动。

header {
  /*height: calc(100vh - 80px);*/
}

.header__container {
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}

.header__container>* {
  border: solid lime 1px;
}
<header>
  <div class="header__container">
    <div class="header__description">
      <h1>Lebanon's most awarded online library platform</h1>
      <h2>Find your dream book with <span class="purple">Library</span></h2>
    </div>
    <figure class="header__img--wrapper">
      <img src="https://via.placeholder.com/200" alt="">
    </figure>
  </div>
</header>

【讨论】:

  • @oissa 我添加了关于尺寸和图像的答案 - 在大屏幕上你的可以,但在固定图像尺寸的较小屏幕上不行。可选的是强制图像缩小尺寸 - 现在你的问题是关于“为什么它太远”与你想要的输出是什么;这可能是一个不同的问题和答案
猜你喜欢
  • 2020-01-28
  • 1970-01-01
  • 2016-12-02
  • 2016-10-28
  • 1970-01-01
  • 2015-05-31
  • 2010-10-17
  • 2022-01-22
  • 1970-01-01
相关资源
最近更新 更多