我有这个非常简单的div容器,每当我将其高度分配给任何百分比时,它都会显示整个divs内容而没有任何溢出?分配给设置的像素值时有效;
工作代码但静态:

.rounds-left{
  max-height: 500px;
  overflow-y:scroll;
}


html - CSS将div分配给任何百分比最终会填满整个屏幕吗?-LMLPHP

代码无法正常工作并产生奇怪的错误

.rounds-left{
  height: 50%;
  overflow-y:scroll;
}


html - CSS将div分配给任何百分比最终会填满整个屏幕吗?-LMLPHP

最佳答案

您需要设置父级的height,因为如果使用%,则它是相对于某些内容的。因此,这里50%必须相对于某些已经定义的height,因此只需添加即可:

html,body {
  height: 100%;
}


代码片段:



html,
body {
  height: 100%;
  background: red
}
.rounds-left {
  height: 50%;
  overflow-y: scroll;
  background: lightblue;
}

<html>

<body>
  <div class="rounds-left">

    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
    survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
    software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less
    normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem
    ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

  </div>
</body>

</html>

关于html - CSS将div分配给任何百分比最终会填满整个屏幕吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33792831/

10-12 00:10
查看更多