【问题标题】:How to wrap (float) cards in bootstrap 4如何在引导程序 4 中包装(浮动)卡片
【发布时间】:2022-01-22 06:19:26
【问题描述】:

我正在使用 bootstrap 4 beta。

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

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

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

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

我怎样才能做到这一点?

下面的例子显示了它们 3 对面,它们之间有很多空间。

 <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>

【问题讨论】:

  • 请分享您的代码?

标签: html twitter-bootstrap twitter4j


【解决方案1】:

在使用了一段时间后,我想出的浮动解决方案并不令人满意,因为在某些情况下,某些卡片下方有大量空间。在查看 bootstrap 4 卡片后,我发现了一个完全可以做到的功能我想要它:“卡片列”

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

Bootstrap Docs on card-columns

【讨论】:

  • 谢谢伙计。你在那里为我节省了大量时间。
  • 我发现卡片列的唯一问题是顺序。如果您在 3 列中有 15 张卡片,则页面顶部的卡片将类似于卡片 1、卡片 6 和卡片 11。不太好!
  • 感谢分享格雷格!我要头疼了
  • 这在 Bootstrap 5 中不起作用是吗?
【解决方案2】:

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

<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>

【讨论】:

    【解决方案3】:

    这里的解决方案... 使用引导行和列来实现这一点,请按照下面的代码。

    <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>
    

    【讨论】:

    • 我试过了,它把它们放在一个水平列中。
    • 从'card' div和行和col的标记中删除宽度
    【解决方案4】:

    根据 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】:

      在 5.1 版中,Bootstrap 已经远离了在 4 版中使用的 CSS 技术。Here 他们提供了一个如何使用 Masonry 插件的示例,请参阅这个 sn-p:

      <!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>

      【讨论】:

      • 有趣。这是我第一次听说 Masonry,但就像你说的,Bootstrap 文档显示它与 Bootstrap 一起使用。
      猜你喜欢
      • 2016-12-26
      • 1970-01-01
      • 2020-05-16
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 2020-04-01
      • 2021-06-23
      相关资源
      最近更新 更多