Flexbox, nested grid/child

Viewed 203

Desired output

I'm trying to achieve a nested Flexbox layout.

I want the children from second and third columns to take the full size on the parent.

It works when depth = 1 for the first column but fails when I add a Flex container.

My thoughts on the error

I'm forced to combine flex: 1 and display: flex in order to use the same class regardless of whether it is used as a container or item.

Any idea ?

Actual code

.grid-stretch {
  display: flex;
  align-items: stretch;
  background-color: yellow; /* to track errors */
}

.child-centered {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: green; /* to track errors */
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  overflow-wrap: break-work;
}

.root {
  max-width: 1200px;
  height: 100%;
}

header {
  min-height: 80px;
  /* set a solid border inside the header and not on its edge */
  box-sizing: border-box;
  border-bottom: 3px solid black;
}

/* depth 1 */
header nav:nth-child(1) {
  flex: 3;
  background-color: blue;
}

header nav:nth-child(2) {
  flex: 5;
  background-color: white;
}

header nav:nth-child(3) {
  flex: 2;
  background-color: red;
}

/* depth 2 */
header nav:nth-child(2) div {
  background-color: orange;
}

header nav:nth-child(3) div div {
  background-color: orange;
}
<!DOCTYPE html>
<html>

<body>
  <div class='root'>
    <header class='grid-stretch'>

      <nav class='child-centered'>
        <a>link</a>
      </nav>

      <nav class='child-centered'>
        <div class='grid-stretch'>
          <div class='child-centered'>
            <a>link</a>
            <a>link</a>
            <a>link</a>
          </div>
        </div>
      </nav>

      <nav class='child-centered'>
        <div class='grid-stretch'>
          <div class='child-centered'>
            <a>link</a>
            <a>link</a>
            <a>link</a>
          </div>
        </div>
      </nav>
    </header>

  </div>
</body>

</html>

1 Answers

Update your code like below:

.grid-stretch {
  display: flex;
  align-items: stretch;
  background-color: yellow; /* to track errors */
}

.child-centered  {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align:center;
  background-color: green; /* to track errors */
}
/* added */
.flex {
  display: flex;
}
.flex  *{
  flex-grow:1;
}
/**/
html,
body {
  height: 100%;
}

body {
  margin: 0;
  overflow-wrap: break-work;
}

.root {
  max-width: 1200px;
  height: 100%;
}

header {
  min-height: 80px;
  /* set a solid border inside the header and not on its edge */
  box-sizing: border-box;
  border-bottom: 3px solid black;
}

/* depth 1 */
header nav:nth-child(1) {
  flex: 3;
  background-color: blue;
}

header nav:nth-child(2) {
  flex: 5;
  background-color: white;
}

header nav:nth-child(3) {
  flex: 2;
  background-color: red;
}

/* depth 2 */
header nav:nth-child(2) div {
  background-color: orange;
}

header nav:nth-child(3) div div {
  background-color: orange;
}
<!DOCTYPE html>
<html>

<body>
  <div class='root'>
    <header class='grid-stretch'>

      <nav class="flex">
        <a class="child-centered">link</a>
      </nav>

      <nav class="flex">
        <div class='grid-stretch'>
          <div class='child-centered'>
            <a>link</a>
            <a>link</a>
            <a>link</a>
          </div>
        </div>
      </nav>

      <nav class="flex">
        <div class='grid-stretch'>
          <div class='child-centered'>
            <a>link</a>
            <a>link</a>
            <a>link</a>
          </div>
        </div>
      </nav>
    </header>

  </div>
</body>

</html>

Related