0

I'm using a card group of bootstrap, however at 576px there is a switch in the layout that I don't like (because all the nine cards are on the same line and are too little) thus I inserted min-width:250px to each card, but in this way the odd cards (the last cards) span all the row, instead I would that cards was all the same size. In the code below the problem (resize the browser to see the problem at some browser sizes). This behavior is due to the flex property of bootstrap (flex-grow exactly)

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <meta
         name="viewport"
         content="width=device-width, initial-scale=1, shrink-to-fit=no"
      />
      <link
         rel="stylesheet"
         href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
         integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
         crossorigin="anonymous"
      />
      <title></title>
      <style>
  

           .card {
            min-width: 250px;
         } 
      </style>
   </head>

   <body>
      <div class="card-group m-sm-2">
         <div class="card m-2">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This content is a little bit
                  longer.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>
         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This card has supporting text below as a natural lead-in to
                  additional content.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>
         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This card has even longer
                  content than the first to show that equal height action.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>

         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This card has even longer
                  content than the first to show that equal height action.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>
         <div class="card m-2">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This content is a little bit
                  longer.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>
         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This card has supporting text below as a natural lead-in to
                  additional content.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>
         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This card has even longer
                  content than the first to show that equal height action.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>

         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This card has even longer
                  content than the first to show that equal height action.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>

         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This card has even longer
                  content than the first to show that equal height action.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>
      </div>

      <!-- Optional JavaScript -->
      <!-- jQuery first, then Popper.js, then Bootstrap JS -->
      <script
         src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
         integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
         crossorigin="anonymous"
      ></script>
      <script
         src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
         integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
         crossorigin="anonymous"
      ></script>
      <script
         src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
         integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
         crossorigin="anonymous"
      ></script>
   </body>
</html>

To fix this problem I customized my card like this:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <meta
         name="viewport"
         content="width=device-width, initial-scale=1, shrink-to-fit=no"
      />
      <link
         rel="stylesheet"
         href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
         integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
         crossorigin="anonymous"
      />
      <title></title>
      <style>
         @media (min-width: 450px) {
            .card-group > .card {
               flex: 0 0 calc(50% - 16px);
            }

            .card-group {
               flex-direction: row;
               flex-wrap: wrap;
            }
         }

         @media (min-width: 800px) {
            .card-group > .card {
               flex: 0 0 calc(25% - 16px);
            }
         }

         /*   .card {
            min-width: 250px;
         } */
      </style>
   </head>

   <body>
      <div class="card-group m-sm-2">
         <div class="card m-2">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This content is a little bit
                  longer.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>
         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This card has supporting text below as a natural lead-in to
                  additional content.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>
         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This card has even longer
                  content than the first to show that equal height action.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>

         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This card has even longer
                  content than the first to show that equal height action.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>
         <div class="card m-2">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This content is a little bit
                  longer.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>
         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This card has supporting text below as a natural lead-in to
                  additional content.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>
         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This card has even longer
                  content than the first to show that equal height action.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>

         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This card has even longer
                  content than the first to show that equal height action.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>

         <div class="card m-2 border-left">
            <img
               src="https://picsum.photos/400/400"
               class="card-img-top"
               alt="..."
            />
            <div class="card-body">
               <h5 class="card-title">Card title</h5>
               <p class="card-text">
                  This is a wider card with supporting text below as a natural
                  lead-in to additional content. This card has even longer
                  content than the first to show that equal height action.
               </p>
            </div>
            <div class="card-footer">
               <small class="text-muted">Last updated 3 mins ago</small>
            </div>
         </div>
      </div>

      <!-- Optional JavaScript -->
      <!-- jQuery first, then Popper.js, then Bootstrap JS -->
      <script
         src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
         integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
         crossorigin="anonymous"
      ></script>
      <script
         src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
         integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
         crossorigin="anonymous"
      ></script>
      <script
         src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
         integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
         crossorigin="anonymous"
      ></script>
   </body>
</html>

But my solution seems too much convoluted and not in bootstrap style. Is there a more smart way?

Nick
  • 1,439
  • 2
  • 15
  • 28

0 Answers0