Bootstrap cards have a fixed width and do not wrap

I've been diving into Django and although I'm fairly new to CSS, I managed to put together a page displaying various products using Bootstrap cards.

https://i.sstatic.net/xme5m.png


It appears that my code should have wrapped these cards properly, but unfortunately, it isn't doing so as expected.

function createProductCards(products) {
    const container = document.querySelector('.card-deck');
  
    products.forEach(product => {
      const cardDiv = document.createElement('div');
      cardDiv.classList.add('card');
  
      const imageTag = document.createElement('img');
      imageTag.setAttribute('src', product.image);
      imageTag.setAttribute('alt', product.name);
      imageTag.classList.add('card-img-top');
      cardDiv.appendChild(imageTag);
      
      const bodyTag = document.createElement('div');
      bodyTag.classList.add('card-body');
      cardDiv.appendChild(bodyTag);
      
      const nameTag = document.createElement('h3');
      nameTag.textContent = product.name;
      nameTag.classList.add('card-title');
      bodyTag.appendChild(nameTag);
      
      
      const descriptionTag = document.createElement('p');
      descriptionTag.textContent = product.description;
      descriptionTag.classList.add('card-text');
      bodyTag.appendChild(descriptionTag);
      
      const genderTag = document.createElement('h6');
      genderTag.textContent = product.gender;
      genderTag.classList.add('card-text');
      bodyTag.appendChild(genderTag);
  
      container.appendChild(cardDiv);
    });
  }
/* Your CSS code for styling */
body {
    display: flex;
    flex-wrap: wrap;
    flex-direction: column;
    min-height: 100vh;
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
}
main {
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    width: 100%;
}
footer {
    background-color: #f8f9fa;
    padding: 20px 0;
    text-align: center;
}

.card-container {
    justify-content: space-between;
    gap: 20px;
    width: 100%;
}

.card-deck {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    padding: 10px;
    background-color: #fff;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    text-align: center;
}

.card {
    flex: 1 0 auto;
    width: 25%;
}

@media screen and (max-width: 768px) {
    .card-deck {
        flex: 1 0 100%;
        max-width: 100%;
    }
}
    
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="main.css">
</head>
<body>
    
    <!-- Header -->

    <main>
        <div class="card-container">
            <div class="card-deck">
                <!-- Product cards will be inserted here dynamically -->
            </div>
        </div>
    </main>

    <!-- Footer -->

    <script>
       // Javascript logic goes here
    </script>
    <script src="home.js"></script>
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e2d213c2b0e7c6077607f">[email protected]</a>/dist/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>


I've also included JavaScript fetching products from the backend and creating the cards on the webpage.

If you have any insights on why the cards aren't wrapping correctly or suggestions on achieving multiple lines with 4 or 5 cards each, please share your thoughts. Thank you in advance...

Answer №1

Your code doesn't seem to display any bootstrap cards, making it difficult to identify where the issue lies. It appears that you are attempting to create a custom flex container instead of utilizing the predefined bootstrap grid. Since you are already using bootstrap, it is advisable to stick with their grid system. For future reference in non-bootstrap projects, you can include the flex-wrap:wrap; property on elements with display:flex. However, in this case, sticking to bootstrap's grid would be more efficient.

By default, anything placed within a bootstrap grid will automatically wrap columns. If this isn't happening, you may not be implementing it correctly. Check out the documentation and examples here: https://getbootstrap.com/docs/5.3/layout/grid/

Additionally, bootstrap cards offer various layout options as well: https://getbootstrap.com/docs/5.3/components/card/#card-layout

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bedcd1d1cacdcaccdfcefe8b908d908f">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">


<div class="row">

  <div class="col-4">
    <div class="card mb-3">
      <div class="card-body">
        <h5 class="card-title">Card title</h5>
        <p class="card-text">Example Card</p>
      </div>
    </div>
  </div>
  
  <div class="col-4">
    <div class="card mb-3">
      <div class="card-body">
        <h5 class="card-title">Card title</h5>
        <p class="card-text">Example Card</p>
      </div>
    </div>
  </div>
  
  <div class="col-4">
    <div class="card mb-3">
      <div class="card-body">
        <h5 class="card-title">Card title</h5>
        <p class="card-text">Example Card</p>
      </div>
    </div>
  </div>
  
  <div class="col-4">
    <div class="card mb-3">
      <div class="card-body">
        <h5 class="card-title">Card title</h5>
        <p class="card-text">Example Card</p>
      </div>
    </div>
  </div>
  
  <div class="col-4">
    <div class="card mb-3">
      <div class="card-body">
        <h5 class="card-title">Card title</h5>
        <p class="card-text">Example Card</p>
      </div>
    </div>
  </div>
  
</div>


EDIT: I see this question is tagged with bootstrap-4. Essentially, the process remains the same. Here are the links to those same pages in the Bootstrap 4 documentation:

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

Mismatch of data types in Google Visualization

I am working with Google Visualization and receiving Unix Epoch timestamps that I need to convert into an array of strings for use in Google Charts. However, I keep encountering an error: Type mismatch. Value 2017-8-25 16:23:54,2017-8-25 16:11:54,... does ...

The fixed div is always positioned in the top corner

My webpage has a 'Book Now' button that remains at the top of the viewport with a 10px gap. However, due to the parent element having a height of 100%, the button scrolls out of view once the viewport surpasses that height. To see an example, cli ...

After fetching an HTML snippet using the $.get() method, Font Awesome icons are no longer visible

Experimenting with creating a seamless 'single-page' experience, I've managed to achieve this by implementing an event listener in jQuery for menu link clicks. This triggers the replacement of the existing content in my main div with an HTML ...

Is it feasible to dynamically change SCSS variables using React props? Let's explore this possibility

I've been struggling for the past 2 days to find a solution to my problem. Unfortunately, I haven't found any solutions yet. I'm facing issues with color states in my project. When I select a color from the color picker, I want to update tha ...

Display targeted information upon clicking a designated division using JavaScript or jQuery

I'm working on a FAQ page and I want to display a specific answer when a particular question is clicked. If you'd like to see it in action, here's a fiddle: http://jsfiddle.net/hdr6shy9/ Below is the code I'm using: HTML: <div cl ...

Unable to generate package.json

After attempting to set the execution policies to unrestricted, I was able to resolve my dummy server error, but I encountered an issue creating package.json. The output is displayed below. Please note that I tried both npm init and npm init -y PS C:&bso ...

Express.js route not being properly handled in post request

This is the structure of my folders: https://i.sstatic.net/9BvbH.png I am attempting to create a python script that receives input from an HTML file and displays its output in the HTML. Below is the jQuery script in my HTML: <script src="https:/ ...

``There seems to be an issue with Firefox's background position percentage

After combining multiple images into one to reduce HTTP requests, I utilized background-position in percentage values to control which image was displayed. Since the width of the images is dictated by the parent element, using percentages rather than pixel ...

Retrieving JSON information from a PHP script with AJAX

I am currently experiencing an issue with my PHP script, 'getNews.php'. Despite working correctly in the terminal and returning the expected data, I am encountering difficulties when trying to retrieve this information through JavaScript. <?p ...

An issue with the font display

I'm having trouble with a specific font on my webpage and jsfiddle, even though it works fine on w3schools website. Here's an example on jsfiddle HTML <div> With CSS3, websites can finally use fonts other than the pre-selected "web-safe" ...

Removing an HTML element entirely

Although I am utilizing .remove() method and it is effectively removing the desired element, upon inspecting the page source by right-clicking in a browser window, I can still see those removed elements. It seems like they are not being permanently delet ...

Is it possible to bundle MongoDB into an Electron application?

Is it possible to include MongoDB in an Electron app to avoid having to install it on a client's computer? I am creating an application on OSX but it will most likely be used on Windows. Will the clients need to individually install Mongo themselves? ...

Creating an X pattern on an HTML5 canvas and detecting intersections with a perimeter

I am currently developing a maze game using HTML 5. Below is the function I have implemented to draw an "X" on the canvas (the player will guide the X through the maze using touchpad). dim = 8; function rect(x,y,xdim){ ctx.beginPath(); ctx.moveT ...

The SetTimeOut function is ineffective for resetting the colour of every cell in a table

I am facing an issue with two HTML tables. The cell values in both tables are changing every 3 seconds and the background color of each cell is set based on the value from the previous cycle. Additionally, a setTimeout function is used to reset the backgro ...

What are the best techniques for streamlining nested objects with Zod.js?

As a newcomer to zod.js, I have found that the DataSchema function is extremely helpful in verifying API data types and simplifying the API response easily. However, I'm curious if there is a way to streamline the data transformation process for myEx ...

Only one admin account can be accessed at a time by multiple logins

I am looking to add a new feature to my app where only one admin can log in at a time. If someone else tries to log in with the same admin ID on another device, a warning should be shown, indicating that the user is already logged in and cannot access the ...

What is the best method for adding files to JSZip from a remote URL?

Is it possible to load files into a Zip folder from a specified URL? For example: var zip = new JSZip(); zip.file("file.txt", "/site.net/files/file.txt"); Update I am following this example: I attempted the code provided but it was unsuccessful. I do ...

Remove an item from a list using Vue.js and Javascript

I have a votes array containing user information. I am looking to delete a user based on their id. role: 2 active: 2 about: null id: 3 email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="126677616652757f737b7e3c717d7f3c717 ...

The event for 'deviceready' in Cordova is not getting triggered when placed inside the angular .run block

Recently, I've been facing difficulties in getting 'deviceready' to register within AngularJS. It was functioning properly earlier, so I'm puzzled about what might have altered. If I trigger 'deviceready' from a global addEve ...

Error: SVG graphic fails to render in live platform environment

I am encountering a problem with viewing the svg elements in production mode, even though they worked perfectly fine in development mode (localhost). My team and I suspect that the issue lies with gulp. <img src="assets/logo.svg" alt="Log ...