Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed width child is shrinking in flexbox [duplicate]

Tags:

html

css

flexbox

I'm using display:flex for the first time.

You find a codepen here: https://codepen.io/chrispillen/pen/GEYpRg

.node:not(.branch) {
  width: auto;
  height: 100px;
  background: red;
  margin-bottom: 1px;
}

.node.branch,
.node.branch .body {
  width: auto;
  overflow: hidden;
}

.node.branch .leaf {
  width: 100%;
  height: 100px;
  background: blue;
}

.node.branch .body {
  width: 100%;
  display: flex;
  align-items: stretch;
}

.node.branch .body .tribe {
  width: 100px;
  background: blue;
  margin-right: 1px;
}

.node.branch .body .subnodes {
  padding-top: 1px;
  width: 100%;
  overflow: hidden;
}
<div class="node"></div>
<div class="node branch">
  <div class="leaf"></div>
  <div class="body">
    <div class="tribe">TRIBE</div>
    <div class="subnodes">
      <div class="node"></div>
      <div class="node"></div>
    </div>
  </div>
  <div class="leaf"></div>
</div>

The "tribe" element doesn't remain with a width of 100px.

Everything else works as supposed. Can I force it somehow? And by the way: is flex the true reason for this behavior?

like image 492
Chris Pillen Avatar asked Jul 08 '17 12:07

Chris Pillen


People also ask

How do I stop my flexbox from shrinking?

Add a min-width with whatever you want the smallest possible value of the box to be. Flexbox won't shrink the width below the min-width. min-width does work fine, but in responsive design it doesn't adjust with the space if its less than the specified.

How do I fix the width on my flexbox?

Solution with Flexbox If you want to have a fixed-width column with Flexbox, you need to use the CSS flex or flex-basis property. First of all, we set the display of our <div> container to "flex". Then, we specify the flex of the "grey" class as "0 0 50px".

How do you stop DIVS from shrinking?

Simply add a min-width size to the things you want to stop shrinking :) Show activity on this post. min-width:500px; would cause the window to have a minimum width of 500px.


1 Answers

Flexbox items have flex-shrink parameter with default of 1. This means that in case your flexbox container don't have enough space its items will shrink.

To disable this behaviour for flexbox item set flex-shrink: 0.

Demo:

.node:not(.branch) {
  width: auto;
  height: 100px;
  background: red;
  margin-bottom: 1px;
}

.node.branch,
.node.branch .body {
  width: auto;
  overflow: hidden;
}

.node.branch .leaf {
  width: 100%;
  height: 100px;
  background: blue;
}

.node.branch .body {
  width: 100%;
  display: flex;
  align-items: stretch;
}

.node.branch .body .tribe {
  width: 100px;
  flex-shrink: 0; /* new */
  background: blue;
  margin-right: 1px;
}

.node.branch .body .subnodes {
  padding-top: 1px;
  width: 100%;
  overflow: hidden;
}
<div class="node"></div>

<div class="node branch">
  <div class="leaf"></div>

  <div class="body">
    <div class="tribe">TRIBE</div>

    <div class="subnodes">
      <div class="node"></div>
      <div class="node"></div>
    </div>
  </div>
  
  <div class="leaf"></div>
</div>
like image 94
Vadim Ovchinnikov Avatar answered Oct 19 '22 03:10

Vadim Ovchinnikov