Cards with different sizes in Bootstrap 4 card-gro

2020-02-28 04:42发布

问题:

Is it possible to mix card of different sizes within a card-group in Bootstrap 4. I want to have a large card (double width) on the left size, and two smaller cards on the right, with the same height of all three.

<div class="container">
  <div class="row">
    <div class="card-group">
        <div class="card col-md-6">
          <div class="card-block">
            <h4 class="card-title">Card 1</h4>
            <p class="card-text">Text 1</p>
          </div>
        </div>
         <div class="card col-md-3">
          <div class="card-block">
            <h4 class="card-title">Card 2</h4>
            <p class="card-text">Text 2</p>
            <p class="card-text">More text 2</p>
            <p class="card-text">More text 2</p>
          </div>
        </div>
        <div class="card col-md-3">
          <div class="card-block">
            <h4 class="card-title">Card 3</h4>
            <p class="card-text">Text 3</p>
          </div>
        </div>
      </div>
    </div>
  </div>

回答1:

I think the intention is that card groups are equal width and height(http://v4-alpha.getbootstrap.com/components/card/#groups), but you could override the default Bootstrap behavior to make it work like this..

.card-group [class*='col-'] {
  float:none;
}

http://codeply.com/go/4WVwRBTyTP

Note: Wildcard CSS selectors like this are slow. It would be better to add a special CSS class to override the Bootstrap float:left behavior of the columns in your card-group

UPDATE

Now that BS4 has flexbox, the extra CSS is no longer required. Just make the card-group and row the same div, and then use col-* as normal to set width. However, using card-group will prevent responsive column wrapping.

http://www.codeply.com/go/jzIcjyg6Xa



回答2:

The Bootstrap 4.1 documentation for card layout currently states:

For the time being, these layout options are not yet responsive.

However, as Zim's answer pointed out, card-groups are now flexbox. Therefore, you can also override the flex-grow style to set relative card sizes.

<div class="card-group">
    <div class="card" style="flex-grow: 2">
        <div class="card-block">
            <h4 class="card-title">Card 1</h4>
            <p class="card-text">Text 1</p>
        </div>
    </div>
    <div class="card">
        <div class="card-block">
            <h4 class="card-title">Card 2</h4>
            <p class="card-text">Text 2</p>
            <p class="card-text">More text 2</p>
            <p class="card-text">More text 2</p>
        </div>
    </div>
    <div class="card">
        <div class="card-block">
            <h4 class="card-title">Card 3</h4>
            <p class="card-text">Text 3</p>
        </div>
    </div>
</div>

Codeply



回答3:

See also https://stackoverflow.com/a/35627731/1596547, as already explained by @Skelly the predefined grid classes (col-md-*) do not only set the width by also a float.

Instead of using the grid classes you can also apply the width directly:

    <div class="card-group">
        <div class="card" style="width:50%;">
          <div class="card-block">
            <h4 class="card-title">Card 1</h4>
            <p class="card-text">Text 1</p>
          </div>
        </div>
         <div class="card">
          <div class="card-block">
            <h4 class="card-title">Card 2</h4>
            <p class="card-text">Text 2</p>
            <p class="card-text">More text 2</p>
            <p class="card-text">More text 2</p>
          </div>
        </div>
        <div class="card">
          <div class="card-block">
            <h4 class="card-title">Card 3</h4>
            <p class="card-text">Text 3</p>
          </div>
        </div>
      </div>