What is the best way to ensure that Bootstrap 4 card decks have consistent widths on each row

I have a layout where I am displaying four cards in each row using a card deck:

<div class="row">
  <div class="card-deck">
    <!-- Four of these -->
    <div class="card">
      <img class="card-img-top img-adjusted" >
       <div class="card-body">...</div>
    </div>
  </div>
</div>
<div class="row">
  <div class="card-deck">
    <!-- Four of these -->
    <div class="card">
      <img class="card-img-top img-adjusted" >
       <div class="card-body">...</div>
    </div>
  </div>
</div>
...

The width of the cards within each deck is consistent, however, the decks themselves are not equal in width. For example, the deck in the second row is wider than the one in the first row. How can I ensure that all decks have the same width?

I attempted to set .card-decks{width: 100%;}, which worked for full rows but caused distortion when there were only 1-2 cards in a row.

Additional CSS: Since my images vary in size, I added the following CSS to make them appear uniform without impacting other styles:

.img-adjusted{
position: relative;
float: left;
background-position: 50% 50%;
background-repeat:   no-repeat;
background-size:     cover;
}

To maintain responsiveness and avoid setting specific pixel widths, I would like a solution that adapts to different screen sizes dynamically.

Answer №1

It's important not to place .card-deck inside a .row. Only .col- columns should be nested within rows..

Content should always be placed within columns, and only columns can be direct children of rows.

The recommended structure is...

<div class="row">
 <div class="col-12">
  <div class="card-deck">
    <!-- 4 cards here -->
    <div class="card">
      <img class="card-img-top img-adjusted" >
       <div class="card-body">...</div>
    </div>
  </div>
 </div>
</div>

The second part of the answer...

When using card-deck (and card-group), cards will automatically fill the width of the next visual row. If you have fewer than 4 cards in each row, the remaining cards will expand to fill the width across. To address this, set a max-width for each card.

.card-deck .card {
    max-width: calc(25% - 30px);
}

Alternatively, you can utilize grid columns to wrap the cards. More details can be found here: Bootstrap 4 card-deck with number of columns based on viewport

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

employing iframes dynamically to overlay a webpage

I inserted this iframe into a webpage <iframe src='http://redbug.redrocksoftware.com.au:80/Pages/chamara.html' style="position:absolute;z-index:1;" ></iframe> The chamara.html page has a button that, when clicked, should cover the c ...

Change the information displayed on buttons when clicked and provide options for users to navigate between different

In order to change the content when a button or number is clicked, you can utilize the buttons "1,2,3,4" or the Next and Prev buttons shown in the snippet. Both methods allow access to the same article. If you want to switch between articles using the Next ...

Establishing headings and frames for a collection of Essays

I have integrated a RSS Feed into my Vue.js application to display a series of articles. I want to organize these articles in a container similar to tweets on the left, with a heading labeled Latest Articles. However, when I use a tag in the template sect ...

Enhance the functionality of various textareas by implementing bullets for easier organization and formatting

Is there a way to add bullets to hidden textareas that are created dynamically? Currently, I can add bullets to visible textareas but would like the functionality to extend to newly created ones as well. Additionally, is it possible for these new bullets t ...

Converting a JavaScript object into HTML output

I have received the following JSON data: [    {       "fields": {          "url": "http://www.domain_name.co.uk/MP3/SF560783-01-01-01.mp3\n",          "track_name": "Lion City ",          "release_id": 560783,    ...

What is the process for extracting content from CSS comments or annotations in a stylesheet?

Here's an interesting concept: allowing users to define a set of CSS rules with annotations. For example: /* @name Page style */ body { font: 16px/1.5 Arial; /* @editable */ background-color: #fff; /* @editable */ } /* @name Section header */ ...

additional dark border streak

When you resize the browser window, you will notice a different layout for iPhone. I am seeing an additional black line below the slider and I'm unsure how to remove it. I have already tried removing the border property but the black line remains. Bel ...

Desktop keyboards are essential for inputting data into HTML fields that are not

I'm currently working on developing an on-screen keyboard. My goal is to prevent the mobile device keyboard from opening while the on-screen keyboard is active. One approach is to make the input read-only so that it doesn't open the keyboard when ...

Seeking a more deliberate option instead of using $(window).load, one that is not as quick as $(document)

Currently, my goal is to utilize JavaScript to conceal my preloader once the DOM and key elements have been loaded. The issue lies in the fact that various iframes on my page can significantly slow down this process. It seems that using jQuery's $(do ...

Is there a way to automatically redirect a webpage based on the URL that is entered

Looking for a solution where a page checks a typed link upon loading, such as: www.test.com/myphp.php=10 If it detects a specific number, it should redirect to another link. For example: Upon finding www.test.com/myphp.php=10, it redirects to www. ...

Use flexbox to manage the width of containers and control the wrapping of elements

Hello, I am a newcomer to utilizing flexbox in CSS. I have these "boxes" that need to maintain their width and wrap when the screen size is small. On a large screen: https://i.sstatic.net/dXnGC.png On a small screen: https://i.sstatic.net/8rZso.pn ...

How can I position a Button at the center of my Grid item using Material UI?

In my React 16.13.0 application, I am utilizing Material UI's Grid and trying to center the content of a button in the middle of a row. Here is what I have tried: center: { alignItems: "center", width: "100%", }, halfRow: { width: "5 ...

Interactive calendar feature displaying events upon hovering over a date

I need some assistance with displaying a drop-down list on full calendar events when hovering over the events. Can someone provide guidance? Here is a glimpse of what I currently have: https://i.sstatic.net/fZXMH.png I've attempted setting the z-in ...

Arranging divs within list items in a single row

My goal is to create a dropdown menu within an <li> element of <ul>. Here's what I have so far: HTML: <ul class="navigationTable"> <li>1</li> <li>2</li> <li><div id="comittee"> <div id= ...

How can I align the navigation bar in the center using Bootstrap?

Let's discuss the code snippet provided below. I am looking to use Bootstrap to center this dropdown menu on the left side of the screen. Here is the screenshot for reference: . Any suggestions on how I can achieve this? Also, I would like to know h ...

`Display PowerPoint file within your internet browser`

I need assistance with viewing pptx files in a web browser that have been uploaded to a specific location using .net core. (approximately 40 mb) (approximately 4 mb) While attempting to view both files using the code below, I am able to see the MonitorP ...

Create and exhibit flawless and optimal React JavaScript code following industry best practices

I recently wrote the code below in my React application (file name: SearchBox.jsx): import React from 'react' import ReactDOM from 'react-dom' import $ from 'jquery' export default class SearchBox extends React.Component { ...

What drawbacks come with using PHP as opposed to HTML?

Currently, I have a small number of pages published, and I am considering transitioning them from html to php. While I intend to use php more extensively in the future, I am not quite ready to fully commit to it as my primary language just yet. I underst ...

Presentation - hyperlink only functioning for final slide

I have a question about Lean Slider, which you can check out at My goal is to link each slide to a different URL. However, I'm facing an issue where only the code in the last slide seems to be executed and applied to all other slides. For example, if ...

Can you explain how bootstrap's implementation of sass maps functions?

According to Bootstraps documentation: In order to modify an existing color in our $theme-colors map, simply include the following code in your custom Sass file: $theme-colors: ( "primary": #0074d9, "danger": #ff4136 ); However, the question remains: ...