在Bootstrap中创建响应式卡片组

时间:2019-07-25 01:06:47

标签: css twitter-bootstrap bootstrap-4

我正在尝试在Bootstrap中创建一个响应式卡片组。卡片机应在移动设备上每行显示一张卡,在平板电脑上每行显示2张卡,在台式机上每行显示3张卡。

下面是我的代码-

<div class="container">
<div class="card-deck">
    <div class="card mb-4">
        <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap">
        <div class="card-body">
            <h4 class="card-title">1 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 mb-4">
        <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap">
        <div class="card-body">
            <h4 class="card-title">2 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="w-100 d-none d-sm-block d-md-none"><!-- wrap every 2 on sm--></div>
    <div class="card mb-4">
        <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap">
        <div class="card-body">
            <h4 class="card-title">3 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="w-100 d-none d-md-block d-lg-none"><!-- wrap every 3 on md--></div>  
</div>

平板电脑部分似乎无法正常工作。帮助吗?

2 个答案:

答案 0 :(得分:0)

尝试此代码,但首先在页面下载链接中继承Bootstrap v4.1.3 https://getbootstrap.com/docs/4.1/getting-started/download/

INACTIVE

答案 1 :(得分:0)

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-6 col-lg-4 col-12">
            <div class="card-deck">
                <div class="card mb-4">
                    <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap">
                    <div class="card-body">
                    <h4 class="card-title">1 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>
        <div class="col-md-6 col-lg-4 col-12">
            <div class="card-deck">
                <div class="card mb-4">
                    <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap">
                    <div class="card-body">
                    <h4 class="card-title">1 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>
        <div class="col-md-6 col-lg-4 col-12">
            <div class="card-deck">
                <div class="card mb-4">
                    <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap">
                    <div class="card-body">
                    <h4 class="card-title">1 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>
    

        <div class="col-lg-4 col-md-6 col-12">
            <div class="card-deck">
                <div class="card mb-4">
                    <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap">
                    <div class="card-body">
                    <h4 class="card-title">1 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>
        <div class="col-lg-4 col-md-6 col-12">
            <div class="card-deck">
                <div class="card mb-4">
                    <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap">
                    <div class="card-body">
                    <h4 class="card-title">1 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>
        <div class="col-lg-4 col-md-6 col-12">
            <div class="card-deck">
                <div class="card mb-4">
                    <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap">
                    <div class="card-body">
                    <h4 class="card-title">1 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>
    </div>
</div>
</body>
</html>

希望它会为您提供帮助

供参考,请通过链接 https://getbootstrap.com/docs/4.0/layout/grid/