Using Bootstrap to horizontally align cards in a single row

When rendering the 4 items in a row using a for loop, I encounter an issue where the first line renders 4 items correctly but subsequent items are rendered on separate lines.

code

<div class="card-group">

    {% for item in wt %}

          <div class="card my-3 text-white bg-dark mb-3" style="width: 18rem;">

            <img src="/media/{{item.thumbnail}}" class="card-img-top" alt="...">
            <div class="card-body">
              <h5 class="card-title">{{item.product_name}}</h5>
              <p class="card-text">{{item.thumbnail_desc}}</p>

              <a href="blog_detail/{{item.post_id}}" class="btn btn-primary">View Product</a>


            </div>
          </div>

    {% if forloop.counter|divisibleby:4 %}
    </div>
    {% endif %}

   {% endfor %}

In this implementation, I am utilizing both Bootstrap and Django frameworks. Despite trying to use the "row" class, it does not resolve the issue entirely.

Answer №1

Automatically arranging 4 items in a row without the need for extra coding:


{% for item in wt %}
    <div class="card my-3 text-white bg-dark mb-3" style="width: 18rem;">
        <img src="/media/{{item.thumbnail}}" class="card-img-top" alt="...">
        <div class="card-body">
            <h5 class="card-title">{{item.product_name}}</h5>
            <p class="card-text">{{item.thumbnail_desc}}</p>
            <a href="blog_detail/{{item.post_id}}" class="btn btn-primary">View Product</a>
        </div>
    </div> 
{% endfor %}

If this doesn't work, consider using the "col-md-3" class instead of "my-3".

Please let me know if this solves the issue.

Answer №2

By adjusting the size of the card and incorporating a counter for the forloop, I was able to resolve the issue where the card was not fitting inside the container.

<h2 class="my-4 text-white bg-dark">New Arrivals</h2>
            <div class="row">
         {% for item in wt %}

          <div class="card text-white bg-dark mb-3" style="width: 10rem;">

            <img src="/media/{{item.thumbnail}}" class="card-img-top" alt="...">
            <div class="card-body">
              <h5 class="card-title">{{item.product_name}}</h5>
              <p class="card-text">{{item.thumbnail_desc}}</p>

              <a href="detail/{{item.post_id}}" class="btn btn-primary">View Product</a>


            </div>
          </div>
                 {% if forloop.counter|divisibleby:4 %}
    </div>
        <div class="row">
    {% endif %}

   {% endfor %}

        </div>

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

Encounter an unforeseen token ), within the webpack configuration

I'm currently in the process of developing a web application and I've made the decision to integrate webpack to enable seamless running of React and Django simultaneously. However, upon executing "webpack --config webpack.config.js", I encounter ...

Adjusting the size of HighCharts HighMaps with a custom function

Simple query here (I believe) I have a DIV with the class "container" acting as my Map. The Highcharts plugin I'm using is responsive, but it only resizes when the window does. I'm looking to manually trigger a resize without adjusting my windo ...

Looking for guidance on JavaScript - Implementing a different font family for each div element

I need help with customizing the font family for each individual post on my website. Currently, I am able to assign a cursive font to all posts within a class, but I would like each post to have its own unique font. Since I am doing this on load, I am fa ...

Exploring Django's Media File Configuration and Organization

In my Django 1.4 project, I have set up the MEDIA_ROOT and STATIC_ROOT settings as follows: SITE_ROOT = os.path.dirname(os.path.realpath(__file__)) MEDIA_ROOT = os.path.join(SITE_ROOT, 'media') STATIC_ROOT = os.path.join(SITE_ROOT, 'stat ...

Leverage the power of personalized SCSS styles in combination with Bootstrap-Vue, Webpack, and

I have been working on an application using VueJS with Bootstrap-Vue and I encountered a challenge while trying to import a SCSS file to customize the Bootstrap variables and add some custom styles. Here are the steps I have taken so far: Installed node ...

Use CSS3 to hide the <li> elements initially and make them appear when the <li> is clicked on

Click here How can I hide the "subline" line from the list and make it visible when the user clicks on the "sub" line, pushing the other lines down? I'm hoping for a solution that doesn't involve using Js or JQuery. Thank you in advance! ...

What is the best way to include a background image in a div within a React component?

I'm currently following a tutorial on creating an RPG game using React. The tutorial can be found at this link. I am trying to replicate the presenter's code by typing it out myself. However, I'm running into an issue when attempting to add ...

Container will keep items from spreading out

Experiencing an issue with the card layout in my weather dashboard app. The cards within a container are not spreading out evenly to match the 100% width of the header. I want the container with the 5 cards to be the same width as the header and wrap prope ...

Show concealed elements above everything else

I've encountered an issue with my custom dropdown list as it displaces other elements when it appears. I want it to overlay on top of everything else, similar to the default select behavior. Despite trying to set position: relative; and z-index:100;, ...

What is the deal with CSS relative magnification?

I am currently utilizing a PHP styleswitcher along with alternate stylesheets in an attempt to replicate the functionality of browser zoom (done using keyboard shortcuts cmd-plus or ctrl-plus). At the moment, when clicking on the "zoom in" graphic, it is ...

React modal image showing a misaligned image upon clicking

I recently integrated the react-modal-image library into my project to display images in a modal when clicked. However, I encountered an issue where the displayed image is off center with most of it appearing offscreen. I'm unsure what is causing this ...

The list-image is nowhere to be found

Seeking assistance with my LI style CSS. Visit this link for reference I'm having trouble getting an image to float on the right side of my sidebar links, and my content is overlapping with the sidebar... .sidebar-menu ul { font : 14px font-fam ...

What is the reason behind a stationary lightbox sticking to the top of the webpage rather than the top of the viewport?

When you visit this page and scroll down a little before clicking on one of the art pieces, have you noticed that the "fixed" lightbox seems to be attaching itself to the top of the page instead of the viewport? I'm looking for ways to make it attach ...

Setting a default currency value (either in dollars or euros) in an input field

Is there a way to automatically insert a currency symbol (€, $) in a text field by default when entering a number? Are there any default values for this feature? Thank you Here is an example ...

background with alternating stripes to decorate the border of a div

Is it possible to give a striped effect to a div element's border using jQuery? Consider the following code snippet: <style type="text/css"> #panel { padding: 50px text-align: center; background-color: #e5eecc; bord ...

In what way is the background-color of WindowInfoBackground utilized?

I attempted to implement background-color : WindowInfoBackground;. The property background-color : WindowInfoBackground; allows for the assignment of the system color to the background-color attribute. Despite using this particular value, the backgroun ...

Can IE Conditional Comments have a negative impact on page speed?

Debates between "CSS hacks" and "Conditional Comments" have become a regular occurrence for me lately, so I wanted to hear the thoughts of the Stack Overflow community on this topic. <!--[if IE]> <link type="text/css" rel="stylesheet" href="ie-s ...

Move the aircraft across the display in a lively animation

I need help creating an animation where a plane flies across the screen, exits from the left side, and re-enters from the right side in a continuous loop. How can I achieve this effect to make the plane fly endlessly across the screen? Here is my code: ...

Creating a responsive design for a cropped image using CSS

Hi there! I am using a cropped image as the background for the top of my webpage, with a fixed size defined in pixels for the containing div. The issue arises when I resize the browser window, causing the image to cover most of the page instead of just the ...

Creating dynamic grids in React.js by utilizing HTML

Currently, I am tackling one of the projects on FCC which is the Game of Life. Prior to diving in, my focus is on figuring out how to render a grid on the page. I aim to modify the table dimensions while ensuring it fits neatly within its container. The ...