Home > Article > Web Front-end > An in-depth explanation of how to use the Card component in Bootstrap
How to use Card component in Bootstrap? The following article will explain how to use the Bootstrap5 Card card component through code examples. I hope it will be helpful to you!
Bootstrap cards provide a flexible, scalable content container with multiple variations and options. Cards are designed to use as little markup and styling as possible so they can be easily aligned and blend well with other Bootstrap components. It has no margin by default, so you can add the spacing general category as needed. [Related recommendations: "bootstrap Tutorial"]
The following is an example of a basic card with mixed content and fixed width. Cards that do not have a fixed width will naturally fill the full width of their parent element. This can be easily adjusted with our various scaling options. Below is a simple example.
<div class="card" style="width: 18rem;"> <img src="../pic/5.jpg" class="card-img-top" alt="卡片的图片宽度会自动设置"> <div class="card-body"> <h5 class="card-title">卡片标题</h5> <p class="card-text">卡片的内容可以使用文本、列表、表格、表单、图片等各种html元素</p> <a href="#" class="btn btn-primary">这是一个按钮</a> </div> </div>
Under normal circumstances, we do not need a complete card structure It is a good idea to use a card component. As shown in the example above, a complete card includes five parts: header, footer, picture (here refers to the picture under the header, which is the same width as the card), body, and list group. , where the body can contain titles and text. The following is a comprehensive example. In fact, under normal circumstances, it is enough to use one or two elements of the card as needed. The following example is only a demonstration. We will explain each part of the function one by one later.
<div class="card" style="width: 18rem;"> <div class="card-header"> 卡片页眉 </div> <img src="../pic/5.jpg" class="card-img-top" alt="卡片的图片宽度会自动设置"> <div class="card-body"> <h5 class="card-title">卡片标题</h5> <p class="card-text">卡片的内容可以使用文本、列表、表格、表单、图片等各种html元素 <img src="../pic/7.jpg" class="card-img-top" alt="卡片主体内的图片"> </p> <a href="#" class="btn btn-primary">这是一个按钮</a> </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-footer text-muted"> 卡片页脚 </div> </div>
Header and footer are not necessary, compared with other content, header and footer The text is lighter in color and the font is slightly smaller. In addition, its alignment can be adjusted through general classes, and many special effects can be achieved through flexible use.
<div class="card"> <h5 class="card-header">通知</h5> <div class="card-body"> <h5 class="card-title">中秋节放假通知</h5> <p class="card-text">下周中秋节放假7天,给大家乐活以下.........</p> <a href="#" class="btn btn-primary">了解详情</a> </div> </div>
<div class="card"> <div class="card-header"> 名言 </div> <div class="card-body"> <blockquote class="blockquote mb-0"> <p>只有诗人同圣徒才会坚信,在沥青路面上辛勤浇水会培植出百合花来。</p> <footer class="blockquote-footer">威廉· 萨默赛特·毛姆的 <cite title="Source Title">《月亮和六便士》</cite></footer> </blockquote> </div> </div>
<div class="card text-center"> <div class="card-header"> 名著推荐 </div> <div class="card-body"> <h5 class="card-title">《红楼梦》</h5> <p class="card-text"> 《红楼梦》是一部具有世界影响力的人情小说作品,举世公认的中国古典小说巅峰之作, 中国封建社会的百科全书,传统文化的集大成者。小说以贾、史、王、薛四大家族的兴衰为背景, 以贾府的家庭琐事、闺阁闲情为脉络,以贾宝玉、林黛玉、薛宝钗的爱情婚姻故事为主线, 刻画了以贾宝玉和金陵十二钗为中心的正邪两赋有情人的人性美和悲剧美。 通过家族悲剧、女儿悲剧及主人公的人生悲剧,揭示出封建末世危机。 </p> <a href="#" class="btn btn-primary">马上阅读</a> </div> <div class="card-footer text-muted"> 2 days ago </div> </div>
The image is set to be displayed at the top or bottom of the card through the class card-img-top or card-img-bottom. Below are two cards, one picture on top and one picture on the bottom.
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>卡片</title> </head> <body> <div> <br><br><br> <div class="card mb-2"> <img src="../pic/5.jpg" alt="..."> <div> <h5>软发布 Win11 </h5> <p>T 微软已宣布将于 6 月 24 日举行“What's next for Windows”活动,届时将发布下一代桌面操作系统 Win11 及其应用商店等内容。 </p> <p><small>Last updated 3 mins ago</small></p> </div> </div> <div> <div> <h5>飞利浦推出4K显示器</h5> <p>飞利浦推出 Momentum 4K 电竞显示器:专为 Xbox 设计,售价过万 </p> <p><small>Last updated 3 mins ago</small></p> </div> <img src="../pic/7.jpg" alt="..."> </div> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> </body> </html>
Convert the image to the card background and overlay the card’s text. Depending on the image, you can choose if you want additional styles or generic categories.
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>卡片</title> </head> <body> <div> <br><br><br> <div class="card card bg-dark text-white"> <img src="../pic/7.jpg" alt="..."> <div class="card-body card-img-overlay"> <h5>微软发布 Win11 </h5> <p> 微软已宣布将于 6 月 24 日举行“What's next for Windows”活动,届时将发布下一代桌面操作系统 Win11 及其应用商店等内容。 </p> <p><small>Last updated 3 mins ago</small></p> </div> </div> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> </body> </html>
Note that we added a black background to the card through bg-dark, set the font to white with text-white, and set card-img-overlay as the background color in card-body. You can also use card-img-overlay directly without card-body as above.
Use grid and A general class that allows cards to render horizontally in a responsive manner. In the example below, we use g-0 to remove the gaps in the grid, and use the col-md-*
class to make the card appear horizontally. Renders horizontally after the md breakpoint. Depending on the card content, some additional adjustments may be required.
<div class="card mb-3" style="max-width: 540px;"> <div class="row g-0"> <div class="col-md-4"> <img src="../pic/3.jpg" alt="..." class="w-100"> </div> <div class="col-md-8"> <div class="card-body"> <h5 class="card-title">春夏秋冬皆自然</h5> <p class="card-text"> 春夏秋冬皆自然,明月清风共我闲,有春风拂面的温柔,也有美好故事的发生,又一个春天如约而至,温柔的朝阳里布谷鸟声声呼唤,愿普天同安康,迎接春天到来的简... </p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> </div> </div>
Of course, you'd better set the image width, otherwise overflow will easily occur .
The building block of the card is .card-body. Use it when you need padding in the card it.
<h></h>
标签中添加card-title
以使用卡片标题。<h></h>
标签中加入card-subtitle
以使用副标题。如果card-title以及card-subtitle元件被放在card-body里面,它们将会对齐良好。
文本内容可以放置任何html元素和bootstrap的组件,如按钮等,在a标签中加入card-link
并使连接彼此相邻。
<div class="card" style="width: 18rem;"> <div class="card-body"> <h5 class="card-title">微软发布 Win11</h5> <h6 class="card-subtitle mb-2 text-muted">科技动态</h6> <p class="card-text"> 微软已宣布将于 6 月 24 日举行“What's next for Windows”活动,届时将发布下一代桌面操作系统 Win11 及其应用商店等内容。 </p> <a href="#" class="card-link">了解详情</a> <a href="#" class="card-link">下载试用</a> </div> </div>
列表群组使用非常简单
<div class="card" style="width: 18rem;"> <div class="card-header text-center"> 晚会活动 </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>
混合并搭配多个内容形式,用来创建你所需要的卡片。以下示例将图片样式、块、文字样式以及列表群组全部包装在一个固定宽度的卡片中。
<div class="card" style="width: 18rem;"> <img src="../pic/cap.jpg" class="card-img-top" alt="..."> <div class="card-body"> <h5 class="card-title">Card title</h5> <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p> </div> <ul class="list-group list-group-flush"> <li class="list-group-item">An item</li> <li class="list-group-item">A second item</li> <li class="list-group-item">A third item</li> </ul> <div class="card-body"> <a href="#" class="card-link">Card link</a> <a href="#" class="card-link">Another link</a> </div> </div>
卡片预设没有特定的起始width,除非另有声明,否则它们的宽度将是100%。
使用网格,根据需要在列和行中包装卡片。需要注意的是卡片默认是每个卡片一行的,要想一行中显示多个卡片,必须把几个卡片放在一个div容器中且每行中的卡片总宽度不能超过屏幕宽度。网格中的row-cols-*
同样适用卡片。另外卡片默认是宽度相同,高度根据内容自动调整的,但是如果卡片拥有页脚,则高度会自动调整到相同。
下面给出一个完整例子。
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>卡片</title> </head> <body> <div> <br><br><br> <div> <div> <div> <div> <h5>微软发布 Win11</h5> <p> 软已宣布将于 6 月 24 日举行“What's next for Windows”活动,届时将发布下一代桌面操作系统 Win11 及其应用商店等内容。 </p> <a href="#" class="btn btn-primary">了解详情</a> </div> </div> </div> <div> <div> <div> <h5>飞利浦推出4K显示器</h5> <p> 飞利浦推出 Momentum 4K 电竞显示器:专为 Xbox 设计,售价过万 </p> <a href="#" class="btn btn-primary">了解详情</a> </div> </div> </div> </div> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> </body> </html>
使用可调整宽度大小的通用类来快速设置卡片的宽度。
使用网格
使用网格,根据需要在列和行中包装卡片。
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>卡片</title> </head> <body> <div> <br><br><br> <div class="card w-75"> <div> <h5>微软发布 Win11</h5> <p> 软已宣布将于 6 月 24 日举行“What's next for Windows”活动,届时将发布下一代桌面操作系统 Win11 及其应用商店等内容。 </p> <a href="#" class="btn btn-primary">了解详情</a> </div> </div> <div class="card w-50"> <div> <h5>飞利浦推出4K显示器</h5> <p> 飞利浦推出 Momentum 4K 电竞显示器:专为 Xbox 设计,售价过万 </p> <a href="#" class="btn btn-primary">了解详情</a> </div> </div> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> </body> </html>
需要注意的是,这里的宽度值必须是预定的,预设包含25%,50%,75%,100%和auto,而不是随意写的。 详情见 Bootstrap5中文手册 通用类 尺寸(Sizing)一节。
在样式表中使用自定义的CSS或使用行内样式设置宽度,这种方式比较灵活,可以使用rem、px、百分比。
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>卡片</title> </head> <body> <div> <br><br><br> <div style="width: 30rem;"> <div> <h5>微软发布 Win11</h5> <p> 软已宣布将于 6 月 24 日举行“What's next for Windows”活动,届时将发布下一代桌面操作系统 Win11 及其应用商店等内容。 </p> <a href="#" class="btn btn-primary">了解详情</a> </div> </div> <div style="width: 20rem;"> <div> <h5>飞利浦推出4K显示器</h5> <p> 飞利浦推出 Momentum 4K 电竞显示器:专为 Xbox 设计,售价过万 </p> <a href="#" class="btn btn-primary">了解详情</a> </div> </div> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> </body> </html>
可以透过文字对齐类别更改任何卡片的整体或特定部分的文本对齐方式。默认全部是局左(text-start)对齐的,可以用card-text、text-end通用类使之居中对齐或居右对齐。通用类可用在card容器,也可以单独以用在标题、页眉页脚、主体等任意部分,如果同时使用,则单独的覆盖整体的。
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>卡片</title> </head> <body> <div> <br><br><br> <div> <div> <div class="card text-center"> <div> 整体居中 </div> <div> <h5>微软发布 Win11</h5> <p> 软已宣布将于 6 月 24 日举行“What's next for Windows”活动,届时将发布下一代桌面操作系统 Win11 及其应用商店等内容。 </p> <a href="#" class="btn btn-primary">了解详情</a> </div> </div> </div> <div> <div> <div class="card-header text-center""> 页眉、标题居中 </div> <div> <h5 class="card-title text-center">微软发布 Win11</h5> <p> 软已宣布将于 6 月 24 日举行“What's next for Windows”活动,届时将发布下一代桌面操作系统 Win11 及其应用商店等内容。 </p> <a href="#" class="btn btn-primary">了解详情</a> </div> </div> </div> <div> <div class="card text-center"> <div> 整体居中中标题居左 </div> <div> <h5 class="card-title text-start">微软发布 Win11</h5> <p> 软已宣布将于 6 月 24 日举行“What's next for Windows”活动,届时将发布下一代桌面操作系统 Win11 及其应用商店等内容。 </p> <a href="#" class="btn btn-primary">了解详情</a> </div> </div> </div> </div> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> </body> </html>
使用Bootstrap的导航组件在卡片的页首(或块)添加一些导航,获得选项卡效果。
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>卡片</title> </head> <body> <div> <br><br><br> <div class="card text-center"> <div> <ul class="nav nav-tabs card-header-tabs"> <li> <a aria-current="true" href="#" data-bs-toggle="tab" data-bs-target="#nav-hot">热门新闻</a> </li> <li> <a class="nav-link active" href="#" data-bs-toggle="tab" data-bs-target="#nav-home">国内新闻</a> </li> <li> <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">国外新闻</a> </li> </ul> </div> <div> <div class="card-body tab-pane fade" id="nav-hot"> <h5>热点新闻</h5> <p>热点新闻</p> <a href="#" class="btn btn-primary">Go somewhere</a> </div> <div class="card-body tab-pane fade show active" id="nav-home"> <h5>国内新闻</h5> <p>国内热点新闻</p> <a href="#" class="btn btn-primary">Go somewhere</a> </div> </div> </div> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> </body> </html>
对于上面的代码,只需经过两处替换,就可以换为胶囊导航
<ul class="nav nav-tabs card-header-tabs">
替换为
<ul class="nav nav-pills card-header-pills">
所有的
data-bs-toggle="tab"
替换为
data-bs-toggle="pill"
卡片包含许多用来自定义背景、边框、颜色的选项。
使用文字和背景通用类别来改变卡片的外观。
<!doctype html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>卡片</title> </head> <body> <div class="container"> <br><br><br> <div class="d-flex"> <div class="card text-white bg-primary" style="max-width: 18rem;"> <div class="card-header">Header</div> <div class="card-body"> <h5 class="card-title">Primary</h5> <p class="card-text">颜色直接作用于card容器上</p> </div> </div> <div class="card text-white bg-secondary" style="max-width: 18rem;"> <div class="card-header">Header</div> <div class="card-body"> <h5 class="card-title">Secondary</h5> <p class="card-text">颜色直接作用于card容器上</p> </div> </div> <div class="card text-white bg-success" style="max-width: 18rem;"> <div class="card-header">Header</div> <div class="card-body"> <h5 class="card-title">Success</h5> <p class="card-text">颜色直接作用于card容器上</p> </div> </div> <div class="card text-white bg-danger" style="max-width: 18rem;"> <div class="card-header">Header</div> <div class="card-body"> <h5 class="card-title">Danger</h5> <p class="card-text">颜色直接作用于card容器上</p> </div> </div> </div> <div class="d-flex"> <div class="card text-dark bg-warning" style="max-width: 18rem;"> <div class="card-header">Header</div> <div class="card-body"> <h5 class="card-title">Warning</h5> <p class="card-text">颜色直接作用于card容器上</p> </div> </div> <div class="card text-dark bg-info" style="max-width: 18rem;"> <div class="card-header">Header</div> <div class="card-body"> <h5 class="card-title">Info</h5> <p class="card-text">颜色直接作用于card容器上</p> </div> </div> <div class="card text-dark bg-light" style="max-width: 18rem;"> <div class="card-header">Header</div> <div class="card-body"> <h5 class="card-title">Light</h5> <p class="card-text">颜色直接作用于card容器上</p> </div> </div> <div class="card text-white bg-dark" style="max-width: 18rem;"> <div class="card-header">Header</div> <div class="card-body"> <h5 class="card-title">Dark</h5> <p class="card-text">颜色直接作用于card容器上</p> </div> </div> </div> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> </body> </html>
<div class="d-flex"> <div class="card border-primary mb-3" style="max-width: 18rem;"> <div class="card-header">页眉</div> <div class="card-body text-primary"> <h5 class="card-title">Primary 卡片标题</h5> <p class="card-text">card-text 卡片文字内容</p> </div> </div> <div class="card border-secondary mb-3" style="max-width: 18rem;"> <div class="card-header">页眉</div> <div class="card-body text-secondary"> <h5 class="card-title">Secondary 卡片标题</h5> <p class="card-text">card-text 卡片文字内容</p> </div> </div> <div class="card border-success mb-3" style="max-width: 18rem;"> <div class="card-header">页眉</div> <div class="card-body text-success"> <h5 class="card-title">Success 卡片标题</h5> <p class="card-text">card-text 卡片文字内容</p> </div> </div> <div class="card border-danger mb-3" style="max-width: 18rem;"> <div class="card-header">页眉</div> <div class="card-body text-danger"> <h5 class="card-title">Danger 卡片标题</h5> <p class="card-text">card-text 卡片文字内容</p> </div> </div> </div> <div class="d-flex"> <div class="card border-warning mb-3" style="max-width: 18rem;"> <div class="card-header">页眉</div> <div class="card-body"> <h5 class="card-title">Warning 卡片标题</h5> <p class="card-text">card-text 卡片文字内容</p> </div> </div> <div class="card border-info mb-3" style="max-width: 18rem;"> <div class="card-header">页眉</div> <div class="card-body"> <h5 class="card-title">Info 卡片标题</h5> <p class="card-text">card-text 卡片文字内容</p> </div> </div> <div class="card border-light mb-3" style="max-width: 18rem;"> <div class="card-header">页眉</div> <div class="card-body"> <h5 class="card-title">Light 卡片标题</h5> <p class="card-text">card-text 卡片文字内容</p> </div> </div> <div class="card border-dark mb-3" style="max-width: 18rem;"> <div class="card-header">页眉</div> <div class="card-body text-dark"> <h5 class="card-title">Dark 卡片标题</h5> <p class="card-text">card-text 卡片文字内容</p> </div> </div> </div>
限于篇幅,仅展示前面一部分边框图片。
也可以依照需求更改卡片页首、页尾的边框,并能使用bg-transparent来移除它们的background-color。
<div class="card border-success mb-3" style="max-width: 26rem;"> <div class="card-header bg-transparent border-success">页眉</div> <div class="card-body text-success"> <h5 class="card-title">Success卡片标题</h5> <p class="card-text"> 注意card-body使用了 text-success",页脚使用了bg-transparent border-success </p> </div> <div class="card-footer bg-transparent border-success">页脚</div> </div>
更多关于bootstrap的相关知识,可访问:bootstrap基础教程!!
The above is the detailed content of An in-depth explanation of how to use the Card component in Bootstrap. For more information, please follow other related articles on the PHP Chinese website!