简体   繁体   English

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

[英]CSS Assigning div to any percent ends up filling the entire screen?

I have this very simple div container and whenever I assign it's height to any percent it displays the entire divs content without any overflow? 我有这个非常简单的div容器,每当我将其高度分配给任何百分比时,它都会显示整个divs内容而没有任何溢出? Works when assigned to a set pixel value; 分配给设置的像素值时有效; Working code but static: 工作代码但静态:

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

工作代码但静态

Code that refuses to work and gives weird bug 代码无法正常工作并产生奇怪的错误

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

代码无效(可以向下滚动页面以查看div的其余部分)

you need to set the height of the parent, because if you are using % , then it is relative to something. 您需要设置父级的height ,因为如果使用% ,则它是相对于某物的height Therefore here the 50% has to be relative to some already defined height , so simply add do this: 因此,这里50%必须相对于一些已经定义的height ,因此只需添加即可:

html,body {
  height: 100%;
}

snippet with your code: 代码片段:

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM