What could be causing the bootstrap card alignment issue when trying to display them side by side

I am currently developing a Flask project where I'm showcasing user information retrieved from the database within bootstrap cards. Utilizing jinja2 to iterate through an object statuses

{% extends "base.html" %}

{% block content %}
  {% if statuses %}
      **{% for status in statuses %}**
          <div class="container">
              <div class="row mt-4">
                <div class="col-sm-3">
                  <div class="card">
                    <img src="{{ url_for('static', filename='img/no_profile.jpg')}}" class="card-img-top" alt="...">
                    <div class="card-body">
                    <h5 class="card-title">{{ status.username }}</h5>
                    <p class="card-text">{{ status.biography }}</p>
                    <a href="#" class="btn btn-primary">{{ status.interests }}</a></div>
                  </div>
                </div>
              </div>
            </div>
      {% endfor %}
  {% else %}
      <h2>There are no statuses</h2>
  {% endif %}
{% endblock %}

Yet, my expectation was for the cards to align side by side instead of stacking up for each entry fetched from the database view the snapshot here

Answer №1

It appears that your {% forloop %} was placed prematurely. Consider using the following structure:

{% extends "base.html" %}

{% block content %}
  {% if statuses %}
          <div class="container">
              <div class="row mt-4">
          **{% for status in statuses %}**
                <div class="col-sm-3">
                  <div class="card">
                    <img src="{{ url_for('static', filename='img/no_profile.jpg')}}" class="card-img-top" alt="...">
                    <div class="card-body">
                    <h5 class="card-title">{{ status.username }}</h5>
                    <p class="card-text">{{ status.biography }}</p>
                    <a href="#" class="btn btn-primary">{{ status.interests }}</a></div>
                  </div>
                </div>
          {% endfor %}
              </div>
            </div>
  {% else %}
      <h2>No statuses found</h2>
  {% endif %}
{% endblock %}

Answer №2

Consider relocating .container and .row from your for loop output to the if output section.

{% extends "base.html" %}

{% block content %}
    {% if statuses %}
        <div class="container">
            <div class="row mt-4">
                **{% for status in statuses %}**
                    <div class="col-sm-3">
                        <div class="card">
                            <img src="{{ url_for('static', filename='img/no_profile.jpg')}}" class="card-img-top" alt="...">
                            <div class="card-body">
                                <h5 class="card-title">{{ status.username }}</h5>
                                <p class="card-text">{{ status.biography }}</p>
                                <a href="#" class="btn btn-primary">{{ status.interests }}</a>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>
        </div>
    {% else %}
        <h2>There are no statuses</h2>
    {% endif %}
{% endblock %}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to share dynamic content from a DIV or SPAN element on WhatsApp using jQuery?

I’ve been attempting to share the text content of a specific DIV on WhatsApp, but so far I haven't been successful. My goal is to only share a portion of the document (specifically the contents of showques). Below is the code snippet I've been ...

What is the solution to having a div move along with window resizing without displacing adjacent divs?

After much effort, I still can't seem to get this working correctly. I've been playing around with a section named "RightExtra" and a div inside it called "RightExtraContent". My goal is to allow these two divs to move freely when the window is ...

Changing the main domain of links with a specific class attribute during an onmousedown event - is it possible?

We are facing a situation where we have numerous webpages on our blog that contain links from one domain (domain1.com) to another domain (domain2.com). In order to avoid manual changes, we are attempting to achieve this without altering the link (href). Th ...

AngularJS Filter from "start to finish" in values

I have been searching everywhere for a solution to this problem, but haven't found one yet. Any help would be greatly appreciated. Angular: var app = angular.module('app', []); app.controller('theController', ['$scope', ...

Tips for preventing the line through effect on a span element when it is checked

I am working on a project that involves a list of items labeled as li. For example: <ul class="list"> <li class="checked">Groceries <span>&#215;</span></li> <li>Medicine <span>& ...

Chart showing a Google Timeline: Allow for each bar to be colored differently when there are multiple bars on the same line

It appears that the Google Timeline charts have a feature to color individual blocks on the timeline as indicated in the documentation at However, there seems to be an issue when two bars overlap on the same line, which is evident in this fiddle: http://j ...

Enhancing user experience with CSS dropdown menu animations

I've been working on adding a dropdown transition to my menu, but I can't seem to get it right. Can anyone point out where I'm going wrong or what needs to be added for the transition effect on the dropdown menu? Here is the CSS and HTML cod ...

One way to retrieve form data in a controller when using serialization in an AJAX request is by accessing the

When using (#myForm).serialaize() in an ajax call for form submission, how can the form data be accessed in the controller? What does the representation of form data look like in the controller? $('#myForm').on('submit', function(e){ ...

Show small information box when hovering over custom toggle button

I have developed a unique custom toggle switch feature When I hover my mouse over the switch, it should display a tooltip message as YES if the switch is in the ON position and NO if it's in the OFF position. Is there a way to implement this functio ...

Can you help me with sorting asynchronous line points for KineticJS?

For the past couple of days, I've been grappling with a peculiar issue that I found difficult to articulate in the title. The challenge I'm facing involves a KineticJs Line, which contains an array of points (line.attrs.points) represented as ob ...

Overlaying text on several images

My challenge is centering text over multiple images using a combination of Bootstrap and CSS. While most online resources suggest using position: absolute; to achieve this, I have only been successful in centering the text in the middle of the screen rathe ...

Add a sublime project with a specific directory path

Currently, I am using Sublime Text 2 for a project that is structured as follows: public vendors a.css b.css myfile.less myfile.css (compiled) I am facing an issue where I need to exclude the css files from the main 'public' folder (specifi ...

Just dipping my toes into Django/Python and feeling completely bewildered by why my webpages won't connect

I'm having trouble with my webpage links. It seems like the file paths are incorrect. I've attempted to link three pages: The homepage The 'artists' icon on the top right of the homepage, linking to 'registerprofessional.html&apo ...

The Hamburger Menu in the Bootstrap Navbar is failing to collapse

My website uses Bootstrap v5.3 and I've added a navbar, but the hamburger icon is not collapsing on mobile devices when clicked. Can someone please review my code to see if there are any issues? I can't figure out what's wrong. <!DOCTY ...

Having issues when dynamically adding options to a multiselect dropdown

I'm working on dynamically adding options to a multiselect using AJAX: <select size='2' name="CraftCode" id=@ccrf class="form-control js-select manualentrydd" ></select> $.ajax({ type: "GET", url: "GetCraftCodes", data: ...

The parameters of a submitted form in JQuery are constantly changing

I attempted to pass a value to another page through the URL. I cannot hardcode it directly in the HTML because I need to gather information from multiple steps first. Therefore, I must modify the form action before it is submitted. Below is the form struc ...

Adjust the alignment of text within the <select> tag to the right in Safari browser

Is there a way to align text right in a select option tag? <select style="direction:rtl;text-align:right"> <option value="" selected="">همه</option> <option value="1">راهبر سيستم</option> <option v ...

The area surrounding inline content, both above and below

Can you explain the space above and below inline content? This phenomenon can be seen here: http://jsbin.com/vertical-spacing-weirdness/ If you look closely, there is a white area between the div/table and the span even though the span does not have ...

Personalizing Web Push Alerts (Google Chrome)

I successfully implemented a web push notification for Google Chrome using Google Project and Service Worker. One thing I'm curious about is how to customize or style the push notification. The plain message box doesn't quite cut it for me – I ...

Why is it that Lotus Notes is unable to render this HTML code properly?

I am having trouble with an email that displays properly on every platform except Lotus Notes. Can anyone provide insight as to why this email is not rendering correctly in Notes? Here is a screenshot: img (please note the images are for demonstration pu ...