Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the width of a CSS flex-box column?

Tags:

html

css

flexbox

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.

I managed to do this with the 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).

JS Fiddle of what I have right now: 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%. 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;, so when it disappears, the other layout parts are only going to be 90% of container's width.

Is there any way of doing this with only flex-box properties? 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?

Thank you.

like image 847
Jimmy Coldfeet Avatar asked Mar 11 '15 22:03

Jimmy Coldfeet


People also ask

How do you adjust column width in Flex?

A flexbox item can be set to a fixed width by setting 3 CSS properties — flex-basis, flex-grow & flex-shrink. flex-basis : This property specifies the initial length of the flex item. flex-grow : This property specifies how much the flex item will grow relative to the rest of the flex items.

How do I change the width and height of my flexbox?

It can be changed by using the flex-direction property. To use flexbox, we have to set display: flex or inline-flex to flex-container. By default, the height and width of a flex-container are set to auto. But we can define a fixed amount to them.

How do I increase the width of my display flex?

You have to add a wrapper around the right column, and then only specify flex-basis on the . menu element. Now when the menu is hidden, the right column expands the full amount. Great, that works as expected!

How do I make my flex item not stretch width?

By default, the child elements of a flexbox container will stretch vertically to fill the height of the container. This can be prevented by using the align-self property on the child element that you do not want to stretch.


1 Answers

You have to add a wrapper around the right column, and then only specify flex-basis on the .menu element.

.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/

Now when the menu is hidden, the right column expands the full amount.

like image 116
ratherblue Avatar answered Nov 15 '22 05:11

ratherblue