Flex Container With Two Columns; Second Column Has Four Rows
I am having difficulty displaying the following layout in flex. I have 5 boxes and I want to divide my container in two, displaying one box vertically and the other 4 vertically.
Solution 1:
I'm not entirely clear on your question or code. But here's a general solution:
flex-container-1 {
display: flex; /* establish flex container */flex-direction: row; /* flex items will align horizontally */justify-content: center; /* center flex items horizontally */align-items: center; /* center flex items vertically *//* for demo purposes only */height: 250px;
width: 400px;
border: 1px solid #777;
background-color: lightgreen;
}
flex-container-1 > flex-item {
height: 90%;
flex: 0045%; /* <flex-grow> <flex-shrink> <flex-basis> */margin-right: 8px; /* a bit of space between the centered items */border: 1px dashed #333;
background-color: yellow;
}
flex-container-2 {
height: 90%;
flex: 0045%;
display: flex; /* flex item is now also flex container */flex-direction: column; /* items will stack vertically */justify-content: space-between; /* align items vertically */
}
flex-container-2 > flex-item {
flex: 0022%;
border: 1px dashed #333;
background-color: yellow;
}
<flex-container-1><!-- main container --><flex-item></flex-item><!-- flex item #1 (first column) --><flex-container-2><!-- flex item #2 / nested flex container (second column) --><flex-item></flex-item><flex-item></flex-item><flex-item></flex-item><flex-item></flex-item></flex-container-2><!-- close nested container --></flex-container-1><!-- close main container -->
jsFiddle
Solution 2:
I struggled and struggled on this one and then serendipitously discovered a new solution to this problem right as I had decided to give up and use floats. I was finally able to get this to work without using separate DIVs for columns.
UPDATE: I have simplified my previous version of this by having the height specified on .items.
- Provide non-percentage
width
andheight
to.items
. - Use
flex-direction: column
on.items
.
CSS:
.items {
display: flex;
flex-direction: column;
flex-wrap: wrap;
width: 40em;
height: 20em;
}
.item:first-child {
width: 20em;
height: 20em;
background-color: black;
}
.item:nth-child(2) {
width: 20em;
height: 5em;
background-color: pink;
}
.item:nth-child(3) {
width: 20em;
height: 5em;
background-color: blue;
}
.item:nth-child(4) {
width: 20em;
height: 5em;
background-color: yellow;
}
.item:last-child {
width: 20em;
height: 5em;
background-color: red;
}
HTML:
<divclass="items"><divclass="item"></div><divclass="item"></div><divclass="item"></div><divclass="item"></div><divclass="item"></div></div><!-- .items -->
Codepen: https://codepen.io/anon/pen/ZXoqJJ
Solution 3:
flex-container-1 {
display: flex; /* establish flex container */flex-direction: row; /* flex items will align horizontally */justify-content: center; /* center flex items horizontally */align-items: center; /* center flex items vertically *//* for demo purposes only */height: 250px;
width: 400px;
border: 1px solid #777;
background-color: lightgreen;
}
flex-container-1 > flex-item {
height: 90%;
flex: 0045%; /* <flex-grow> <flex-shrink> <flex-basis> */margin-right: 8px; /* a bit of space between the centered items */border: 1px dashed #333;
background-color: yellow;
}
flex-container-2 {
height: 90%;
flex: 0045%;
display: flex; /* flex item is now also flex container */flex-direction: column; /* items will stack vertically */justify-content: space-between; /* align items vertically */
}
flex-container-2 > flex-item {
flex: 0022%;
border: 1px dashed #333;
background-color: yellow;
}
<flex-container-1><!-- main container --><flex-item></flex-item><!-- flex item #1 (first column) --><flex-container-2><!-- flex item #2 / nested flex container (second column) --><flex-item></flex-item><flex-item></flex-item><flex-item></flex-item><flex-item></flex-item></flex-container-2><!-- close nested container --></flex-container-1><!-- close main container -->
Post a Comment for "Flex Container With Two Columns; Second Column Has Four Rows"