Bootstrap 4 cards - Can't get them responsive

I just built a new site from scratch using Bootstrap 4. I created a fluid container, then into that I inserted a Grid Row with 8 columns, and into each of these 8 columns I inserted a card with images.

Everything looked fine until I realized that it isn’t responsive. In fact at xl the cards display at 4 in a row, but at lg the cards all stack vertically as if viewing on a phone.

Try as I might, I can’t get the cards to be responsive no matter how much I play with the col classes.

Can someone help me to get this thing responsive? Basically I’d like the cards 4 across at xl (that’s what they’re doing now), 3 across at lg, and 2 across at md, sm, and xs. Again, right now it’s displaying at 4 across at xl and only 1 across (stacking vertically) at all other sizes.

This is my first time here and hopefully after I type this I can include some code. Much appreciated.

Welcome to the forums, @steambc.

That would certainly help - either that or a link to a live page where we can see the issue.

When you post code on the forums, you need to format it so it will display correctly. You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.

More help on posting code can be found here:
Forum Posting Basics

Hi there steambc,

I would suggest that you do not use “BootStrap”
for this simple basic coding. :unhappy:

Here is an example that I kocked up in five minutes…

Full page:-

https://codepen.io/coothead/full/WNbBqzW

HYML

<!DOCTYPE HTML>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">

<title>Untitled document</title>

<link rel="stylesheet" href="screen.css" media="screen">

</head>
<body>

 <ul>
  <li><img src="https://picsum.photos/id/1/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/2/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/3/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/4/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/5/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/6/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/7/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/8/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/2/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/3/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/4/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/5/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/6/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/7/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/8/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/1/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/3/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/4/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/5/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/6/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/7/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/8/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/1/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/2/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/4/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/5/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/6/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/7/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/8/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/1/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/2/648/400" width="648" height="400" alt=""></li>
  <li><img src="https://picsum.photos/id/3/648/400" width="648" height="400" alt=""></li>
 </ul>

</body>
</html>

CSS

body {
    background-color: #9ff;
    font: normal 1em / 1.5em verdana, helvetica, arial, sans-serif;
 }

ul {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    padding: 0.25em;
    margin: 0;
    border: 1px solid #000;
    list-style: none;
    background-color: #fff;
 }

li {
    flex: 0 0 12%;
    margin: 0.25em 0;
 }

img {
    display: block;
    width: 100%;
    height: auto;
 }

@media ( max-width: 64em ){
li {
    flex: 0 0 24.5%;
  } 
 }

@media ( max-width: 50em ){
li {
    flex: 0 0 49.5%;
  }
 }
 
@media ( max-width: 25em ){
li {
    flex: 0 0 99.5%;
  }
 }  

coothead

4 Likes

You’d need to set the column classes appropriately.

e.g. To go from 4 across down to one across you would use the classes like this.

<div class="col-12 col-sm-6 col-md-4 col-lg-3">

Column classes indicate the number of columns you like which is based on a grid of 12 columns. Therefore -12 spans 12 columns meaning one item per row. sm-6 says span 6 columns (i.e. 2 per row) for the small breakpoint.

Read through the grid documentation again and it will begin to come clear. I had to read it a few times before it clicked :slight_smile:

Here’s a codepen you can fork and play around with and goes from 4 columns down to one column. If you want the smallest to be 2 columns instead of one like the codepen then change the col-12 to col-6.

Yes its much easier in basic css :slight_smile:

3 Likes

Thanks, guys, for the help. I’m dealing with a kidney stone (not my first) and I will dig into all your help and great advice a bit later and report back.

You help is greatly appreciated!

Thanks, TechnoBear. Duly noted.

1 Like

Thanks, PaulOB, your solution worked like a charm. I have a better understanding now how the grid works. I applied your advice to the column and it worked perfectly. I greatly appreciate the info.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.