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

后端 未结 11 1143
無奈伤痛
無奈伤痛 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 07:06

    Define columns by min width based on viewport:

    
        /* Number of Cards by Row based on Viewport */
        @media (min-width: 576px) {
            .card-deck .card {
                min-width: 50.1%; /* 1 Column */
                margin-bottom: 12px;
            }
        }
    
        @media (min-width: 768px) {
            .card-deck .card {
                min-width: 33.4%;  /* 2 Columns */
            }
        }
    
        @media (min-width: 992px) {
            .card-deck .card {
                min-width: 25.1%;  /* 3 Columns */
            }
        }
    
        @media (min-width: 1200px) {
            .card-deck .card {
                min-width: 20.1%;  /* 4 Columns */
            }
        }
    
    0 讨论(0)
  • 2020-11-28 07:06

    There's simpler solution for that - set fixed height of card elements - header and body. This way, we can set resposive layout with standard boostrap column grid.

    Here is my example: http://codeply.com/go/RHDawRSBol

     <div class="card-deck text-center">
        <div class="col-sm-6 col-md-4 col-lg-3">
            <div class="card mb-4">
                <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap">
                <div class="card-body" style="height: 20rem">
                    <h4 class="card-title">1 Card title</h4>
                    <p class="card-text">This is a longer 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>
    
    0 讨论(0)
  • 2020-11-28 07:09

    @Zim provided a great solution above (well deserved up-vote from me), however, it didn't quite fit what I needed since I was implementing this in Jekyll and wanted my card deck to automatically update every time I added a post to my site. Growing a card deck such as this with each new post is straight forward in Jekyll, the challenge was to correctly place the breakpoints. My solution make use of additional liquid tags and modulo mathematics.

    While this question is old, I came across it and found it useful, and maybe someday someone will come along wanting to do this with Jekyll.

    <div class = "container">
      <div class = "card-deck">
    
        {% for post in site.posts %}
          <div class = "card border-0 mt-2">
            <a href = "{{ post.url }}"><img src = "{{ site.baseurl }}{{ post.image }}" class = "mx-auto" alt = "..."></a>
            <div class = "card-body">
              <h5 class = "card-title"><a href = "{{ post.url }}">{{ post.title }}</a></h5>
              <span>Published: {{ post.date | date_to_long_string }} </span>
              <p class = "text-muted">{{ post.excerpt }}</p>
            </div>
            <div class = "card-footer bg-white border-0"><a href = "{{ post.url }}" class = "btn btn-primary">Read more</a></div>
          </div>
    
          <!-- Use modulo to add divs to handle break points -->
          {% assign sm = forloop.index | modulo: 2 %}
          {% assign md = forloop.index | modulo: 3 %}
          {% assign lg = forloop.index | modulo: 4 %}
          {% assign xl = forloop.index | modulo: 5 %}
    
          {% if sm == 0 %}
            <div class="w-100 d-none d-sm-block d-md-none"><!-- wrap every 2 on sm--></div>
          {% endif %}
    
          {% if md == 0 %}
            <div class="w-100 d-none d-md-block d-lg-none"><!-- wrap every 3 on md--></div>
          {% endif %}
    
          {% if lg == 0 %}
            <div class="w-100 d-none d-lg-block d-xl-none"><!-- wrap every 4 on lg--></div>
          {% endif %}
    
          {% if xl == 0 %}
            <div class="w-100 d-none d-xl-block"><!-- wrap every 5 on xl--></div>
          {% endif %}
    
        {% endfor %}
      </div>
    </div>
    

    This whole code block can be used directly in a website or saved in your Jekyll project _includes folder.

    0 讨论(0)
  • 2020-11-28 07:13
    <div class="w-100 d-lg-none mt-4"></div>
    

    I created 4 cards and place this code between second and third card, try this.

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

    Using Bootstrap 4.4.1, I was able to set the number of cards per deck using simple classes by adding some scss into the mix.

    HTML

    <div class="card-deck deck-1 deck-md-2 deck-lg-3">
       <div class="card">
          <h2 class="card-header">Card 1</h3>
          <div class="card-body">
              Card body
          </div>
          <div class="card-footer">
              Card footer
          </div>
       </div>
       <div class="card">
          <h2 class="card-header">Card 2</h3>
          <div class="card-body">
              Card body
          </div>
          <div class="card-footer">
              Card footer
          </div>
       </div>
       <div class="card">
          <h2 class="card-header">Card 3</h3>
          <div class="card-body">
              Card body
          </div>
          <div class="card-footer">
              Card footer
          </div>
       </div>
    </div>
    

    SCSS

    // _card_deck_columns.scss
    // add deck-X and deck-BP-X classes to select the number of cards per line
    @for $i from 1 through $grid-columns {
      .deck-#{$i} > .card {
        $percentage: percentage(1 / $i);
        @if $i == 1 {
          $width: $percentage;
          flex-basis: $width;
          max-width: $width;
          margin-left: 0;
          margin-right: 0;
        } @else {
          $width: unquote("calc(#{$percentage} - #{$grid-gutter-width})");
          flex-basis: $width;
          max-width: $width;
        }
      }
    }
    @each $breakpoint in map-keys($grid-breakpoints) {
      $infix: breakpoint-infix($breakpoint, $grid-breakpoints);
      @include media-breakpoint-up($breakpoint) {
        @for $i from 1 through $grid-columns {
          .deck#{$infix}-#{$i} > .card {
            $percentage: percentage(1 / $i);
            @if $i == 1 {
              $width: $percentage;
              flex-basis: $width;
              max-width: $width;
              margin-left: 0;
              margin-right: 0;
            } @else {
              $width: unquote("calc(#{$percentage} - #{$grid-gutter-width})");
              flex-basis: $width;
              max-width: $width;
              margin-left: $grid-gutter-width / 2;
              margin-right: $grid-gutter-width / 2;
            }
          }
        }
      }
    }
    

    CSS

    .deck-1 > .card {
      flex-basis: 100%;
      max-width: 100%;
      margin-left: 0;
      margin-right: 0; }
    
    .deck-2 > .card {
      flex-basis: calc(50% - 30px);
      max-width: calc(50% - 30px); }
    
    .deck-3 > .card {
      flex-basis: calc(33.3333333333% - 30px);
      max-width: calc(33.3333333333% - 30px); }
    
    .deck-4 > .card {
      flex-basis: calc(25% - 30px);
      max-width: calc(25% - 30px); }
    
    .deck-5 > .card {
      flex-basis: calc(20% - 30px);
      max-width: calc(20% - 30px); }
    
    .deck-6 > .card {
      flex-basis: calc(16.6666666667% - 30px);
      max-width: calc(16.6666666667% - 30px); }
    
    .deck-7 > .card {
      flex-basis: calc(14.2857142857% - 30px);
      max-width: calc(14.2857142857% - 30px); }
    
    .deck-8 > .card {
      flex-basis: calc(12.5% - 30px);
      max-width: calc(12.5% - 30px); }
    
    .deck-9 > .card {
      flex-basis: calc(11.1111111111% - 30px);
      max-width: calc(11.1111111111% - 30px); }
    
    .deck-10 > .card {
      flex-basis: calc(10% - 30px);
      max-width: calc(10% - 30px); }
    
    .deck-11 > .card {
      flex-basis: calc(9.0909090909% - 30px);
      max-width: calc(9.0909090909% - 30px); }
    
    .deck-12 > .card {
      flex-basis: calc(8.3333333333% - 30px);
      max-width: calc(8.3333333333% - 30px); }
    
    .deck-1 > .card {
      flex-basis: 100%;
      max-width: 100%;
      margin-left: 0;
      margin-right: 0; }
    
    .deck-2 > .card {
      flex-basis: calc(50% - 30px);
      max-width: calc(50% - 30px);
      margin-left: 15px;
      margin-right: 15px; }
    
    .deck-3 > .card {
      flex-basis: calc(33.3333333333% - 30px);
      max-width: calc(33.3333333333% - 30px);
      margin-left: 15px;
      margin-right: 15px; }
    
    .deck-4 > .card {
      flex-basis: calc(25% - 30px);
      max-width: calc(25% - 30px);
      margin-left: 15px;
      margin-right: 15px; }
    
    .deck-5 > .card {
      flex-basis: calc(20% - 30px);
      max-width: calc(20% - 30px);
      margin-left: 15px;
      margin-right: 15px; }
    
    .deck-6 > .card {
      flex-basis: calc(16.6666666667% - 30px);
      max-width: calc(16.6666666667% - 30px);
      margin-left: 15px;
      margin-right: 15px; }
    
    .deck-7 > .card {
      flex-basis: calc(14.2857142857% - 30px);
      max-width: calc(14.2857142857% - 30px);
      margin-left: 15px;
      margin-right: 15px; }
    
    .deck-8 > .card {
      flex-basis: calc(12.5% - 30px);
      max-width: calc(12.5% - 30px);
      margin-left: 15px;
      margin-right: 15px; }
    
    .deck-9 > .card {
      flex-basis: calc(11.1111111111% - 30px);
      max-width: calc(11.1111111111% - 30px);
      margin-left: 15px;
      margin-right: 15px; }
    
    .deck-10 > .card {
      flex-basis: calc(10% - 30px);
      max-width: calc(10% - 30px);
      margin-left: 15px;
      margin-right: 15px; }
    
    .deck-11 > .card {
      flex-basis: calc(9.0909090909% - 30px);
      max-width: calc(9.0909090909% - 30px);
      margin-left: 15px;
      margin-right: 15px; }
    
    .deck-12 > .card {
      flex-basis: calc(8.3333333333% - 30px);
      max-width: calc(8.3333333333% - 30px);
      margin-left: 15px;
      margin-right: 15px; }
    
    @media (min-width: 576px) {
      .deck-sm-1 > .card {
        flex-basis: 100%;
        max-width: 100%;
        margin-left: 0;
        margin-right: 0; }
    
      .deck-sm-2 > .card {
        flex-basis: calc(50% - 30px);
        max-width: calc(50% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-sm-3 > .card {
        flex-basis: calc(33.3333333333% - 30px);
        max-width: calc(33.3333333333% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-sm-4 > .card {
        flex-basis: calc(25% - 30px);
        max-width: calc(25% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-sm-5 > .card {
        flex-basis: calc(20% - 30px);
        max-width: calc(20% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-sm-6 > .card {
        flex-basis: calc(16.6666666667% - 30px);
        max-width: calc(16.6666666667% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-sm-7 > .card {
        flex-basis: calc(14.2857142857% - 30px);
        max-width: calc(14.2857142857% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-sm-8 > .card {
        flex-basis: calc(12.5% - 30px);
        max-width: calc(12.5% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-sm-9 > .card {
        flex-basis: calc(11.1111111111% - 30px);
        max-width: calc(11.1111111111% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-sm-10 > .card {
        flex-basis: calc(10% - 30px);
        max-width: calc(10% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-sm-11 > .card {
        flex-basis: calc(9.0909090909% - 30px);
        max-width: calc(9.0909090909% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-sm-12 > .card {
        flex-basis: calc(8.3333333333% - 30px);
        max-width: calc(8.3333333333% - 30px);
        margin-left: 15px;
        margin-right: 15px; } }
    @media (min-width: 768px) {
      .deck-md-1 > .card {
        flex-basis: 100%;
        max-width: 100%;
        margin-left: 0;
        margin-right: 0; }
    
      .deck-md-2 > .card {
        flex-basis: calc(50% - 30px);
        max-width: calc(50% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-md-3 > .card {
        flex-basis: calc(33.3333333333% - 30px);
        max-width: calc(33.3333333333% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-md-4 > .card {
        flex-basis: calc(25% - 30px);
        max-width: calc(25% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-md-5 > .card {
        flex-basis: calc(20% - 30px);
        max-width: calc(20% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-md-6 > .card {
        flex-basis: calc(16.6666666667% - 30px);
        max-width: calc(16.6666666667% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-md-7 > .card {
        flex-basis: calc(14.2857142857% - 30px);
        max-width: calc(14.2857142857% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-md-8 > .card {
        flex-basis: calc(12.5% - 30px);
        max-width: calc(12.5% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-md-9 > .card {
        flex-basis: calc(11.1111111111% - 30px);
        max-width: calc(11.1111111111% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-md-10 > .card {
        flex-basis: calc(10% - 30px);
        max-width: calc(10% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-md-11 > .card {
        flex-basis: calc(9.0909090909% - 30px);
        max-width: calc(9.0909090909% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-md-12 > .card {
        flex-basis: calc(8.3333333333% - 30px);
        max-width: calc(8.3333333333% - 30px);
        margin-left: 15px;
        margin-right: 15px; } }
    @media (min-width: 992px) {
      .deck-lg-1 > .card {
        flex-basis: 100%;
        max-width: 100%;
        margin-left: 0;
        margin-right: 0; }
    
      .deck-lg-2 > .card {
        flex-basis: calc(50% - 30px);
        max-width: calc(50% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-lg-3 > .card {
        flex-basis: calc(33.3333333333% - 30px);
        max-width: calc(33.3333333333% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-lg-4 > .card {
        flex-basis: calc(25% - 30px);
        max-width: calc(25% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-lg-5 > .card {
        flex-basis: calc(20% - 30px);
        max-width: calc(20% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-lg-6 > .card {
        flex-basis: calc(16.6666666667% - 30px);
        max-width: calc(16.6666666667% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-lg-7 > .card {
        flex-basis: calc(14.2857142857% - 30px);
        max-width: calc(14.2857142857% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-lg-8 > .card {
        flex-basis: calc(12.5% - 30px);
        max-width: calc(12.5% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-lg-9 > .card {
        flex-basis: calc(11.1111111111% - 30px);
        max-width: calc(11.1111111111% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-lg-10 > .card {
        flex-basis: calc(10% - 30px);
        max-width: calc(10% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-lg-11 > .card {
        flex-basis: calc(9.0909090909% - 30px);
        max-width: calc(9.0909090909% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-lg-12 > .card {
        flex-basis: calc(8.3333333333% - 30px);
        max-width: calc(8.3333333333% - 30px);
        margin-left: 15px;
        margin-right: 15px; } }
    @media (min-width: 1200px) {
      .deck-xl-1 > .card {
        flex-basis: 100%;
        max-width: 100%;
        margin-left: 0;
        margin-right: 0; }
    
      .deck-xl-2 > .card {
        flex-basis: calc(50% - 30px);
        max-width: calc(50% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-xl-3 > .card {
        flex-basis: calc(33.3333333333% - 30px);
        max-width: calc(33.3333333333% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-xl-4 > .card {
        flex-basis: calc(25% - 30px);
        max-width: calc(25% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-xl-5 > .card {
        flex-basis: calc(20% - 30px);
        max-width: calc(20% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-xl-6 > .card {
        flex-basis: calc(16.6666666667% - 30px);
        max-width: calc(16.6666666667% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-xl-7 > .card {
        flex-basis: calc(14.2857142857% - 30px);
        max-width: calc(14.2857142857% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-xl-8 > .card {
        flex-basis: calc(12.5% - 30px);
        max-width: calc(12.5% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-xl-9 > .card {
        flex-basis: calc(11.1111111111% - 30px);
        max-width: calc(11.1111111111% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-xl-10 > .card {
        flex-basis: calc(10% - 30px);
        max-width: calc(10% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-xl-11 > .card {
        flex-basis: calc(9.0909090909% - 30px);
        max-width: calc(9.0909090909% - 30px);
        margin-left: 15px;
        margin-right: 15px; }
    
      .deck-xl-12 > .card {
        flex-basis: calc(8.3333333333% - 30px);
        max-width: calc(8.3333333333% - 30px);
        margin-left: 15px;
        margin-right: 15px; } }
    
    0 讨论(0)
提交回复
热议问题