Bootstrap4 卡片组件

卡片组件的重要性

卡片是Bootstrap 4中最重要的组件之一,用于:

  • 展示各种类型的内容(文本、图片、链接等)
  • 创建灵活的布局和内容容器
  • 替代Bootstrap 3中的面板、缩略图和wells
  • 提供一致的视觉设计

基本卡片

Bootstrap 4 提供了简单易用的卡片组件,使用 .card 类创建卡片容器。

基本卡片示例
简单卡片
卡片标题
卡片副标题

这是一个简单卡片的示例文本。卡片是Bootstrap 4中用于显示内容的灵活容器。

卡片链接 另一个链接
带图片的卡片
卡片图片
带图片的卡片

这是一个带有图片的卡片示例。图片位于卡片顶部。

了解更多
<!-- 简单卡片 -->
<div class="card" style="width: 18rem;">
  <div class="card-body">
    <h5 class="card-title">卡片标题</h5>
    <h6 class="card-subtitle mb-2 text-muted">卡片副标题</h6>
    <p class="card-text">这是一个简单卡片的示例文本。</p>
    <a href="#" class="card-link">卡片链接</a>
    <a href="#" class="card-link">另一个链接</a>
  </div>
</div>

<!-- 带图片的卡片 -->
<div class="card" style="width: 18rem;">
  <img src="image.jpg" class="card-img-top" alt="卡片图片">
  <div class="card-body">
    <h5 class="card-title">带图片的卡片</h5>
    <p class="card-text">这是一个带有图片的卡片示例。</p>
    <a href="#" class="btn btn-primary">了解更多</a>
  </div>
</div>

卡片内容类型

卡片可以包含多种类型的内容,包括标题、文本、图片、列表组等。

卡片内容类型示例
带列表组的卡片
功能列表
  • 功能一
  • 功能二
  • 功能三
带页眉页脚的卡片
特色
特别优惠

享受我们的特别优惠,限时有效。

立即购买
图片覆盖卡片
卡片图片
图片覆盖标题

这是一个图片覆盖卡片的示例文本。

最后更新 3 分钟前

<!-- 带列表组的卡片 -->
<div class="card" style="width: 18rem;">
  <div class="card-header">
    功能列表
  </div>
  <ul class="list-group list-group-flush">
    <li class="list-group-item">功能一</li>
    <li class="list-group-item">功能二</li>
    <li class="list-group-item">功能三</li>
  </ul>
  <div class="card-body">
    <a href="#" class="card-link">了解更多</a>
  </div>
</div>

<!-- 图片覆盖卡片 -->
<div class="card text-white" style="width: 18rem;">
  <img src="image.jpg" class="card-img" alt="卡片图片">
  <div class="card-img-overlay">
    <h5 class="card-title">图片覆盖标题</h5>
    <p class="card-text">这是一个图片覆盖卡片的示例文本。</p>
    <p class="card-text">最后更新 3 分钟前</p>
  </div>
</div>

卡片样式

Bootstrap 4 提供了多种卡片样式,包括背景颜色、边框颜色等。

卡片样式示例
Header
Primary card title

Some quick example text to build on the card title and make up the bulk of the card's content.

Header
Secondary card title

Some quick example text to build on the card title and make up the bulk of the card's content.

Header
Success card title

Some quick example text to build on the card title and make up the bulk of the card's content.

Header
Danger card title

Some quick example text to build on the card title and make up the bulk of the card's content.

Header
Warning card title

Some quick example text to build on the card title and make up the bulk of the card's content.

Header
Info card title

Some quick example text to build on the card title and make up the bulk of the card's content.

<!-- 主要卡片 -->
<div class="card text-white bg-primary mb-3" style="max-width: 18rem;">
  <div class="card-header">Header</div>
  <div class="card-body">
    <h5 class="card-title">Primary card title</h5>
    <p class="card-text">Some quick example text...</p>
  </div>
</div>

<!-- 成功卡片 -->
<div class="card text-white bg-success mb-3" style="max-width: 18rem;">
  <div class="card-header">Header</div>
  <div class="card-body">
    <h5 class="card-title">Success card title</h5>
    <p class="card-text">Some quick example text...</p>
  </div>
</div>

卡片布局

卡片可以使用Bootstrap的网格系统进行布局,也可以使用卡片组、卡片列等特殊布局。

卡片布局示例
卡片组
...
卡片标题

这是一个更宽的卡片,支持更长的文本内容。

最后更新 3 分钟前

...
卡片标题

这个卡片有更长的文本内容。

最后更新 3 分钟前

...
卡片标题

这是另一个卡片,具有相同的宽度和高度。

最后更新 3 分钟前

卡片列
...
卡片标题较长,可能会换行

这是一个较长的卡片,具有更长的文本内容。

这是一个引用卡片,用于显示引用内容。

著名人物在 来源标题
...
卡片标题

这个卡片有更长的文本内容。

最后更新 3 分钟前

具有背景色的引用卡片。

某人著名在 来源标题
卡片标题

这个卡片有居中的文本内容。

最后更新 3 分钟前

...

具有右对齐文本的引用卡片。

著名人物在 来源标题
卡片标题

这是另一个卡片,具有相同的内容。

最后更新 3 分钟前

<!-- 卡片组 -->
<div class="card-group">
  <div class="card">
    <img src="..." class="card-img-top" alt="...">
    <div class="card-body">
      <h5 class="card-title">卡片标题</h5>
      <p class="card-text">卡片内容...</p>
    </div>
  </div>
  <div class="card">
    <img src="..." class="card-img-top" alt="...">
    <div class="card-body">
      <h5 class="card-title">卡片标题</h5>
      <p class="card-text">卡片内容...</p>
    </div>
  </div>
</div>

<!-- 卡片列 -->
<div class="card-columns">
  <div class="card">
    <img src="..." class="card-img-top" alt="...">
    <div class="card-body">
      <h5 class="card-title">卡片标题</h5>
      <p class="card-text">卡片内容...</p>
    </div>
  </div>
  <!-- 更多卡片 -->
</div>

自定义卡片样式

通过自定义CSS,您可以创建独特的卡片样式,以适应您的设计需求。

自定义卡片样式示例
渐变背景卡片
渐变卡片

这是一个具有渐变背景的自定义卡片。

了解更多
悬停效果卡片
卡片图片
悬停效果卡片

这是一个具有悬停效果的自定义卡片。

了解更多
水平卡片
...
水平卡片

这是一个水平布局的自定义卡片。

最后更新 3 分钟前

<!-- 渐变背景卡片 -->
<style>
.card-gradient {
  background: linear-gradient(135deg, #6f42c1, #e83e8c);
  color: white;
}
</style>

<div class="card card-gradient text-white" style="width: 18rem;">
  <div class="card-body">
    <h5 class="card-title">渐变卡片</h5>
    <p class="card-text">这是一个具有渐变背景的自定义卡片。</p>
    <a href="#" class="btn btn-light">了解更多</a>
  </div>
</div>

<!-- 水平卡片 -->
<div class="card card-horizontal" style="max-width: 540px;">
  <div class="row no-gutters">
    <div class="col-md-4">
      <img src="..." class="card-img" alt="...">
    </div>
    <div class="col-md-8">
      <div class="card-body">
        <h5 class="card-title">水平卡片</h5>
        <p class="card-text">这是一个水平布局的自定义卡片。</p>
      </div>
    </div>
  </div>
</div>

最佳实践

使用卡片组件的建议
  • 保持一致性 - 在整个应用中使用一致的卡片样式
  • 合理使用图片 - 确保图片与卡片内容相关且质量良好
  • 优化内容层次 - 使用清晰的标题、副标题和文本层次
  • 响应式设计 - 确保卡片在不同设备上都能良好显示
  • 合理使用交互 - 为可点击的卡片添加适当的悬停和焦点状态

学习完成!

您已经学会了Bootstrap 4中的卡片组件使用方法。现在可以尝试在自己的项目中使用这些技术了。

继续学习下一章:Bootstrap 4 组件