1

I have to create a 5 column grid in Bootstrap 4 to show over 100 images. The code below works ONLY if I have 5 items. How can I go about it?

PS: It must change 2 column grid on mobile screen.

I understand there's a lot of duplicate with correctly marked answer but there's no actual solution to it when I have more than 5 column.

JSFiddle Demo: https://jsfiddle.net/fwg2os0h/

<div class="row">
   <div class="col">
      1
   </div>
   <div class="col">
      2
   </div>
   <div class="col">
      3
   </div>
   <div class="col">
      4
   </div>
   <div class="col">
      5
   </div>
   <div class="col">
      6
   </div>
   <div class="col">
      7
   </div>
   <div class="col">
      8
   </div>
   <div class="col">
      9
   </div>
   <div class="col">
      10
   </div>
</div>
Elaine Byene
  • 3,868
  • 12
  • 50
  • 96

3 Answers3

2

Maybe, you can set the min-width to 20% and that should fix it. At any point of time, this will have a minimum width of 20%, which will force it to show only 5 a row.

.my-col {
  min-width: 20%;
}

And for the mobile support, please use a custom media query to set min-width: 50%.

@media screen and (max-width: 480px) {
  .my-col {
    min-width: 50%;
  }
}

Demos

Here's how it looks when it's normal.

.my-col {
  min-width: 20%;
}
@media screen and (max-width: 480px) {
  .my-col {
    min-width: 50%;
  }
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
  <div class="col my-col">
    1
  </div>
  <div class="col my-col">
    2
  </div>
  <div class="col my-col">
    3
  </div>
  <div class="col my-col">
    4
  </div>
  <div class="col my-col">
    5
  </div>
  <div class="col my-col">
    6
  </div>
  <div class="col my-col">
    7
  </div>
  <div class="col my-col">
    8
  </div>
  <div class="col my-col">
    9
  </div>
  <div class="col my-col">
    10
  </div>
</div>

When you have less than 5, it will automatically show them justified.

.my-col {
  min-width: 20%;
}
@media screen and (max-width: 480px) {
  .my-col {
    min-width: 50%;
  }
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
  <div class="col my-col">
    1
  </div>
  <div class="col my-col">
    2
  </div>
  <div class="col my-col">
    3
  </div>
  <div class="col my-col">
    4
  </div>
</div>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
2

I think it would be easiest to add a media query based column width class for 20%...

@media (min-width: 768px){
    .col-sm-five {
        flex: 0 0 20%;
        max-width: 20%;
    }
}


<div class="container">
    <div class="row">
        <div class="col-6 col-sm-five">
            1
        </div>
        <div class="col-6 col-sm-five">
            2
        </div>
        <div class="col-6 col-sm-five">
            3
        </div>
         ..
     </div> 
</div>

https://www.codeply.com/go/RRp8uUraZe

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
0

Please check out Bootstrap's docs about grid systems.

Play around with the numbers to get what you want. col-md- is for tablets or bigger, col-sm- for mobile.

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
  <div class="col-md-2 col-sm-6">
    1
  </div>
  <div class="col-md-2 col-sm-6">
    2
  </div>
  <div class="col-md-2 col-sm-6">
    3
  </div>
  <div class="col-md-2 col-sm-6">
    4
  </div>
  <div class="col-md-2 col-sm-6">
    5
  </div>
  <div class="col-md-2 col-sm-6">
    6
  </div>
  <div class="col-md-2 col-sm-6">
    7
  </div>
  <div class="col-md-2 col-sm-6">
    8
  </div>
  <div class="col-md-2 col-sm-6">
    9
  </div>
  <div class="col-md-2 col-sm-6">
    10
  </div>
</div>
Naseki
  • 59
  • 4