简体   繁体   English

flexbox 内的弹性方向“行”和弹性方向“列”

[英]flex direction "row" inside a flexbox with flex-direction "column"

I am trying to make a navbar using flexbox. In my code I have the actual navbar wrapped with flex- direction:"row" to align the logo and the button.我正在尝试使用 flexbox 制作导航栏。在我的代码中,实际导航栏用 flex-direction:"row" 包裹,以对齐徽标和按钮。 Now I want to have the nav-inner (the beige div) under the navbar (that should be 100vw wide), but actually it sits next to the navbar.现在我想在导航栏(应该是 100vw 宽)下放置 nav-inner(米色 div),但实际上它位于导航栏旁边。 I have tried to change the flex-direction to "column" inside my nav-menu div, but the Hamburger button goes out of the screen.我试图将 flex-direction 更改为我的导航菜单 div 中的“列”,但汉堡包按钮超出了屏幕。 Am I doing something wrong?难道我做错了什么?

 body { margin: 0; overflow: hidden; } /* defaults */.safe-view { display: flex; height: 100vh; }.hamburger { height: 30px; width: 30px; } /**/ /* navbar */.navbar { position: sticky; display: flex; align-items: center; justify-content: space-between; padding: 0 30px; width: 100vw; font-size: 1.2em; height: 100px; }.nav-menu { width: 100%; display: flex; flex-direction:row; /*flex-direction:column;*/ position: absolute; height: 100%; overflow: hidden; background-color: white; }.nav-inner { height: 100%; width: 100%; background-color: blanchedalmond; } /**/
 <div class="safe-view"> <div class="nav-menu"> <div class="navbar"> <h1>logo</h1> <button class="hamburger"></button> </div> <div class="nav-inner"></div> </div> </div>

This is a CSS box-model issue.这是一个 CSS 盒模型问题。 You need to add box-sizing: border-box .您需要添加box-sizing: border-box This will ensure that padding is included in calculation of the width.这将确保填充包含在宽度的计算中。

*{
   box-sizing: border-box;  
}

By default box-sizing is set to content-box .默认情况下 box-sizing 设置为content-box This will only care about the element content and shift padding and border outside of the element.这将只关心元素内容以及元素外部的填充和边框。 That is why you saw the button push out to the right!这就是为什么您看到按钮向右推出的原因! This can also help you to understand further.也可以帮助你进一步理解。

Also, flex-direction for.还有, flex-direction的。 nav-menu needs to be set to column in order to position .nav-inner below. nav-menu需要设置为column以便 position .nav-inner下面。

Heres an alternative.这是另一种选择。 I removed padding and just used calc() function to create padding.我删除了填充,只使用calc() function 来创建填充。 But always include box-sizing:border-box in your CSS:)但始终在您的 CSS 中包含box-sizing:border-box :)

 *{ box-sizing: border-box; } body { margin: 0; padding: 0; overflow: hidden; }.safe-view { display: flex; height: 100vh; }.navbar { position: sticky; display: flex; flex-direction:row; align-items: center; justify-content: space-between; width: calc(100vw - 60px); margin: 0 auto; font-size: 1.2em; height: 100px; }.nav-menu { width: 100%; display: flex; flex-direction:column; position: absolute; height: 100%; overflow: hidden; background-color: white; }.nav-inner { height: 50px; width: 100%; background-color: blanchedalmond; }
 <div class="safe-view"> <div class="nav-menu"> <div class="navbar"> <h1 class="logo">logo</h1> <button class="hamburger">button</button> </div> <div class="nav-inner"></div> </div> </div>

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

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