【问题标题】:Items inside of container won't stay inside parent despite child having overflow set to "auto"尽管子级溢出设置为“自动”,但容器内的项目不会留在父级内
【发布时间】:2021-09-14 20:40:51
【问题描述】:

我有点吃不消了。已经搜索和测试了一段时间,但似乎无法让它工作。

这是我目前得到的:

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  height: 100%;
  font-family: Arial, sans-serif;
}

.container {
  position: fixed;
  top: 0;
  right: 0;
  height: 100%;
  width: 30%;
  background-color: #f9f9f9;
  padding: 20px 30px;
}

.box-container {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  height: 100%;
  overflow-y: auto;
}

.box {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 150px;
  width: 49%;
  background-color: #FFDAC7;
  margin-bottom: 5px;
  font-size: 32px;
}
<div class="container">
  <h2>lorem ipsum dolor sit amet</h2>
  <h3>Some other stuff</h3>
  <div class="box-container">
    <div class="box">1</div>
    <div class="box">2</div>
    <div class="box">3</div>
    <div class="box">4</div>
    <div class="box">5</div>
    <div class="box">6</div>
    <div class="box">7</div>
    <div class="box">8</div>
    <div class="box">9</div>
    <div class="box">10</div>
  </div>
  <h5>Maybe a button here</h5>
</div>

现在,我希望“box-container” div 只占用 100% 的可用空间,并且让子级可滚动,但事实并非如此。

似乎我遗漏了一些明显的东西。

这支笔适合喜欢的人,顺便说一句:https://codepen.io/georgiosApo/pen/gORxJwb

提前感谢大家的指点!

// G

【问题讨论】:

  • 只需将overflow-y: auto; 添加到.container

标签: html css scroll overflow


【解决方案1】:

像这样改变你css

.container {
  height: 100vh;
  width: 100%;
  background-color: #f9f9f9;
  padding: 20px 30px;
}

Working example

【讨论】:

  • 看起来,要保持在右侧,请使用position: absolute; right: 0; width: 30%;
  • 是的,也许“固定”位置在这里引起了一些问题。尝试“绝对”,但这并不能真正解决我的问题。现在整个容器都在滚动,不仅仅是我希望的 box-container div。不过谢谢!
  • 我不知道您只希望 box-container 可滚动。你能检查一下吗:jsfiddle.net/exypgv75/31
  • 感谢您的帮助!这确实为我指明了正确的方向。该死,这真的困扰了我一段时间。在我遇到这个问题的主要代码中,我在侧边栏中有一个子 div,我想让它自己的子级溢出 auto。这似乎真的不可能,所以我结束了将所有孩子放在最顶层的容器中以使其工作。谢谢。我会将此标记为正确答案。如果您有更多关于嵌套子级和溢出自动的提示,请告诉我。再次欢呼并感谢您的帮助!
  • 你好@NenadMilosavljevic !我做了一个新的小提琴来说明嵌套容器的问题。知道为什么这不起作用吗?虽然我的问题已解决,但我仍然有点困扰,我似乎无法真正理解为什么这不能像我预期的那样工作:jsfiddle.net/y8bc6dw5 再次感谢!
猜你喜欢
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
  • 2015-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多