简体   繁体   English

CSS 将侧边栏安装在顶栏下方

[英]CSS to fit sidebar under topbar

I need to create an empty topbar and an empty sidebar as below.我需要创建一个空的顶栏和一个空的侧边栏,如下所示。 I want to stack them neatly so that the top of the sidebar touches the bottom of the topbar.我想将它们整齐地堆叠起来,以便侧边栏的顶部接触到顶栏的底部。 For that, I am using percentage.为此,我使用百分比。 However, I just can't get them to do that.但是,我就是不能让他们这样做。

I saw some other threads suggesting to set html and body with height: 100%;我看到其他一些线程建议设置htmlbody with height: 100%; and width: 100% .width: 100% But in my app, the layout below is not in the main html document.但在我的应用程序中,下面的布局不在 html 主文档中。

 #nav-bar { position: absolute; top: 0; left: 0; background-color: blue; height: 10%; width: 100%; } #side-bar { position: absolute; top: 0; left: 0; background-color: #011a21; height: 90%; width: 7.5%; /*margin-top: 10%;*/ }
 <div id="nav-bar">&nbsp;</div> <div id="side-bar"></div>

I tend to use flex box for this, Maybe grid would be smarter though.我倾向于为此使用弹性框,但也许网格会更聪明。 I took also care of some scrolling things.我还处理了一些滚动的事情。 Using min with and height 0 on flex parents is key for that.在弹性父母上使用 min 和 height 0 是关键。

 body, section { min-height: 0; min-width: 0; } body { padding: 0; margin: 0; display: flex; flex-direction: column; height: 100vh; } nav { height: 2rem; background: green; } section { flex: 1 1 0%; display: flex; } aside { width: 4rem; background: blue; height: 100%; } main { background: pink; flex: 1 1 0%; overflow: auto; padding: 1rem; } div { border: 1px black dashed; height: 300%; }
 <nav></nav> <section> <aside></aside> <main> <div> Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum, tempora. Voluptates, iure quis enim hic eius ullam sequi suscipit maiores, eum consectetur nesciunt quae dolorum. Voluptate adipisci praesentium illum dolorem. <br> Lorem ipsum dolor sit amet consectetur adipisicing elit, Rerum. tempora, Voluptates, iure quis enim hic eius ullam sequi suscipit maiores. eum consectetur nesciunt quae dolorum. Voluptate adipisci praesentium illum dolorem, <br> Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum, tempora, Voluptates. iure quis enim hic eius ullam sequi suscipit maiores. eum consectetur nesciunt quae dolorum, Voluptate adipisci praesentium illum dolorem. <br> Lorem ipsum dolor sit amet consectetur adipisicing elit, Rerum, tempora. Voluptates, iure quis enim hic eius ullam sequi suscipit maiores, eum consectetur nesciunt quae dolorum! Voluptate adipisci praesentium illum dolorem. <br> </div> </main> </section>

Your #nav-bar has a height: 10% , and you can set the same value for the top rule, selector #side-bar .您的#nav-bar有一个height: 10% ,您可以为top规则设置相同的值,选择器#side-bar

 #nav-bar { position: absolute; top: 0; left: 0; background-color: blue; height: 10%; width: 100%; } #side-bar { position: absolute; top: 10%; left: 0; background-color: #011a21; height: 90%; width: 7.5%; /*margin-top: 10%;*/ }
 <div id="nav-bar">&nbsp;</div> <div id="side-bar"></div>

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

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