The key to this layout is applying equal heights to the main flex container.
Then create the flex items of nested flex containers that can center the content of flex items.
Therefore, the upper level creates equal height. The second level does centering.
(For more details see the note below).
Here is an example based on your code structure:
body { height: 300px; color: white; } flex-container { display: flex; flex-direction: row; align-items: stretch; height: 100%; } flex-item { display: flex; flex-direction: column; justify-content: center; align-items: center; } flex-item:first-child { flex: 3; background-color: #a333c8; } flex-item:last-child { flex: 1; background-color: #db2828; }
<flex-container> <flex-item> <p>Text Text Text</p> <p>Text Text Text</p> <p>Text Text Text</p> <p>Text Text Text</p> </flex-item> <flex-item> <div>Forward</div> </flex-item> </flex-container>
People sometimes consider the element of flexibility, and its contents - one of the elements. This is not true.
The HTML structure of a flexible container has three levels:
Therefore, the content inside the element does not represent the element; it is a separate element.
If the content inside the element is text, it becomes anonymous.
From the flexbox specification:
4. Flex items
Each child stream of a flexible container becomes a flexible element, and each continuous run of text that is directly contained within the flex container is wrapped in an anonymous flexibility element.
That is why in the above solution, the flexible element becomes a flexible container. This allows you to use flex properties for children of a flexible element (content).
source share