【问题标题】:Why does a vertical scrollbar appear on the page?为什么页面上会出现垂直滚动条?
【发布时间】:2021-09-24 05:37:33
【问题描述】:

我正在创建一个名为 Catty Music 的示例 flexbox 项目。 我看到的是出现了一个垂直滚动条。我想要做的是在底部有一个固定的div 与播放器控件@ 987654327@ 高度和位于其正上方的可滚动主要内容区域,它将填充视口高度的剩余部分 - 没有滚动条. 您可以在HERE 或以下 sn-p 看到它的快照。

html {
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

html,
body {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  background-color: #fff;
  margin: 0;
  font-family: Lato, sans-serif;
  color: #222;
  font-size: 0.9em;
}

main {
  flex: 1 0 auto;
  height: calc(100vh - 90px);
  overflow: auto;
  display: flex;
}

.content {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
}

.music-head {
  display: flex;
  flex: 0 0 280px;
  padding: 40px;
  background-color: #4e4e4e;
}

.music-list {
  flex: 1 0 auto;
  list-style-type: none;
  padding: 5px 10px 0px;
}

li {
  display: flex;
  padding: 0 20px;
  min-height: 50px;
}

li p {
  flex: 0 0 25%;
}

li span.catty-cloud {
  border: 1px solid black;
  font-size: 0.6em;
  padding: 3px;
}

li:nth-child(2n) {
  background-color: #f2f2f2;
}

.catty-music {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  font-weight: 300;
  color: #fff;
  padding-left: 50px;
}

.catty-music div:nth-child(1) {
  margin-bottom: auto;
}

.catty-music div:nth-child(2) {
  margin-top: 0;
}

.catty-music div:nth-child(2) i.fa {
  font-size: 0.9em;
  padding: 0 0.7em;
}
.catty-music div:nth-child(1) p:first-child {
  font-size: 1.8em;
  margin: 0 0 10px;
}

aside {
  display: flex;
  flex-direction: column;
  flex: 0 0 40px;
  justify-content: space-around;
  align-items: center;
  background-color: #f2f2f2;
}

aside i.fa,
aside i.fab {
  font-size: 0.9em;
}

footer {
  display: flex;
  flex: 0 0 90px;
  padding: 10px;
  color: #fff;
  background-color: rgba(61, 100, 158, 0.9);
}

footer img {
  width: 90px;
}

.footer-current-song-details {
  margin-left: 10px;
  margin-right: auto;
}

.footer-music-controls {
  display: flex;
  width: 50%;
  margin-right: 10px;
  justify-content: space-between;
  align-items: center;
}

@media screen and (max-width: 670px) {
  .music-head {
    flex-direction: column;
  }

  .catty-music {
    padding: 0;
  }

  .catty-music * {
    text-align: center;
  }

  .catty-music div:nth-child(1) p:first-child {
    font-size: 1em;
    margin: 10px 0 10px;
  }

  .music-head > img {
    margin: auto;
    max-width: 50%;
  }

  .artist-details,
  .album-details {
    display: none;
  }
}
<main>
  <aside>
    <i class="fa fa-bars"></i> <i class="fa fa-home"></i>
    <i class="fa fa-search"></i> <i class="fa fa-volume-up"></i>
    <i class="fa fa-user"></i> <i class="fab fa-spotify"></i>
    <i class="fa fa-cog"></i> <i class="fab fa-soundcloud"></i>
  </aside>
  <section class="content">
    <div class="music-head">
      <img src="images/cattyboard.jpg" />
      <section class="catty-music">
        <div>
          <p>CattyBoard Top 100 Single Charts (11.06.36)</p>
          <p class="artist-details">Unknown Artist</p>
          <p class="album-details">2016 . Charts . 100 songs</p>
        </div>
        <div>
          <i class="fa fa-play"> Play all</i>
          <i class="fa fa-plus"> Add to</i>
          <i class="fa fa-ellipsis-h"> More</i>
        </div>
      </section>
    </div>
    <ul class="music-list">
      <li>
        <p>1. One Dance</p>
        <p>Crake feat CatKid & Cyla</p>
        <p>2:54</p>
        <p><span class="catty-cloud">CATTY CLOUD SYNC</span></p>
      </li>
      <li>
        <p>2. Panda</p>
        <p>Cattee</p>
        <p>4:06</p>
        <p><span class="catty-cloud">CATTY CLOUD SYNC</span></p>
      </li>
      <li>
        <p>3. Can't Stop the Feeling!</p>
        <p>Catin Cimberlake</p>
        <p>3:56</p>
        <p><span class="catty-cloud">CATTY CLOUD SYNC</span></p>
      </li>
      <li>
        <p>4. Work From Home</p>
        <p>Cat Harmony feat Colla</p>
        <p>3:34</p>
        <p><span class="catty-cloud">CATTY CLOUD SYNC</span></p>
      </li>
      <li>
        <p>1. One Dance</p>
        <p>Crake feat CatKid & Cyla</p>
        <p>2:54</p>
        <p><span class="catty-cloud">CATTY CLOUD SYNC</span></p>
      </li>
      <li>
        <p>2. Panda</p>
        <p>Cattee</p>
        <p>4:06</p>
        <p><span class="catty-cloud">CATTY CLOUD SYNC</span></p>
      </li>
    </ul>
  </section>
</main>
<footer>
  <img src="images/cattyboard.jpg" />
  <div class="footer-current-song-details">
    <p>If It Ain't Love</p>
    <p>Catson Derulo</p>
  </div>
  <div class="footer-music-controls">
    <i class="fa fa-step-backward"></i>
    <i class="fa fa-pause"></i>
    <i class="fa fa-step-forward"></i>
    <i class="fas fa-redo"></i>
    <i class="fa fa-random sm-hide"></i>
  </div>
</footer>

我的计算或浏览器的计算似乎有误。 我看到的是我的电脑上&lt;main&gt;&lt;footer&gt; 元素的总和是744px。 window.innerHeight 也给出 744px。 &lt;html&gt; 元素的高度也为 744px。我不知道这个滚动条是从哪里出现的。 Firefox DevTools 在&lt;html&gt; 元素上有一个标签,上面写着“这个元素导致元素溢出”。 &lt;html&gt; 是根元素,不是吗?那么&lt;html&gt; 元素使哪个外部元素溢出?

如果有人帮助我了解这里发生了什么,我会很高兴。

【问题讨论】:

标签: html css flexbox scrollbar overflow


【解决方案1】:

您忘记了页脚中的padding: 10px。所以你必须将main 的计算改为height: calc(100vh - 110px) (90px + 2x 10px)。

工作示例:

html {
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

html,
body {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  background-color: #fff;
  margin: 0;
  font-family: Lato, sans-serif;
  color: #222;
  font-size: 0.9em;
}

main {
  flex: 1 0 auto;
  height: calc(100vh - 110px);
  overflow: auto;
  display: flex;
}

.content {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
}

.music-head {
  display: flex;
  flex: 0 0 280px;
  padding: 40px;
  background-color: #4e4e4e;
}

.music-list {
  flex: 1 0 auto;
  list-style-type: none;
  padding: 5px 10px 0px;
}

li {
  display: flex;
  padding: 0 20px;
  min-height: 50px;
}

li p {
  flex: 0 0 25%;
}

li span.catty-cloud {
  border: 1px solid black;
  font-size: 0.6em;
  padding: 3px;
}

li:nth-child(2n) {
  background-color: #f2f2f2;
}

.catty-music {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  font-weight: 300;
  color: #fff;
  padding-left: 50px;
}

.catty-music div:nth-child(1) {
  margin-bottom: auto;
}

.catty-music div:nth-child(2) {
  margin-top: 0;
}

.catty-music div:nth-child(2) i.fa {
  font-size: 0.9em;
  padding: 0 0.7em;
}
.catty-music div:nth-child(1) p:first-child {
  font-size: 1.8em;
  margin: 0 0 10px;
}

aside {
  display: flex;
  flex-direction: column;
  flex: 0 0 40px;
  justify-content: space-around;
  align-items: center;
  background-color: #f2f2f2;
}

aside i.fa,
aside i.fab {
  font-size: 0.9em;
}

footer {
  display: flex;
  flex: 0 0 90px;
  padding: 10px;
  color: #fff;
  background-color: rgba(61, 100, 158, 0.9);
}

footer img {
  width: 90px;
}

.footer-current-song-details {
  margin-left: 10px;
  margin-right: auto;
}

.footer-music-controls {
  display: flex;
  width: 50%;
  margin-right: 10px;
  justify-content: space-between;
  align-items: center;
}

@media screen and (max-width: 670px) {
  .music-head {
    flex-direction: column;
  }

  .catty-music {
    padding: 0;
  }

  .catty-music * {
    text-align: center;
  }

  .catty-music div:nth-child(1) p:first-child {
    font-size: 1em;
    margin: 10px 0 10px;
  }

  .music-head > img {
    margin: auto;
    max-width: 50%;
  }

  .artist-details,
  .album-details {
    display: none;
  }
}
<main>
  <aside>
    <i class="fa fa-bars"></i> <i class="fa fa-home"></i>
    <i class="fa fa-search"></i> <i class="fa fa-volume-up"></i>
    <i class="fa fa-user"></i> <i class="fab fa-spotify"></i>
    <i class="fa fa-cog"></i> <i class="fab fa-soundcloud"></i>
  </aside>
  <section class="content">
    <div class="music-head">
      <img src="images/cattyboard.jpg" />
      <section class="catty-music">
        <div>
          <p>CattyBoard Top 100 Single Charts (11.06.36)</p>
          <p class="artist-details">Unknown Artist</p>
          <p class="album-details">2016 . Charts . 100 songs</p>
        </div>
        <div>
          <i class="fa fa-play"> Play all</i>
          <i class="fa fa-plus"> Add to</i>
          <i class="fa fa-ellipsis-h"> More</i>
        </div>
      </section>
    </div>
    <ul class="music-list">
      <li>
        <p>1. One Dance</p>
        <p>Crake feat CatKid & Cyla</p>
        <p>2:54</p>
        <p><span class="catty-cloud">CATTY CLOUD SYNC</span></p>
      </li>
      <li>
        <p>2. Panda</p>
        <p>Cattee</p>
        <p>4:06</p>
        <p><span class="catty-cloud">CATTY CLOUD SYNC</span></p>
      </li>
      <li>
        <p>3. Can't Stop the Feeling!</p>
        <p>Catin Cimberlake</p>
        <p>3:56</p>
        <p><span class="catty-cloud">CATTY CLOUD SYNC</span></p>
      </li>
      <li>
        <p>4. Work From Home</p>
        <p>Cat Harmony feat Colla</p>
        <p>3:34</p>
        <p><span class="catty-cloud">CATTY CLOUD SYNC</span></p>
      </li>
      <li>
        <p>1. One Dance</p>
        <p>Crake feat CatKid & Cyla</p>
        <p>2:54</p>
        <p><span class="catty-cloud">CATTY CLOUD SYNC</span></p>
      </li>
      <li>
        <p>2. Panda</p>
        <p>Cattee</p>
        <p>4:06</p>
        <p><span class="catty-cloud">CATTY CLOUD SYNC</span></p>
      </li>
    </ul>
  </section>
</main>
<footer>
  <img src="images/cattyboard.jpg" />
  <div class="footer-current-song-details">
    <p>If It Ain't Love</p>
    <p>Catson Derulo</p>
  </div>
  <div class="footer-music-controls">
    <i class="fa fa-step-backward"></i>
    <i class="fa fa-pause"></i>
    <i class="fa fa-step-forward"></i>
    <i class="fas fa-redo"></i>
    <i class="fa fa-random sm-hide"></i>
  </div>
</footer>

【讨论】:

  • 啊!我懂了!我在想box-sizing: border-box 的填充将从 90px 高度中减去。然而flex-basis 似乎不是这样工作的。对我来说有趣的是,如果我添加 height:90px 那么它会变为 70px 高度 + 2x10px 填充,但 calc(100vh - 90px) 仍然提供滚动条。
  • 所以一个错误似乎是这里问题的一部分。 bugzilla.mozilla.org/show_bug.cgi?id=1720680
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 2017-07-18
相关资源
最近更新 更多