Search code examples
htmlcssautocompletecss-grid

make grid items automatically fill empty spaces


I have the following CSS grid system that has 4 kinds of different cell sizes: small, wide, tall and large. In a desktop version I've made them in a puzzle like shape that looks good but to make it mobile friendly I need the cells with a class small to automatically fill up the blank spaces that appear.

Here's the css:

.portfolio-holder {
    margin: 66px auto;
    display: flex;
    flex-wrap: wrap;
}

.portfolio {
    margin: auto;
    display: grid;
    height: 100%;
    grid-template-rows: repeat(13, 200px);
    grid-template-columns: repeat(4, 200px);
    grid-gap: 10px;
    padding: 24px;
    grid-auto-rows: minmax(100px, auto);
}

.portfolio_hat {
    background: #fd3a1e;
    overflow: hidden;
    color: black;
    font-weight: 14px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.5em;
    background-size: cover;
    background-position: top left;
    position: relative;
}

.portfolio_hat.tall {
    grid-row: span 2;
    grid-column: span 1;
}

.portfolio_hat.wide {
    grid-row: span 1;
    grid-column: span 2;
}

.portfolio_hat.small {
    grid-row: span 1;
    grid-column: span 1;
}

.portfolio_hat.tall {
    grid-row: span 2;
    grid-column: span 2;
}

@media (max-width:860px) {
    .portfolio {
        grid-template-rows: repeat(26, 40vw);
        grid-template-columns: repeat(2, 40vw);
    }
}

and the HTML looks something like this:

<section class="portfolio-holder">
    <div id="port" class="portfolio">
        <div href="#" class="portfolio_hat tall"></div>
        <div href="#" class="portfolio_hat small"></div>
        <div href="#" class="portfolio_hat wide"></div>
        <div href="#" class="portfolio_hat large"></div>
        <!-- divs like the four above are generated from MYSQL DATABASE and there's a lot of them in a chaotic order -->
    </div>
</section>

I want a way to make sure that no matter the number of columns in the grid I won't get any empty spaces.

Thank You


Solution

  • Take a look at https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow

    The grid-auto-flow CSS property controls how the auto-placement algorithm works, specifying exactly how auto-placed items get flowed into the grid.

    .portfolio-holder {
        margin: 66px auto;
        display: flex;
        flex-wrap: wrap;
    }
    
    .portfolio {
        margin: auto;
        display: grid;
        height: 100%;
        grid-template-rows: repeat(13, 200px);
        grid-template-columns: repeat(4, 200px);
        grid-gap: 10px;
        padding: 24px;
        grid-auto-rows: minmax(100px, auto);
    grid-auto-flow: row dense;/*  here your CSS update */
    }
    .portfolio_hat {
      background: #fd3a1e;
      overflow: hidden;
      color: black;
      font-weight: 14px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 1.5em;
      background-size: cover;
      background-position: top left;
      position: relative;
    }
    
    .portfolio_hat.tall {
      grid-row: span 2;
      grid-column: span 1;
    }
    
    .portfolio_hat.wide {
      grid-row: span 1;
      grid-column: span 2;
    }
    
    .portfolio_hat.small {
      grid-row: span 1;
      grid-column: span 1;
    }
    
    .portfolio_hat.tall {
      grid-row: span 2;
      grid-column: span 2;
    }
    
    @media (max-width:860px) {
    .portfolio {
        grid-template-rows: repeat(26, 40vw);
        grid-template-columns: repeat(2, 40vw);
       }
    }
    div {/* see them for demo */
      border: solid 1px;
      padding: 1em;
      margin: 2px;
    }
    <section class="portfolio-holder">
        <div id="port" class="portfolio">
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <div href="#" class="portfolio_hat tall"></div>
            <div href="#" class="portfolio_hat small"></div>
            <div href="#" class="portfolio_hat wide"></div>
            <div href="#" class="portfolio_hat large"></div>
            <!-- divs like the four above are generated from MYSQL DATABASE and there's a lot of them in a chaotic order -->
        </div>
    </section>