简体   繁体   English

Bootstrap 4 Card Deck Fixed Card Width (当少于每行指定数量的卡片时)

[英]Bootstrap 4 Card Deck Fixed Card Width (when less than specified amount of cards per row)

I have a deck of x amount of cards on my website, so far, I am pretty pleased with how they behave responsively.我的网站上有一副 x 数量的卡片,到目前为止,我对它们的响应方式非常满意。 They never exceed the set amount, n, per row for a given resolution, they remain the same height etc. The only issue, however, is that If I have a row of 2, with 2 below, then an extra 1 in a third row, that final card will expand to fill the width of 2 cards instead of staying the correct size, and staying to the left.对于给定的分辨率,它们永远不会超过每行的设定数量 n,它们保持相同的高度等。但是,唯一的问题是,如果我有一排 2,下面有 2,那么三分之一的额外 1行,最后一张卡片将扩展以填充 2 张卡片的宽度,而不是保持正确的大小,并保持在左侧。

I am using a system similar to below to set how many cards are allowed per row, for a set resolution:我正在使用类似于下面的系统来设置每行允许多少张卡片,以实现设定的分辨率:

@media (min-width:992px) and (max-width: 1199px) {
   .card-deck span:nth-of-type(4n) {
      display: block;
      width: 100%;
    }

}
@media (min-width:768px) and (max-width: 991px) {
    .card-deck span:nth-of-type(3n) {
      display: block;
      width: 100%;
    }
}
@media (min-width: 576px) and (max-width: 767px) {
    .card-deck span:nth-of-type(2n) {
      display: block;
      width: 100%;
    }
}

And then each card is defined in HTML as:然后每张卡在 HTML 中定义为:

        <div class="card-deck">
        <div class="card mb-4" data-clickable="true" data-href="#">
            <img class="card-img-top" src="https://via.placeholder.com/700x400" alt="Card image cap">
            <div class="card-body">
              <h5 class="card-title">Test Card</h5>
            </div>
            <div class="card-footer">
              <small class="text-muted">More Info Here</small>
            </div>
        </div>
        <span></span>

Each card is separated by a span tag, I can't remember why.每张卡都由一个跨度标签分隔,我不记得为什么了。

Here is a codepen: https://www.codeply.com/p/A2JgtdhP1Y (press tablet view, then change the width whilst watching the bottom card/s)这是一个codepen: https://www.codeply.com/p/A2JgtdhP1Y (按平板电脑视图,然后在观看底部卡片的同时更改宽度)

As you can see at certain resolutions the cards on the bottom row, if they are less than the maximum amount of cards allowed in that row will try to fill the gap by expanding width ways, I would like to stop this and have them aligned to the left with the same width as the other cards above.正如您在某些分辨率下看到的那样,底行上的卡片,如果它们少于该行中允许的最大卡片数量,将尝试通过扩展宽度方式来填补空白,我想停止这个并将它们对齐到左边的宽度与上面的其他卡片相同。

EDIT: For anyone else who has this issue, setting the global max-width for cards works.编辑:对于遇到此问题的其他任何人,设置卡片的全局最大宽度是有效的。 You have to try a few different widths for all of your media queries so the bottom one lines up, I also used a html id for my card deck so this css would only apply to those specific cards and would not apply site wide.您必须为所有媒体查询尝试几种不同的宽度,因此底部一行,我还为我的卡片组使用了 html id,因此此 css 仅适用于这些特定卡片,不适用于站点范围。

You need to define max-width at global level.您需要在全局级别定义最大宽度。 You can refer to bootstrap documentation.您可以参考引导文档。

https://getbootstrap.com/docs/4.0/components/card/ https://getbootstrap.com/docs/4.0/components/card/

or you can refer this previous post.或者你可以参考这个之前的帖子。

Previously discussed: Bootstrap 4 card-deck with number of columns based on viewport 以前讨论过:Bootstrap 4 card-deck 与基于视口的列数

.card {
    max-width: 13rem;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Bootstrap 4卡Z索引减少到小于表thead - Reduce Bootstrap 4 Card Z-index less than table thead 如何从一副牌中随机选择 select 而不是 select 呢? - How to randomly select a card from a deck of cards and not select it again? 尝试洗牌时有 1 张牌未定义 - 1 card is undefined when trying to shuffle deck 如何将引导卡中的元素与卡底部对齐? - How to align elements in bootstrap cards to bottom of card? 如何在 bootstrap vue 的卡片组组中制作滑块? - How can I make slider in the card deck group on the bootstrap vue? 无法为反应 js 引导程序居中和空间卡组 - Can't center and space card deck for react js bootstrap 当屏幕宽度小于不工作的数量时更改文本 - Change text when screen width is less than amount not working 具有展开/折叠内容的引导 4 卡片组 - 希望展开的内容仅影响当前卡片 - bootstrap 4 card deck with expand / collapse content - would like the expanded content to just affect current card 折叠Bootstrap 4中的卡片表行时,更改Font Awesome图标 - Change Font Awesome icon when collapsing a card table row in Bootstrap 4 如何为卡片组中的卡片设置值并比较哪个具有“更高”或“更低”值? - How to set values for cards in a card deck and compare which one has "higher" or "lower" value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM