Bootstrap 4 - Responsive cards in card-columns

前端 未结 6 851
遇见更好的自我
遇见更好的自我 2020-11-29 20:27

I\'m playing with Bootstrap 4 and I can\'t find a solution to add responsiveness to cards while in a div with class=\"card-columns\" (this class ap

6条回答
  •  春和景丽
    2020-11-29 21:00

    Another late answer, but I was playing with this and came up with a general purpose Sass solution that I found useful and many others might as well. To give an overview, this introduces new classes that can modify the column count of a .card-columns element in very similar ways to columns with .col-4 or .col-lg-3:

    @import "bootstrap";
    
    $card-column-counts: 1, 2, 3, 4, 5;
    
    .card-columns {
        @each $column-count in $card-column-counts {
            &.card-columns-#{$column-count} {
                column-count: $column-count;
            }
        }
    
        @each $breakpoint in map-keys($grid-breakpoints) {
            @include media-breakpoint-up($breakpoint) {
                $infix: breakpoint-infix($breakpoint, $grid-breakpoints);
                @each $column-count in $card-column-counts {
                    &.card-columns#{$infix}-#{$column-count} {
                        column-count: $column-count;
                    }
                }
            }
        }
    }
    

    The end result of this is if you have the following:

    ...

    Then you would have 2 columns by default, 3 for medium devices and up and 4 for xl devices and up. Additionally if you change your grid breakpoints this will automatically support those, and the $card-column-counts can be overridden to change the allowed numbers of columns.

提交回复
热议问题