Cards with different sizes in Bootstrap 4 card-group

≡放荡痞女 提交于 2019-11-30 18:11:27
Zim

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

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

Bass Jobsen

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!