简体   繁体   English

在不使用100vh的情况下将div强制设为完整页面高度

[英]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.

相关问题 为什么 100% 或 100vh 高度没有占据整个页面高度 - why 100% or 100vh height not occupying full page height 使用 100vh 并具有居中内容的 HTML 全高英雄 - HTML full height hero using 100vh with centered content 为什么即使高度为100%,高度为100vh,也无法获得完整的页面? - Why I can't get full length page even with height 100% and height 100vh? 车身高宽为100vh; 并且其中的 div 宽度为 100vh 或 100% 但它没有接触边缘?为什么 - Body height and width is 100vh; and the div in this has width 100vh or 100% but it is not touching the edge ?why CSS 100vh未将div设置为100%高度 - css 100vh is not setting the div to 100% height 如何在高度为 100Vh 的页面上添加 100vh header 图像? - How to add a 100vh header image on a page with a height of 100Vh? 将一个 div 与其父 div 的底部对齐,同时保持 100VH 的父高度并且不使用固定定位 - Align a div to the bottom of its parent div while maintaining the parent height of 100VH and not using fixed positioning 使用高度100vh / 100%时,移动浏览器页面底部的空白 - Gap on bottom of the page on mobile browsers when using height 100vh/100% 为什么使用100vh作为高度后组件没有拉伸到web页面的高度 - Why is the component not stretching to the height of the web page after using 100vh as the height 将 div 置于高度为 100vh 的背景图像上 - centering a div on background image which has 100vh as height
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM