skip to Main Content

Can I make this structure with flexbox?

enter image description here

I give an example below with grid, but I want make this with flexbox.

.container {
    display: grid;
    grid-template-columns: auto auto auto;
    gap: 10px;
}
.grid-item {
    background-color: aquamarine;
}
.small {
    height: 200px;
}
.big {
    grid-row: span 10;
}
<div class="container">
    <div class="grid-item small">Cell 1</div>
    <div class="grid-item small">Cell 2</div>
    <div class="grid-item big">Cell 3</div>
    <div class="grid-item small">Cell 4</div>
    <div class="grid-item small">Cell 5</div>
    <div class="grid-item small">Cell 6</div>
    <div class="grid-item small">Cell 7</div>
    <div class="grid-item small">Cell 8</div>
    <div class="grid-item small">Cell 9</div>
    <div class="grid-item small">Cell 10</div>
    <div class="grid-item small">Cell 11</div>
    <div class="grid-item small">Cell 12</div>
</div>

What I want to achieve with flexbox:

width: 100%;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;

and

<div class="item">1</div>
<div class="item">2</div>
<div class="item big">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>

This is challenging because

  1. is with display flex
  2. in one row

2

Answers


  1. * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    /* debug */
    body:before {
      content: '';
      inset: 0 auto 0 calc(50% - 1px);
      border-right: dashed 2px red;
      position: fixed;
    }
    
    .container {
      display: flex;
      flex-wrap: wrap;
      position: relative;
      gap: 8px;
      padding-right: calc(50% + calc(var(--gap) / 2));
      --gap: 8px;
    }
    
    .grid-item {
      width: calc(50% - calc(var(--gap) / 2));
      display: grid;
      place-items: center;
      background-color: coral;
    }
    
    .small {
      height: 200px;
    }
    
    .big {
      position: absolute;
      inset:0 0 0 calc(50% + var(--gap) / 2);
    }
    <div class="container">
      <div class="grid-item small">Cell 1</div>
      <div class="grid-item small">Cell 2</div>
      <div class="grid-item big">Cell 3</div>
      <div class="grid-item small">Cell 4</div>
      <div class="grid-item small">Cell 5</div>
      <div class="grid-item small">Cell 6</div>
      <div class="grid-item small">Cell 7</div>
      <div class="grid-item small">Cell 8</div>
      <div class="grid-item small">Cell 9</div>
      <div class="grid-item small">Cell 10</div>
      <div class="grid-item small">Cell 11</div>
      <div class="grid-item small">Cell 12</div>
    </div>
    Login or Signup to reply.
  2. So far I know, this is the only way to do it using Flexbox as it is one-dimensional.

    *{
        margin: 0;
        padding: 0;
    }
    
    .flex-container{
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
    }
    
    .flex-inner-container {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
      flex: 1 0 calc(50% - 10px);
    }   
    
    .flex-inner-container .item {
      background-color: aquamarine;
      flex: 1 0 calc(50% - 10px);
      height: 200px;
    }
    
    .big {
        flex: 1 0 calc(50% - 10px);
        background-color: aquamarine;
    }
    <div class="flex-container">
        <div class="flex-inner-container">
            <div class="item">1</div>
            <div class="item">2</div>        
            <div class="item">4</div>
            <div class="item">5</div>
            <div class="item">6</div>
            <div class="item">7</div>
        </div>
    
        <div class="item big">3</div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search