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

后端 未结 11 1142
無奈伤痛
無奈伤痛 2020-11-28 06:10

I\'m trying to implement the card-deck feature in bootstrap 4 to make all of my cards the same height.

The examples that bootstrap provides show 4 nice cards, but it

相关标签:
11条回答
  • 2020-11-28 06:51

    I got this to work by adding a min-width to the cards:

    <div class="card mb-3" style="min-width: 18rem;">
      <p>Card content</p>
    </div>
    

    The cards don't go below this width, but still properly fill each row and have equal heights.

    0 讨论(0)
  • 2020-11-28 06:56

    I've used CSS Grid to fix that. CSS Grid will make all the elements in the same row, all the same height.

    I haven't looked into making all the elements in all the rows the same height though.

    Anyway, here's how it can be done:

    HTML:

    <div class="grid-container">
    
      <div class="card">...</div>
      <div class="card">...</div>
    </div>
    

    CSS:

    .grid-container {
      display: grid;  
      grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    }
    

    Here's a complete JSFiddle. https://jsfiddle.net/bluegrounds/owjvhstq/4/

    0 讨论(0)
  • 2020-11-28 06:58

    Updated 2018

    If you want a responsive card-deck, use the visibility utils to force a wrap every X columns on different viewport width(breakpoints)...

    Bootstrap 4 responsive card-deck (v 4.1)


    Original answer for Bootstrap 4 alpha 2:

    You can use the grid col-*-* to get the different widths (instead of card-deck) and then set equal height to the cols using flexbox.

    .row > div[class*='col-'] {
      display: flex;
      flex:1 0 auto;
    }
    

    http://codeply.com/go/O0KdSG2YX2 (alpha 2)

    The problem is that w/o flexbox enabled the card-deck uses table-cell where it becomes very hard to control the width. As of Bootstrap 4 Alpha 6, flexbox is default so the additional CSS is not required for flexbox, and the h-100 class can be used to make the cards full height: http://www.codeply.com/go/gnOzxd4Spk


    Related question: Bootstrap 4 - Responsive cards in card-columns

    0 讨论(0)
  • 2020-11-28 07:00

    It took me a bit to figure this out, but the answer is to not use a card-deck, but instead to use .row and .cols.

    This makes a responsive set of cards with specifics for each screen size: 3 cards for xl, 2 for lg and md, and 1 for sm and xs. .my-3 puts a padding on top and bottom so they look nice.

    mixin postList(stuff)
      .row
        - site.posts.each(function(post, index){
          .col-sm-12.col-md-6.col-lg-6.col-xl-4
            .card.my-3
              img.card-img-top(src="...",  alt="Card image cap")
              .card-body
                h5.card-title Card title #{index}
                p.card-text Some quick example text to build on the card title and make up the bulk of the cards content.
                a.btn.btn-primary(href="#") Go somewhere
        - })
    
    0 讨论(0)
  • 2020-11-28 07:01

    This answer is for those who are using Bootstrap 4.1+ and for those who care about IE 11 as well

    Card-deck does not adapt the number visible of cards according to the viewport size.

    Above methods work but do not support IE. With the below method, you can achieve similar functionality and responsive cards.

    You can manage the number of cards to show/hide in different breakpoints.

    In Bootstrap 4.1+ columns are same height by default, just make sure your card/content uses all available space. Run the snippet, you'll understand

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <div class="container">
        <div class="row">
            <div class="col-sm-6 col-lg-4 mb-3">
                <div class="card mb-3 h-100">
    
                    <div class="card-body">
                        <h5 class="card-title">Card title</h5>
                        <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                        <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
                    </div>
                </div>
            </div>
            <div class="col-sm-6 col-lg-4 mb-3">
                <div class="card mb-3 h-100">
                    <div class="card-body">
                        <h5 class="card-title">Card title</h5>
                        <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                        <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
                    </div>
                </div>
            </div>
            <div class="col-sm-6 col-lg-4 mb-3">
                <div class="card mb-3 h-100">
                    <div class="card-body">
                        <h5 class="card-title">Card title</h5>
                        <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                        <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    0 讨论(0)
  • 2020-11-28 07:05

    Here's a solution with Sass to configure the number of cards per line depending on breakpoints: https://codepen.io/migli/pen/OQVRMw

    It works fine with Bootstrap 4 beta 3

    // Bootstrap 4 breakpoints & gutter
    $grid-breakpoints: (
        xs: 0,
        sm: 576px,
        md: 768px,
        lg: 992px,
        xl: 1200px
    ) !default;
    
    $grid-gutter-width: 30px !default;
    
    // number of cards per line for each breakpoint
    $cards-per-line: (
        xs: 1,
        sm: 2,
        md: 3,
        lg: 4,
        xl: 5
    );
    
    @each $name, $breakpoint in $grid-breakpoints {
        @media (min-width: $breakpoint) {
            .card-deck .card {
                flex: 0 0 calc(#{100/map-get($cards-per-line, $name)}% - #{$grid-gutter-width});
            }
        }
    }
    

    EDIT (2019/10)

    I worked on another solution which uses horizontal lists group + flex utilities instead of card-deck:

    https://codepen.io/migli/pen/gOOmYLb

    It's an easy solution to organize any kind of elements into responsive grid

    <div class="container">
        <ul class="list-group list-group-horizontal align-items-stretch flex-wrap">
            <li class="list-group-item">Cras justo odio</li>
            <li class="list-group-item">Dapibus ac facilisis in</li>
            <li class="list-group-item">Morbi leo risus</li>
            <li class="list-group-item">Cras justo odio</li>
            <li class="list-group-item">Dapibus ac facilisis in</li>
            <!--= add as many items as you need  =-->
        </ul>
    </div>
    
    .list-group-item {
        width: 95%;
        margin: 1% !important;
    }
    
    @media (min-width: 576px) {
        .list-group-item {
            width: 47%;
            margin: 5px 1.5% !important;
        }
    }
    
    @media (min-width: 768px) {
        .list-group-item {
            width: 31.333%;
            margin: 5px 1% !important;
        }
    }
    
    @media (min-width: 992px) {
        .list-group-item {
            width: 23%;
            margin: 5px 1% !important;
        }
    }
    
    @media (min-width: 1200px) {
        .list-group-item {
            width: 19%;
            margin: 5px .5% !important;
        }
    }
    
    0 讨论(0)
提交回复
热议问题