Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is flex-basis being ignored?

Tags:

html

css

flexbox

In the following situation, why is the height of <header> reduced? As far as I can see, <header> should retain the height declared in by flex-basis and <div class="content-wrapper"> should take up the remaining space. This does work until it contains content that is taller than the space available to it. In this situation, <header> partially collapses.

 main {
   position: absolute;
   top: 0;
   bottom: 0;
   left: 0;
   right: 0;
   display: flex;
   flex-direction: column;
 }
 header {
   flex-basis: 50px;
   background: red;
 }
 .content-wrapper {
   background: blue;
   flex: 2;
   overflow-y: auto;
 }
 .content {
   height: 1000px;
   background: green;
 }
<main>
  <header></header>
  <div class="content-wrapper">
    <div class="content"></div>
  </div>
</main>

If you run the snippet full-screen and change the height, the header height changes relative to the screen-height. I would expect it to remain fixed at 50px.

like image 869
Undistraction Avatar asked Jul 02 '15 19:07

Undistraction


People also ask

Should I use width or flex-basis?

There is no difference between how flex-basis: will function on your element and how width: will function on your element. Width will even obey flex-shrink when using Flexbox.

Why Flex-grow is not working?

Firstly, you can't apply a fixed width AND flex-grow. Secondly, flex-grow only works if the parent element has display:flex . In this case the section has display flex but the links do not and the flexgrow divs are children of the link…not the section.

What does flex-basis mean?

The flex-basis property defines the size of the flex-item along the main axis of the flex container. The main axis is horizontal if flex-direction is set to row and it'll be vertical if the flex-direction property is set to column .

How do I get rid of Flex-basis?

If you want to delete flex-basis , you can use width or height instead. When specified on a flex item, the auto keyword retrieves the value of the main size property as the used flex-basis . And the main size property is given by width (in row layouts) or height (in column layouts):


1 Answers

@Eric Martinez' comment is correct. To keep elements from shrinking, set flex-shrink to 0 instead.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

 main {
   position: absolute;
   top: 0;
   bottom: 0;
   left: 0;
   right: 0;
   display: flex;
   flex-direction: column;
 }
 header {
   height: 50px;
   flex-shrink: 0;
   background: red;
 }
 .content-wrapper {
   background: blue;
   flex: 2;
   overflow-y: auto;
 }
 .content {
   height: 1000px;
   background: green;
 }
<main>
  <header></header>
  <div class="content-wrapper">
    <div class="content"></div>
  </div>
</main>
like image 57
Sphinxxx Avatar answered Sep 29 '22 11:09

Sphinxxx