[英]Forcing a div to be full page height without using 100vh
This is a simplified HTML & CSS code to what I currently have in project. 这是我目前在项目中拥有的简化的HTML和CSS代码。 Essentially since I'm using ReactJS & routes, I can't wrap everything inside another container with a css grid so I'm looking for solutions to get full page height on both the 'sidebar' & 'content' classes. 本质上,由于我使用ReactJS和路由,因此无法使用CSS网格将所有内容包装在另一个容器中,因此我正在寻找解决方案以在'sidebar'和'content'类上获得完整的页面高度。
I do understand that I could use 100vh and substract from that the height of the header but in this case the header doesn't have a fixed height so I'm looking for alternatives. 我确实知道我可以使用100vh并从标头的高度中减去,但是在这种情况下标头没有固定的高度,因此我正在寻找替代方案。
.header { display: grid; grid-template-columns: auto auto; background-color: lightblue; } .main { display: grid; grid-template-columns: 0.2fr 1fr; } .sidebar { background-color: lightpink; height: 100%; } .content { background-color: orange; height: 100%; }
<div class="header"> <div> One </div> <div> Two </div> </div> <div class="main"> <div class="sidebar"> Menu </div> <div class="content"> Content </div> </div>
http://jsfiddle.net/de5ut6np/39/ http://jsfiddle.net/de5ut6np/39/
You can consider the body as the main container: 您可以将主体视为主要容器:
html { height:100%; } body { margin:0; height:100%; display: grid; grid-template-rows: max-content 1fr; } .header { display: grid; grid-template-columns: auto auto; background-color: lightblue; } .main { display: grid; grid-template-columns: 0.2fr 1fr; } .sidebar { background-color: lightpink; height: 100%; } .content { background-color: orange; height: 100%; }
<div class="header"> <div> One<br> more </div> <div> Two </div> </div> <div class="main"> <div class="sidebar"> Menu </div> <div class="content"> Content </div> </div>
I tested like this and it worked: 我像这样测试,它的工作原理:
html { height:100%; } body { margin: 0; height:100%; display: grid; grid-template-rows: max-content 1fr; } .header { display: grid; grid-template-columns: auto auto; background-color: lightblue; } .main { display: grid; grid-template-columns: 0.2fr 1fr; } .sidebar { display: grid; background-color: lightpink; } .content { display: grid; background-color: orange; }
<div class="header"> <div> One </div> <div> Two </div> </div> <div class="main"> <div class="sidebar"> Menu </div> <div class="content"> Content </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.