[英]How to change the width of a CSS flex-box column?
I have a menu div that fills 100% of its container height, and beside it, to the right, are the header , body and footer sections. 我有一个菜单 div填充其容器高度的100%,并在其旁边,右侧,是标题 , 正文和页脚部分。
I managed to do this with the CSS flex property
. 我设法用
CSS flex property
做了这个。 However, I want to change the amount of width the "menu"
div takes out of the container, as in, making it 10% width of container's width
, and make the rest divs fill what's left
(even if the "menu"
div is hidden at a later time). 但是,我想更改
"menu"
div从容器中取出的宽度,如同,使其10% width of container's width
,并使rest divs fill what's left
(即使"menu"
div是隐藏在以后)。
JS Fiddle of what I have right now: https://jsfiddle.net/jf29vr0x/3/ 我现在所掌握的JS小提琴: https : //jsfiddle.net/jf29vr0x/3/
.container { display: flex; flex-flow: column wrap; width: 75%; height: 500px; position: relative; margin: 0 auto; } .menu { flex: 0 0 100%; background: lightblue; } .header { flex: 0 0 10%; background: lightgray; } .body { flex: 0 0 80%; background: purple; } .footer { flex: 0 0 10%; background: lightgreen; }
<div class="container"> <div class="menu"> Menu </div> <div class="header"> Header </div> <div class="body"> Body </div> <div class="footer"> Footer </div> </div>
I found that if I set the width explicitly on the "menu" class, it resizes it, but the rest of the boxes don't fill the space left, unless I also explicitly state their width to 100%. 我发现如果我在“菜单”类中明确设置宽度,它会调整它的大小,但其余的框不会填充剩余的空间,除非我也明确地将它们的宽度指定为100%。 However, doing this, makes the container overflow, and because the container is aligned to the center, it looks off.
但是,这样做会使容器溢出,并且因为容器与中心对齐,所以容器会看起来很松散。
I could also set the width to 90% (on header, footer body) and 10% on the menu, but the menu is sometimes set to display: none;
我还可以将宽度设置为90%(在页眉,页脚主体上)和菜单上的10%,但菜单有时设置为
display: none;
, so when it disappears, the other layout parts are only going to be 90% of container's width. ,所以当它消失时,其他布局部分只会是容器宽度的90%。
Is there any way of doing this with only flex-box properties? 有没有办法只用flex-box属性这样做? I don't understand very well what flex-shrink and flex-grow are about, but I think they only affect (in this case) the height?
我不太了解flex-shrink和flex-grow的含义,但我认为它们只影响(在这种情况下)高度?
Thank you. 谢谢。
You have to add a wrapper around the right column, and then only specify flex-basis
on the .menu
element. 您必须在右列附近添加包装器,然后仅在
.menu
元素上指定flex-basis
。
.container { display: flex; width: 75%; height: 500px; position: relative; margin: 0 auto; height: 500px; overflow: hidden; } .right-col { flex-flow: column wrap; flex-grow: 1; height: 100%; display: flex; } .menu { flex-basis: 10%; background: lightblue; } .header { flex: 0 0 10%; background: lightgray; } .body { flex: 0 0 80%; background: purple; } .footer { flex: 0 0 10%; background: lightgreen; }
<div class="container"> <div class="menu">Menu</div> <div class="right-col"> <div class="header">Header</div> <div class="body">Body</div> <div class="footer">Footer</div> </div> </div>
Fiddle: https://jsfiddle.net/jf29vr0x/11/ 小提琴: https : //jsfiddle.net/jf29vr0x/11/
Now when the menu is hidden, the right column expands the full amount. 现在,当隐藏菜单时,右列会扩展全部数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.