When should I nest flexboxes?



When should I nest flexboxes?


We can use nested flexboxes when we require the content of one of our flex items to be easily aligned, ordered, spaced and/or scaled in a horizontal or vertical direction.

Imagine a hypothetical layout where we have a flex container (<section> element) with three flex items (<div> elements), where the last flex item contains some buttons that we want to align and space easily. In this situation we’ll want to use a nested flexbox where our third flex item is also a flex container:

<!DOCTYPE html>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>My Site - Flexbox</title>
  <div class="with-content">
    <button>btn 1</button>
    <button>btn 2</button>
    <button>btn 3</button>


section {
  display: flex;
  border: 1px solid black;
  justify-content: space-between;
  flex-wrap: wrap;

div {
  width: 300px;
  height: 200px;
  border: 1px solid red;

.with-content {
  display: flex; /*will make our third flex item, within our `<section>` element, a flex container*/
  align-items: center;
  justify-content: space-around;
  flex-wrap: wrap;