1
<br>
<h6>
This row/deck contains two elements. You can see that even with col-md-4 it spans more than 2/3 of the width.
</h6>
  <div class="flex-row">
    <div class="card-deck">
      <div class="card col-12 col-md-4">

      </div>
      <div class="card col-12 col-md-4">

      </div>
    </div>
  </div> <br><br>
  <h6>
This row/deck contains three elements. You can see that each card spaces 1/3 of the width as expected.
</h6>
  <div class="flex-row">
    <div class="card-deck">
      <div class="card col-12 col-md-4">

      </div>
      <div class="card col-12 col-md-4">

      </div>
            <div class="card col-12 col-md-4">

      </div>
    </div>
  </div> 
</div>

https://jsfiddle.net/9e85Lb0y/

In this example there are two flex-rows each containing a card deck. The first deck has 2 cards and the second deck has 3 cards. All of the cards are identical. Why is it that the top row has wider cards? I would have expected the top row to have the same widths as the bottom row.

2 Answers2

0

Columns aren't meant to be inside card-deck. They're only supposed to be contained in .row. The cards would go inside the columns.

From the Bootstrap docs...

"Rows are wrappers for columns... In a grid layout, content must be placed within columns and only columns may be immediate children of rows."

If you're trying to set widths for the cards in card-decks, see:

Bootstrap 4 card-deck with number of columns based on viewport, or
bootstrap 4 card-deck containing cards with different width

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
0

TLDR

Use flex-row and col-* to explicitly specify widths. Use card-deck and card to create evenly sized blocks. Avoid mixing the two, they are not designed for use together straight out of the box.

Long Answer

The issue here is most likely caused by the mixture of both the flex-row and card-deck classes. While both offer very similar functionality, there are some key differences which separate how they behave.

The card-deck class simply guarantees that any immediate elements with the card class are all the same width as one another.

<div class="card-deck">
  <div class="card">
    <!-- Content -->
  </div>
  <div class="card">
    <!-- Content -->
  </div>
  <div class="card">
    <!-- Content -->
  </div>
</div>

Every <div class="card"></div> will now become a uniform width. They are not guaranteed to fill the whole of their parent container. The card class also applies left and right margins of 15px to keep them separated.

<div class="flex-row">
  <div class="col-4">
    <!-- 1/3 Width Column -->
  </div>
  <div class="col-4">
    <!-- 1/3 Width Column -->
  </div>
  <div class="col-4">
    <!-- 1/3 Width Column -->
  </div>
</div>

The flex-row and col-* classes on the other hand allow you to create columns of specific size. Each col-* class simply applies a width to the element, the onus is on you to make sure your content will fit appropriately at each breakpoint. col's SHOULD NOT have any margins applied as CSS width declarations do not account for margins.