Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto width of flex column

Tags:

css

flexbox

How can I set the width of first flex column so that its width is only the size of its content? http://codepen.io/anon/pen/rrKkOW

<div class="flex">
  <div class="inside inside-left">Left</div>
  <div class="inside inside-right">RIght</div>
</div>

.flex {
  display: flex;
}

.inside {
  flex: 1;
  border: 1px solid #000;
}

Setting width: auto doesn't work... Something like flex-basis: content would be nice to have :)

like image 979
Ivan Topić Avatar asked Oct 13 '16 10:10

Ivan Topić


People also ask

How do you adjust column width in Flex?

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.

How do you control flex width?

Instead of flex-direction: column , you can try a wrapping flexbox using flex-wrap: wrap ; and you can set: flex-basis: 50% for the half width divs. flex-basis: 100% for the full width divs.

How do you get flex items to take the content width?

Use align-items: flex-start on the container, or align-self: flex-start on the flex items. No need for display: inline-flex . An initial setting of a flex container is align-items: stretch . This means that flex items will expand to cover the full length of the container along the cross axis.


2 Answers

Don't give that column flex:1 as it will expand to take up as much room as is permitted.

In fact, don't give it any flex value at all and it will default to width:auto.

.flex {
  display: flex;
}
.inside {
  border: 1px solid #000;
}
.inside-right {
  flex: 1;
  background:#c0ffee;
}
<div class="flex">
  <div class="inside inside-left">Left</div>
  <div class="inside inside-right">RIght</div>
</div>

<div class="flex">
  <div class="inside inside-left">Left Left Left LeftLeft Left</div>
  <div class="inside inside-right">RIghtRIghtRIght RIght</div>
</div>

<div class="flex">
  <div class="inside inside-left">Left Left Left LeftLeft LeftLeft Left Left LeftLeft Left</div>
  <div class="inside inside-right">RIghtRIghtRIght RIghtRIghtRIght</div>
</div>

EDIT: It seems the intention was to have every "left" element be the same size.

This is not possible with flexbox but CSS Tables can achieve this.

.row {
  display: table-row;
}
.inside {
  display: table-cell;
  border: 1px solid grey;
}
.inside-left {
  background: lightgreen;
}
<div class="row">
  <div class="inside inside-left">Left</div>
  <div class="inside inside-right">RIght</div>
</div>

<div class="row">
  <div class="inside inside-left">Left Left Left LeftLeft Left</div>
  <div class="inside inside-right">RIghtRIghtRIght RIght</div>
</div>

<div class="row">
  <div class="inside inside-left">Left Left Left LeftLeft LeftLeft Left Left LeftLeft Left</div>
  <div class="inside inside-right">RIghtRIghtRIght RIghtRIghtRIght</div>
</div>
like image 181
Paulie_D Avatar answered Sep 30 '22 19:09

Paulie_D


You have to restructure your HTML a bit to achieve what you want:

.flex {
  display: flex;
}
.inside {
  border: 1px solid #000;
}
.flex-grow {
  flex: 1;
}
.inside-right {
  background:#c0ffee;
}
<div class="flex">
  <div>
    <div class="inside">Left</div>
    <div class="inside">Left Left Left LeftLeft Left</div>
    <div class="inside">Left Left Left LeftLeft LeftLeft Left Left LeftLeft Left</div>
  </div>
  <div class="flex-grow">
    <div class="inside inside-right">Right</div>
    <div class="inside inside-right">RIghtRIghtRIght RIght</div>
    <div class="inside inside-right">RIghtRIghtRIght RIghtRIghtRIght</div>
  </div>
</div>
like image 43
YingYang Avatar answered Sep 30 '22 19:09

YingYang