Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Flexbox issue: Why is the width of my flexchildren affected by their contents?

Tags:

html

css

flexbox

The 2 children of my flexbox are each given a style of box-flex: 1. My understanding is that their widths should then be equal to each other (both occupying 50% of the total width of their parent flexbox). But when content is added to the children, their width changes (depending on what the content is and padding)! Why does this happen?

CSS:

.hasFlex {    display: box;    display: -webkit-box;    display: -moz-box;    -webkit-box-align: start;    -moz-box-align: start;    box-align: start;} .box0 {    -webkit-box-flex: 0;    -moz-box-flex: 0;    box-flex: 0;} .box1 {   -webkit-box-flex: 1;   -moz-box-flex: 1;   box-flex: 1;} .box2 {    -webkit-box-flex: 2;    -moz-box-flex: 2;    box-flex: 2;} .box3 {    -webkit-box-flex: 3;    -moz-box-flex: 3;    box-flex: 3;} .container { margin-bottom: 10px; } 

HTML:

<div class="container hasFlex"> <div id="main" role="main" class="box1">     <div class="innerBG">       a bunch of stuff (divs, text, etc) go here     </div> </div> <div id="sidebar" class="box1">     <div class="innerBG">        a bunch more stuff (divs, text, etc.) go here     </div> </div> </div> 
like image 262
zakdances Avatar asked Nov 02 '11 17:11

zakdances


People also ask

How do I fix the width on my flexbox?

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.

Does CSS columns have effect on a Flex container?

1 Answer. Show activity on this post. the column-* properties in the Multi-column Layout module [CSS3COL] have no effect on a flex container.


1 Answers

The workaround for this is to add width: 0 to the .box1 elements.

See: http://jsfiddle.net/thirtydot/fPfvN/

I think I found that out for myself here: http://oli.jp/2011/css3-flexbox/

The preferred width of a box element child containing text content is currently the text without line breaks, leading to very unintuitive width and flex calculations → declare a width on a box element child with more than a few words (ever wonder why flexbox demos are all “1,2,3”?)

Note that for your situation, it looks far easier to use display: table + table-layout: fixed: http://jsfiddle.net/thirtydot/fPfvN/1/. Though, flexbox does allow you to quickly change things around in a way that display: table can't compete with.

like image 176
thirtydot Avatar answered Nov 11 '22 23:11

thirtydot