Customize Bootstrap 4 Card Width in Card Decks for a Fixed Number of Cards per Row

My website features a deck of cards, and I must say, they are quite responsive. They adhere to the set amount per row for each resolution and maintain consistent height. However, there is one issue that bothers me - when there are 2 cards in a row, with 2 below them and an extra card in a third row, that final card expands to fill the width of 2 cards instead of staying the correct size and aligned to the left.

To control how many cards appear per row based on resolution, I have implemented a system like this:

@media (min-width:992px) and (max-width: 1199px) {
   .card-deck span:nth-of-type(4n) {
      display: block;
      width: 100%;
    }

}
@media (min-width:768px) and (max-width: 991px) {
    .card-deck span:nth-of-type(3n) {
      display: block;
      width: 100%;
    }
}
@media (min-width: 576px) and (max-width: 767px) {
    .card-deck span:nth-of-type(2n) {
      display: block;
      width: 100%;
    }
}

Each card is structured in HTML as follows:

<div class="card-deck">
        <div class="card mb-4" data-clickable="true" data-href="#">
            <img class="card-img-top" src="https://via.placeholder.com/700x400" alt="Card image cap">
            <div class="card-body">
              <h5 class="card-title">Test Card</h5>
            </div>
            <div class="card-footer">
              <small class="text-muted">More Info Here</small>
            </div>
        </div>
        <span></span>

The use of span tags between each card might be a bit perplexing, but that's how it was done.

If you check out this Codepen (switch to tablet view and adjust the width), you'll notice that at certain resolutions, the cards in the bottom row try to expand to fill any empty spaces. My goal is to prevent this and have them aligned to the left with consistent widths like the rest of the cards.

EDIT: I discovered that setting a global max-width for the cards can resolve this issue. It may require some tweaking for different media queries to ensure alignment, and using a specific html id for the card deck will make sure the CSS only affects those cards and not the entire site.

Answer №1

To ensure proper layout, it's important to establish a maximum width at the global level. To learn more about this concept, you can consult the official Bootstrap documentation.

https://getbootstrap.com/docs/4.0/components/card/

Alternatively, you may find helpful information in the following post:

Previously discussed: Bootstrap 4 card-deck with number of columns based on viewport

.card {
    max-width: 13rem;
}

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

Experiencing Challenges with JavaScript Implementation Within an HTML Document

Code: <!DOCTYPE html> <head> <script type="text/javascript" src="jquery-3.3.1.js"> </script> <script type="text/javascript"> $(function() { alert("Hello World"); }); </script> <meta charset ...

Time-driven occurrences in HTML

I want to create a daily event, like an alert box or a message displayed in a window, at 10 am on my webpage. How can I achieve this without constantly checking the time and wasting resources? ...

Find a way to sift through an array of objects to ensure that each object is distinct from the others

Here is an array where objects with the same values for st and ct should be considered identical. For example, the first and third objects have the same values for st and ct, so the third object should be filtered out of the final array. How can I achiev ...

Mastering Bootstrap 4 flexbox for optimal content filling: A comprehensive guide

I am currently working on an angular app integrated with bootstrap 4. The challenge I am facing is related to the layout design. I have a fixed navbar at the top and a content area below it. Within the content area, I have a div that includes header and fo ...

What is the most effective way to extract content values from different divs for calculation using jQuery?

I am working on creating a function that retrieves the content values from the <div class="rowtabela"> div and reads the nodes of <div class="item v_...">. Check out my code below: <div class="adicionados" id=& ...

Utilizing the Embed tag to invoke URL requests repeatedly within an Angular application

I am currently developing a custom PDF viewer to display the PDF content fetched from an API response. The main issue I'm encountering is that the API call is being triggered multiple times, approximately 50 - 60 times or even more in some cases. Stra ...

Utilizing JQuery to extract the image title and render it as HTML code

I am currently facing an issue with displaying an image in my project. I want to hide the image using CSS (display: none) and instead, retrieve the value of its title attribute and display it within the parent div by utilizing JQuery. Despite knowing tha ...

Tips for transforming intricate JavaScript arrays into a unified array and iterating through to create a table

I need help combining two arrays that contain objects and reassigning the values in a standard format. Can anyone provide some guidance? Thank you. Here is my current situation: array1 = [{Apple: Ken, Orange: Mary, Melon: Tina, Strawberry: Jessica, Mango ...

Looking to retrieve the coordinates of an element following a CSS3 translation using JavaScript?

I came across this issue in two different variations on stackoverflow, but unfortunately the proposed solutions didn't work for me. My problem is that I need to translate an item, but when I try to retrieve its position with obj.style.left or obj.off ...

Having Trouble Shutting Down Bootstrap Modal Using Fade Feature

I have been using the code below to generate modal pop-ups dynamically, but I've run into an issue. The problem is that while the modal displays correctly, I am unable to close it. I've attempted both data-dismiss="modal" and .modal("hide"), but ...

Troubleshoot redirect issues in JavaScript or PHP

I am facing a simple issue that is proving to be time-consuming to solve. The challenge that I am encountering involves an HTML form with 2 buttons. Here is the relevant code snippet: $html1 = "<div class='pai-forms'> <form ...

The import error occurred because the module 'styled-components' does not export 'createGlobalStyle'

I am encountering an issue while attempting to import createGlobalStyle from styled-components. Despite having installed the styled-components npm package with version @3.4.10, it doesn't seem to work as expected. const GlobalStyle = createGlobalStyl ...

What is the best way to load multiple items using ajax?

I'm facing an issue when trying to load multiple external SVG files on a single HTML page. If I include only one SVG, everything works fine. However, when I attempt to call different SVG sprites, only one of them loads while the others are ignored. Is ...

Transition one background image into another using background positioning

Snippet of code: https://jsfiddle.net/foy4m43j/ HTML: <div id="background"></div> CSS: #background { background-image: url("http://i.imgur.com/hH9IWA0.png"); background-position: 0 0; background-repeat: repea ...

Dynamic Select Option housing Bootstrap Popover

I am attempting to create a popover that generates dynamic content and reacts when an option is selected. On my page, I have a select element and one of the options should trigger a popover containing a form with 'Accept' and 'Cancel' ...

Breakpoint for mobile menu in FoundationCSS

When the browser is resized, the mobile menu appears at 568x320, which is the default breakpoint. https://i.sstatic.net/ZSbiq.jpg I would like to create a breakpoint around 900px to address the issue with the menu being too large as shown in the image be ...

What is the best way to incorporate a state variable into a GraphQL query using Apollo?

My current challenge involves using a state as a variable for my query in order to fetch data from graphQl. However, I'm encountering an issue where the component does not read the state correctly. Any suggestions on how to solve this? class usersSc ...

Retrieve the latest inserted ID in a Node.js application and use it as a parameter in a subsequent query

I am currently working with an SQL database that consists of two tables, namely club and players. These tables are connected through a one-to-many relationship. Although the query in my node.js code is functioning properly, I am facing an issue retrieving ...

Showing different results in the same table every few seconds using jQuery AJAX

At the moment, I am displaying the 3 most recent items from a table in a static manner: Database Table : id | question | answer ------------------------------------------------- 1 | Q? | A? ...

Having trouble managing external CSS files on Django 1.3 development server

Trying to get external CSS files to work on the Django 1.3 development server has been a challenge. Despite reading through Django's 'managing static files' guide and various similar Stack Overflow questions, I still can't seem to figur ...