Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flex-basis not working as expected

Tags:

css

flexbox

As i understand flex-basis is responsible for deciding the size of an element.

In the example below, I am trying to size all boxes equally to 100px. Just using flex-basis is not achieving the effect.

.each_box {
  -webkit-flex-grow: 0;
  -webkit-flex-shrink: 0;
  -webkit-flex-basis: 100px;

  flex-grow: 0;
  flex-shrink: 0;
  flex-basis: 100px;

  background-color: yellow;
  height: 50px;

  border: 1px solid black;

}

Plnkr here: http://plnkr.co/edit/LvrrzHWIw1tPGwK05bCU

like image 930
runtimeZero Avatar asked Jan 24 '15 16:01

runtimeZero


People also ask

Why is flex-basis not working?

You need to put your each_box divs directly inside of a display:flex element for them to honor their flex-related properties. You've got container styled as display:flex , but that does no good for your each_box elements, because they're grandchildren, separated from the flex container by the display:block ng-scope.

How does flex-basis work?

flex-basis: auto looks up the main size of the element and defines the size. For example, on a horizontal flex container, auto will look for width and height if the container axis is vertical. If no size is specified, auto will fall back to content .

Should I use flex-basis or width?

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.


1 Answers

The flex-grow, flex-shrink, flex-basis properties only have an effect on elements in a flex container -- i.e. elements whose parent has display:flex.

You need to put your each_box divs directly inside of a display:flex element for them to honor their flex-related properties.

Specifically, your markup looks like this (from right clicking one of the yellow divs + hitting "inspect" in Firefox):

<div class="container">
  <!-- ngRepeat: stock in stockList -->
  <div class="ng-scope" ng-repeat="stock in stockList">
    <div class="each_box ng-binding">
    0-FB/100

You've got container styled as display:flex, but that does no good for your each_box elements, because they're grandchildren, separated from the flex container by the display:block ng-scope.

So you either need to get rid of the ng-scope wrapper, or make it also have display:flex.

like image 53
dholbert Avatar answered Oct 05 '22 12:10

dholbert