In the previous two exercises, the dimensions of the divs were determined by heights and widths set with CSS. Another way of specifying the width of a flex item is with the flex-basis
property. flex-basis
allows us to specify the width of an item before it stretches or shrinks.
<div class='container'> <div class='side'> <h1>Left side!</h1> </div> <div class='center'> <h1>Center!</h1> </div> <div class='side'> <h1>Right side!</h1> </div> </div>
.container { display: flex; } .side { flex-grow: 1; flex-basis: 100px; } .center { flex-grow: 2; flex-basis: 150px; }
In the example above, the .side
divs will be 100 pixels wide and the .center
div will be 150 pixels wide if the .container
div has just the right amount of space (350 pixels, plus a little extra for margins and borders). If the .container
div is larger, the .center
div will absorb twice as much space as the .side
divs.
The same would hold true if we assigned flex-shrink
values to the divs above as well.
Instructions
In style.css, inside the .grow.side
ruleset, add a flex-basis of 60px
.
In the same rule, add a flex-grow
value of 1
.
In the .grow.center
rule, add a flex-grow
property with a value of 3
.
In the .shrink.side
rule, add a flex-basis
property with a value of 300px
.
In the same rule, add a flex-shrink
property with a value of 3
.
In the .shrink.center
rule, add a flex-shrink
property with a value of 2
.
In the same rule, add a flex-basis
property with a value of 150px
. Now stretch and shrink the browser.