Bootstrap4 卡片

2018-05-29 14:35 更新

卡片是一個(gè)靈活的、可擴(kuò)展的內(nèi)容窗口。它包含了可選的卡片頭和卡片腳、一個(gè)大范圍的內(nèi)容、上下文背景色以及強(qiáng)大的顯示選項(xiàng)。


創(chuàng)建卡片

我們可以通過(guò) Bootstrap4 的 .card 與 .card-body 類來(lái)創(chuàng)建一個(gè)簡(jiǎn)單的卡片,實(shí)例如下:

<div class="card">

  <div class="card-body">簡(jiǎn)單的卡片</div>

</div>


頭部和底部

.card-header類用于創(chuàng)建卡片的頭部樣式, .card-footer 類用于創(chuàng)建卡片的底部樣式:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap4 實(shí)例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank" >
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
  <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
  <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>
<div class="container">
  <h2>卡片頭部和底部</h2>
  <div class="card">
    <div class="card-header">頭部</div>
    <div class="card-body">內(nèi)容</div> 
    <div class="card-footer">底部</div>
  </div>
</div>
</body>
</html>


多種顏色卡片

Bootstrap 4 提供了多種卡片的背景顏色類: .bg-primary, .bg-success, .bg-info, .bg-warning, .bg-danger, .bg-secondary, .bg-dark 和 .bg-light。

實(shí)例:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap4 實(shí)例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank" >
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
  <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
  <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>
<div class="container">
  <h2>多種顏色卡片</h2>
  <div class="card">
    <div class="card-body">Basic card</div>
  </div>
  <br>
  <div class="card bg-primary text-white">
    <div class="card-body">Primary card</div>
  </div>
  <br>
  <div class="card bg-success text-white">
    <div class="card-body">Success card</div>
  </div>
  <br>
  <div class="card bg-info text-white">
    <div class="card-body">Info card</div>
  </div>
  <br>
  <div class="card bg-warning text-white">
    <div class="card-body">Warning card</div>
  </div>
  <br>
  <div class="card bg-danger text-white">
    <div class="card-body">Danger card</div>
  </div>
  <br>
  <div class="card bg-secondary text-white">
    <div class="card-body">Secondary card</div>
  </div>
  <br>
  <div class="card bg-dark text-white">
    <div class="card-body">Dark card</div>
  </div>
  <br>
  <div class="card bg-light text-dark">
    <div class="card-body">Light card</div>
  </div>
</div>
</body>
</html>


標(biāo)題、文本和鏈接

我們可以在頭部元素上使用 .card-title 類來(lái)設(shè)置卡片的標(biāo)題 。 .card-text 類用于設(shè)置卡片正文的內(nèi)容。 .card-link 類用于給鏈接設(shè)置顏色。

實(shí)例:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap4 實(shí)例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank" >
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
  <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
  <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>
<div class="container">
  <h2>標(biāo)題、文本和鏈接</h2>
  <div class="card">
    <div class="card-body">
      <h4 class="card-title">卡片標(biāo)題</h4>
      <p class="card-text">卡片內(nèi)容。</p>
      <a href="#" class="card-link">卡片鏈接</a>
      <a href="#" class="card-link">卡片鏈接</a>
    </div>
  </div>
</div>
</body>
</html>


圖片卡片

我們可以給 <img> 添加 .card-img-top(圖片在文字上方) 或 .card-img-bottom(圖片在文字下方 來(lái)設(shè)置圖片卡片:

實(shí)例:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap4 實(shí)例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank" >
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
  <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
  <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>
<div class="container">
  <h2>圖片卡片</h2>
  <p>圖片在頭部 (card-img-top):</p>
  <div class="card" style="width:400px">
    <img class="card-img-top" src="http://m.hgci.cn/attachments/image/20180524/1527144620597215.png" alt="Card image" style="width:100%">
    <div class="card-body">
      <h4 class="card-title">bootstrap4</h4>
      <p class="card-text">Some example text some example text. John Doe is an architect and engineer</p>
      <a href="#" class="btn btn-primary">See Profile</a>
    </div>
  </div>
  <br>
  <p>圖片在底部(card-img-bottom):</p>
  <div class="card" style="width:400px">
    <div class="card-body">
      <h4 class="card-title">bootstrap4</h4>
      <p class="card-text">Some example text some example text. Jane Doe is an architect and engineer</p>
      <a href="#" class="btn btn-primary">See Profile</a>
    </div>
    <img class="card-img-bottom" src="http://m.hgci.cn/attachments/image/20180524/1527144620597215.png" alt="Card image" style="width:100%">
  </div>
</div>
</body>
</html>

如果圖片要設(shè)置為背景,可以使用 .card-img-overlay 類:

實(shí)例:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap4 實(shí)例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank" >
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
  <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
  <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>
<div class="container">
  <h2>文字覆蓋圖片</h2>
  <p>如果圖片要設(shè)置為背景,可以使用 .card-img-overlay 類:</p>
  <div class="card img-fluid" style="width:500px">
    <img class="card-img-top" src="https://atts.w3cschool.cn/attachments/knowledge/201804/30601.png" alt="Card image" style="width:100%">
    <div class="card-img-overlay">
      <h4 class="card-title">W3Cschool</h4>
      <p class="card-text">Some example text some example text. Some example text some example text. Some example text some example text. Some example text some example text.</p>
      <a href="#" class="btn btn-primary">See Profile</a>
    </div>
  </div>
</div>
</body>
</html>


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)