Bootstrap / Django: recommended way to render two cards side-by-side (with responsiveness)

Viewed 235

I have the following index.html page that leverages Django templates:

{% extends "layout.html" %}

{% block body %}

    {% for entry in entries %}

    <div class="card h-100 mb-3" style="max-width: 540px;">
        <a href="{% url 'entry' entry.id %}">
            <img class="card-img-top" src="{{ entry.image.url }}" alt="{{ entry.title }}">
        </a>
            <div class="card-body">
                <h5 class="card-title">{{ entry.title }}</h5>
                <p class="card-text">{{ entry.description }}</p>
     
                {% endif %}

            </div>
    </div>
        
    {% empty %}
        No content found.
    {% endfor %}
 
{% endblock %}

Currently, the Bootstrap cards render in a single column (one on top of the other), and they're left-justified.

What is the best way to render cards such that there are two cards side-by-side (and responsive to become a single column if the screen becomes narrow)?

Thanks in advance!

1 Answers

I am not familiar with Django but I would recommend putting those cards in a bootstrap container, which has columns and rows. See the documentation below:

https://getbootstrap.com/docs/4.0/layout/grid/

You can put classes to adjust the breakpoints. I would use boot strap classes like w-100 and vh-100 if you want those cards to take up all screen space after you hit your desired breakpoint.

I made a quick example for you to test on code pen:

<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
    <div class="container-fluid bg-danger">
      <div class="row bg-warning">
        <div class="col-6 col-sm-12">
          <div class="card">
            <img class="card-img-top" src="..." alt="Card image cap">
            <div class="card-body">
              <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
             </div>
          </div>
        </div>
        
        <div class="col-6 col-sm-12">
           <div class="card">
              <img class="card-img-top" src="..." alt="Card image cap">
              <div class="card-body">
                <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
               </div>
            </div>
        </div>
        
      </div>
                  
    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  </body>
</html>
<div class="container-fluid bg-danger">
</div>

Best of luck!

Related