简体   繁体   English

div 的高度没有填充 100% 的身高

[英]height of div is not filling 100% body height

I have a sidebar a header and a main container.我有一个侧边栏一个标题和一个主容器。 main container and header is nested inside a parent div.主容器和标题嵌套在父 div 中。 When I set height 100% for both sidebar and main container the sidebar doesn't take 100% of the height of the body.当我为侧边栏和主容器设置高度 100% 时,侧边栏不会占用主体高度的 100%。 I was thinking to not use any percentage values and let flexbox do the work.我在考虑不使用任何百分比值,让 flexbox 完成工作。 But I suspect that as I append elements inside the main container area sidebar will behave the same and not take the height of the body space.但我怀疑当我在主容器区域侧边栏中附加元素时,其行为会相同,并且不会占用主体空间的高度。 How can I fix this?我怎样才能解决这个问题?

 html { height: 100%; } body { margin: 0; } .sidebar { display: inline-flex; min-height: 100vh; } .sidebar-container { background-color: #00ffff; width: fit-content; } .header_main { display: inline-block; position: absolute; color: white; } .header { background-color: black; width: 100vw; } .main { height: 100vh; width: 100vw; background-color: bisque; }
 <div class="sidebar"> <div class="sidebar-container">sidebar</div> </div> <div class="header_main"> <div class="header">header</div> <div class="main">main</div> </div>

Easiest way would be to use CSS-Grid.最简单的方法是使用 CSS-Grid。 Then simply set the body as grid-container and give it a min-height: 100vh :然后简单地将 body 设置为 grid-container 并给它一个min-height: 100vh

 body { margin: 0; min-height: 100vh; display: grid; grid-template-columns: min-content auto; grid-template-rows: min-content auto; } aside { width: fit-content; background-color: #00ffff; grid-row: 1 / -1; } header { background-color: pink; } main { background-color: bisque; }
 <aside>Sidebar</aside> <header>Header</header> <main>Main</main>

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

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