如何在 bootstrap 4 中包装(浮动)卡片

How to wrap (float) cards in bootstrap 4

我正在使用 bootstrap 4 测试版。

我有一张卡片清单。我希望它们具有固定宽度,但高度将根据内容变化。

卡片的数量可以从1到n。

我想要的效果是卡片从左到右排列,直到屏幕 space 用完,然后换行到下一行。

我已经尝试了 bootstrap 4 卡片示例页面中列出的所有不同选项,但它们似乎没有换行。

我怎样才能做到这一点?

下面的示例显示了它们 3 个横跨,它们之间有很多 space。

 <div class="card-columns">

        <div class="card" style="width: 18rem;">
            <img class="card-img-top" data-src="..." alt="Card image cap">
            <div class="card-block">
                <h4 class="card-title">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>
        <div class="card"style="width: 18rem;">
            <img class="card-img-top" data-src="..." alt="Card image cap">
            <div class="card-block">
                <h4 class="card-title">Card title</h4>
                <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p>
                <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
            </div>
        </div>
        <div class="card" style="width: 18rem;">
            <img class="card-img-top" data-src="..." alt="Card image cap">
            <div class="card-block">
                <h4 class="card-title">Card title</h4>
                <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This card has even longer content than the first to show that equal height action.</p>
                <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
            </div>
        </div>
        <div class="card" style="width: 18rem;">
            <img class="card-img-top" data-src="..." alt="Card image cap">
            <div class="card-block">
                <h4 class="card-title">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>
        <div class="card" style="width: 18rem;">
            <img class="card-img-top" data-src="..." alt="Card image cap">
            <div class="card-block">
                <h4 class="card-title">Card title</h4>
                <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p>
                <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
            </div>
        </div>
        <div class="card" style="width: 18rem;">
            <img class="card-img-top" data-src="..." alt="Card image cap">
            <div class="card-block">
                <h4 class="card-title">Card title</h4>
                <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This card has even longer content than the first to show that equal height action.</p>
                <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
            </div>
        </div>
    </div>

这里是解决方案... 使用 bootstrap 行和列来实现这一点,请遵循以下代码。

<div class="row">
 <div class="col-lg-4 col-md-3 col-sm-6">
   <div class="card">
      <img class="card-img-top" data-src="..." alt="Card image cap">
      <div class="card-block">
         <h4 class="card-title">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>
 </div>
</div>

我想出了一个有效的浮动卡class:

<style>
    .floating-card {
        float: left;
        margin: 5px;
        max-width: 300px;

    }
</style>

用法示例:

    <div class="floating-card sortable">
    <section class="box-typical task-card task">
        <div class="task-card-photo">
            <img src="img/img-task.jpg" alt="">
        </div>
        <div class="task-card-in">
            <div class="btn-group task-card-menu">
                <button type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <i class="font-icon-dots-vert-square"></i>
                </button>
                <div class="dropdown-menu dropdown-menu-right">
                    <a class="dropdown-item" href="#"><i class="font-icon font-icon-pencil"></i>Edit</a>
                    <a class="dropdown-item" href="#"><i class="font-icon font-icon-archive"></i>Archive</a>
                    <a class="dropdown-item" href="#"><i class="font-icon font-icon-trash"></i>Delete</a>
                </div>
            </div>
            <div class="task-card-title">
                <a href="#">Create new webpage for sales and marketing stuff</a>
                <span class="task-card-title-label">(BIG)</span>
            </div>
            <div class="progress-compact-style">
                <progress class="progress" value="25" max="100">
                    <div class="progress">
                        <span class="progress-bar" style="width: 25%;">25%</span>
                    </div>
                </progress>
                <div class="progress-compact-style-label">37% done</div>
            </div>
            <div class="task-card-tags">
                <a href="#" class="label label-light-grey">Default</a>
                <a href="#" class="label label-light-grey">Primary</a>
                <a href="#" class="label label-light-grey">Success</a>
            </div>
        </div>
        <div class="task-card-footer">
            <div class="task-card-meta-item"><i class="font-icon font-icon-comments-2"></i>10</div>
            <div class="task-card-meta-item"><i class="font-icon font-icon-clip"></i>24</div>
            <div class="avatar-preview avatar-preview-32">
                <a href="#">
                    <img src="img/photo-64-2.jpg" alt="">
                </a>
            </div>
        </div>
    </section><!--.task-card-->
</div>

使用了一段时间后,我提出的浮动解决方案并不令人满意,因为在某些情况下,某些卡片下方有大量 space.. 审查后 bootstrap 4 张卡片,我发现一个功能完全符合我的要求:"card-columns"

它将您的卡片排成三列,并在屏幕尺寸较小时重新排列成一列。

Bootstrap Docs on card-columns

根据 Srikanth 的建议,您可以使用以下代码。您可以调整 class="col-lg-3 col-md-4 col-sm-6 以根据浏览器的宽度显示更多或更少的卡片。我还包括一个宽度显示来显示像素宽度。

当然可以调整php循环数量来测试更多或更少的卡数。

<body>        
    <center>Width: <span id="width"></span></center>
    <br>
    <div class="container">            
        <div class="row">
<?php    
    for ($x = 0; $x <= 10; $x++) {
        $card = <<<EOF
            <div class="col-lg-3 col-md-4 col-sm-6" style="margin-bottom:10px;">
                <div class="card">
                     <img class="card-img-top" src="your_image.jpg" alt="Card image cap">
                     <div class="card-block" style="margin:5px;">
                         <h4 class="card-title">Card title {$x}</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>
                         <center class="link">Join</center>
                     </div>      
                 </div>
            </div>
EOF;
      echo $card;
    }    
?>     
        </div>   
    </div>
<script>
$(window).resize(function() {
    var viewportWidth = $(window).width();
    $('#width').html(viewportWidth);    
});
</script>
</body>

在 5.1 版中,Bootstrap 已经放弃了版本 4 中使用的 CSS 技术。Here 他们提供了一个如何使用 Masonry 插件的示例,请参阅此片段:

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/masonry-layout@4.2.2/dist/masonry.pkgd.min.js" integrity="sha384-GNFwBvfVxBkLMJpYMOABq3c+d3KnQxudP/mGPkzpZSTYykLBNsZEnG2D9G/X/+7D" crossorigin="anonymous" async></script>

</head>

<body>
  <main class="container py-5">
    <h1>Bootstrap and Masonry</h1>
    <div class="row" data-masonry='{"percentPosition": true }'>
      <div class="col-sm-6 col-lg-4 mb-4">
        <div class="card">
          <svg class="bd-placeholder-img card-img-top" width="100%" height="200" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: Image cap" preserveAspectRatio="xMidYMid slice" focusable="false"><title>Placeholder</title><rect width="100%" height="100%" fill="#868e96"/><text x="50%" y="50%" fill="#dee2e6" dy=".3em">Image cap</text></svg>

          <div class="card-body">
            <h5 class="card-title">Card title that wraps to a new line</h5>
            <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>
          </div>
        </div>
      </div>
      <div class="col-sm-6 col-lg-4 mb-4">
        <div class="card p-3">
          <figure class="p-3 mb-0">
            <blockquote class="blockquote">
              <p>A well-known quote, contained in a blockquote element.</p>
            </blockquote>
            <figcaption class="blockquote-footer mb-0 text-muted">
              Someone famous in <cite title="Source Title">Source Title</cite>
            </figcaption>
          </figure>
        </div>
      </div>
      <div class="col-sm-6 col-lg-4 mb-4">
        <div class="card">
          <svg class="bd-placeholder-img card-img-top" width="100%" height="200" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: Image cap" preserveAspectRatio="xMidYMid slice" focusable="false"><title>Placeholder</title><rect width="100%" height="100%" fill="#868e96"/><text x="50%" y="50%" fill="#dee2e6" dy=".3em">Image cap</text></svg>

          <div class="card-body">
            <h5 class="card-title">Card title</h5>
            <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</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-4">
        <div class="card bg-primary text-white text-center p-3">
          <figure class="mb-0">
            <blockquote class="blockquote">
              <p>A well-known quote, contained in a blockquote element.</p>
            </blockquote>
            <figcaption class="blockquote-footer mb-0 text-white">
              Someone famous in <cite title="Source Title">Source Title</cite>
            </figcaption>
          </figure>
        </div>
      </div>
      <div class="col-sm-6 col-lg-4 mb-4">
        <div class="card text-center">
          <div class="card-body">
            <h5 class="card-title">Card title</h5>
            <p class="card-text">This card has a regular title and short paragraph of text below it.</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-4">
        <div class="card">
          <svg class="bd-placeholder-img card-img" width="100%" height="260" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: Card image" preserveAspectRatio="xMidYMid slice" focusable="false"><title>Placeholder</title><rect width="100%" height="100%" fill="#868e96"/><text x="50%" y="50%" fill="#dee2e6" dy=".3em">Card image</text></svg>

        </div>
      </div>
      <div class="col-sm-6 col-lg-4 mb-4">
        <div class="card p-3 text-end">
          <figure class="mb-0">
            <blockquote class="blockquote">
              <p>A well-known quote, contained in a blockquote element.</p>
            </blockquote>
            <figcaption class="blockquote-footer mb-0 text-muted">
              Someone famous in <cite title="Source Title">Source Title</cite>
            </figcaption>
          </figure>
        </div>
      </div>
      <div class="col-sm-6 col-lg-4 mb-4">
        <div class="card">
          <div class="card-body">
            <h5 class="card-title">Card title</h5>
            <p class="card-text">This is another card with title and supporting text below. This card has some additional content to make it slightly taller overall.</p>
            <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
          </div>
        </div>
      </div>
    </div>

  </main>
</body>

</html>