I’m using Flexbox to make 5 boxes responsive when the browser window is shrunk. 4 of the boxes are in 2 columns and fail to shrink (flex-direction: column
). The single box that is not in a column shrinks correctly.
(Bob is behaving correctly, but Kate, Nick, Jane and Fred refuse to reduce their width.)
How do I make the width of the columns shrink as the browser window size is reduced?
.container {
display: flex;
width: 100%;
justify-content: center;
border: 2px solid red;
}
.column {
display: flex;
flex-direction: column;
}
.big {
display: flex;
width: 400px;
height: 400px;
background: yellow;
border: 5px solid blue;
}
.small {
display: flex;
width: 195px;
height: 195px;
background: yellow;
border: 5px solid blue;
}
<div class="container">
<div class="big"><span>Bob</span></div>
<div class="column">
<div class="small"><span>Kate</span></div>
<div class="small"><span>Nick</span></div>
</div>
<div class="column">
<div class="small"><span>Jane</span></div>
<div class="small"><span>Fred</span></div>
</div>
</div>
Demo here: https://jsfiddle.net/CodeCabbie/1xvjeo3p/44/
3
Answers
Using fixed width (
width: 195px;
) for the small boxes using the.small
class seems strange, and would explain why those columns do not shrink as the window resizes.A combination of:
flex-basis
for the small boxesflex-grow
property on the columnsThat would allow all columns to distribute the remaining space accordingly.
Something like this example: https://jsfiddle.net/ko4Lszdy/
If it is not essential to use a
flexbox
, it is better to create all this using agrid
layout. As a bonus, you will get a more concise and understandable code:Just add
min-width: 0
to the.column
classFIDDLE