Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifing width of a flexbox flex item: width or basis? [duplicate]

Tags:

css

flexbox

Say I'm doing 3 flex columns, first one 50%, the other two auto adjust.

.half {     flex: 0 0 auto ;     width: 50% ; } 

or

.half {     flex: 0 0 50%; } 

These seem to be functionally the same. Are they?

like image 792
Doug Cassidy Avatar asked Mar 31 '16 20:03

Doug Cassidy


People also ask

How do I set flexbox item width?

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.

Is Flex basis width or height?

Flex-basis is both width and height in a Flexbox, depending on the flex direction.

What's the purpose of Flex basis property and how it is different from width property?

When using flex-basis , the container ignores the sizing of its children, and the children overflow the container. But with the width property, the container respects the sizing of its children and expands accordingly.

How do you increase the width of a flex item?

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


1 Answers

The bottom statement is equivalent to:

.half {    flex-grow: 0;    flex-shrink: 0;    flex-basis: 50%; } 

Which, in this case, would be equivalent as the box is not allowed to flex and therefore retains the initial width set by flex-basis.

Flex-basis defines the default size of an element before the remaining space is distributed so if the element were allowed to flex (grow/shrink) it may not be 50% of the width of the page.

I've found that I regularly return to https://css-tricks.com/snippets/css/a-guide-to-flexbox/ for help regarding flexbox :)

like image 51
Charlie Frater Avatar answered Sep 20 '22 17:09

Charlie Frater