So in Bootstrap v4, I see there's a new feature for card decks ( http://v4-alpha.getbootstrap.com/components/card/#decks ) that makes a group of cards, with their height equal according to the tallest content in the group.
It appears the number of columns are based on how many card-div's are in the group. Is there a way to make the number of columns change based on the viewport? For example, 4 columns / card wide at large width, 2 wide at medium width, and 1 at small width?
Currently they stay the same number of columns/cards wide until less than 544px. At 544px and greater, they have display: table-cell
with the screen (min-width: 544px)
rule.
Is there a way to make the cards have different numbers of columns based on viewport by changing only the CSS?
Edit - Looking to not use flex / flexbox due to IE support
Codepen example of 4 col/card wide and 3 col/card wide at http://codepen.io/jpost-vlocity/full/PNEKKy/
This can be done with javascript.
First you need to create divs for each breakpoint in your html document.
Then you need to assign the divs a default value in css. Create media queries for each breakpoint that override that default value
Then write a some javascript. One function to test for the breakpoints by checking if the default css value you assigned has been overridden, and another to add and remove appropriate classes. I removed the
card-deck-wrapper
andcard-deck
classes when I wanted to make them responsive, and added the appropriatecol-**-**
classes. Finally, add calls to these functions in thedocument.ready
andwindow.resize
areas.Bootstrap 4 card deck responsive
I tried to use the
<container>
,<row>
and<col>
to make it responsive, but you lose thefeature of the card deck, if you do that. What you need is CSS:
Here is my CodePen: Bootstrap 4 card deck responsive
I found a pretty easy workaround using css-grid. You could tweak the 250px to fit your use case.
Update 2018
It's hard to set the cards widths (responsive) with
card-deck
because it usesdisplay:table-cell
andwidth:1%
.I found it easier to make them responsive using the cards inside the Bootstap grid
col-*
and then you can use the grid viewport breakpoints. Enable Bootstrap's flexbox if you want the cards to be equal height like thecard-deck
.http://www.codeply.com/go/6eqGPn3Qud
Also,
img-responsive
has changed toimg-fluid
Bootstrap 4.0.0
Flexbox is now the default, but there is still not a supported way to make responsive card-deck's. The recommended way is to use cards inside the grid:
Responsive cards using grid
Another way to make a responsive card-deck, is using responsive reset divs every x columns. This will force cards to wrap at specific breakpoints.
For example: 5 on xl, 4 on lg, 3 on md, 2 on sm, etc..
Responsive card deck demo (4.0.0)
Responsive card deck demo (alpha 6)
CSS pseudo elements variation
Here you go, http://codepen.io/KarlDoyle/pen/pypWbR
The main thing is that you have to override the styles. as shown below.