概覽
卡片的建構成使用最少的標記和樣式,但仍然能提供多面向的控制和自訂功能。它們內建彈性盒狀模型,提供簡易的對齊功能,且能與其他元件完美結合。
一開始,<b-card>
沒有固定寬度,因此它們會自然填滿其父元素的整個寬度。可透過樣式或標準 Bootstrap v4 大小類別輕易自訂。
透過指定 tag
道具,將預設的根標籤 div
變更為任何其他 HTML 元素
<div>
<b-card
title="Card Title"
img-src="https://picsum.photos/600/300/?image=25"
img-alt="Image"
img-top
tag="article"
style="max-width: 20rem;"
class="mb-2"
>
<b-card-text>
Some quick example text to build on the card title and make up the bulk of the card's content.
</b-card-text>
<b-button href="#" variant="primary">Go somewhere</b-button>
</b-card>
</div>
內容類型
卡片支援各種廣泛的內容,包括圖片、文字、列表群組、連結等。以下列出 <b-card>
中支援的內容範例:
卡片內容
一個 <b-card>
的構建區塊是 <b-card-body>
區段,可以在卡片中提供一個填補的區段。
預設 <b-card>
內容會自動置入一個 <b-card-body>
區段中
<div>
<b-card class="text-center">
<div class="bg-secondary text-light">
This is some content within the default <samp><b-card-body></samp> block of the
<samp><b-card></samp> component. Notice the padding between the card's border and this
gray <samp><div></samp>.
</div>
</b-card>
</div>
若要停用自動 <b-card-body>
區段(及相關填補)請在 <b-card>
屬性中設定 no-body
。
<div>
<b-card no-body class="text-center">
<div class="bg-secondary text-light">
This is some content without the default <samp><b-card-body></samp> section. Notice the
lack of padding between the card's border and this gray <samp><div></samp>.
</div>
</b-card>
</div>
請注意當 no-body
啟用時,title
和 sub-title
屬性的內容將不會呈現。
使用 <b-card-body>
子元件,可以將您自己的卡片主體置於具有 no-body
屬性的 <b-card>
元件中的任何位置。
標題、文字及連結
透過 title
屬性增加卡片標題,而子標題則透過 sub-title
屬性增加。標題會使用子元件 <b-card-title>
呈現,而子標題會使用子元件 <b-card-sub-title>
呈現。
透過子元件 <b-card-text>
,可以增加段落文字至卡片中。卡片主體中的最後一個 <b-card-text>
會自動移除其底部空白(透過 CSS)。<b-card-text>
中的文字也可以運用標準 HTML 標籤調整樣式。
可以透過在 <a>
標記(或 <b-link>
元件)新增 .card-link
類別,來增加連結並將其並排置放。
<div>
<b-card title="Card title" sub-title="Card subtitle">
<b-card-text>
Some quick example text to build on the <em>card title</em> and make up the bulk of the card's
content.
</b-card-text>
<b-card-text>A second paragraph of text in the card.</b-card-text>
<a href="#" class="card-link">Card link</a>
<b-link href="#" class="card-link">Another link</b-link>
</b-card>
</div>
圖片
卡片的屬性 img-src
將會在卡片最上層放置一張圖片,並使用 img-alt
屬性指定一個字串,以放置於圖片的 alt
屬性中。由 img-src
屬性指定的圖片會是回應式的,並且會在卡片寬度改變時調整其寬度。
或者,您可以使用子元件 <b-card-img>
手動放置圖片於 <b-card>
中。詳見下方廚具水槽示範的使用方式。
<div>
<div>
<h4>Top and Bottom</h4>
<b-card-group deck>
<b-card img-src="https://placekitten.com/1000/300" img-alt="Card image" img-top>
<b-card-text>
Some quick example text to build on the card and make up the bulk of the card's content.
</b-card-text>
</b-card>
<b-card img-src="https://placekitten.com/1000/300" img-alt="Card image" img-bottom>
<b-card-text>
Some quick example text to build on the card and make up the bulk of the card's content.
</b-card-text>
</b-card>
</b-card-group>
</div>
<div class="mt-4">
<h4>Left and Right (or Start and End)</h4>
<b-card img-src="https://placekitten.com/300/300" img-alt="Card image" img-left class="mb-3">
<b-card-text>
Some quick example text to build on the card and make up the bulk of the card's content.
</b-card-text>
</b-card>
<b-card img-src="https://placekitten.com/300/300" img-alt="Card image" img-right>
<b-card-text>
Some quick example text to build on the card and make up the bulk of the card's content.
</b-card-text>
</b-card>
</div>
</div>
注意:左、右圖像可能需要針對 .card-img-left
和 .card-img-right
類別套用額外的樣式,原因為如果圖片短於內容時,圖片高度會「拉長」。針對左右對齊的圖像,不支援標頭和頁尾。您可能會發現水平卡配置這個範例對於建立橫向回應式卡片時更有彈性。
疊加圖像
將布林值屬性 overlay
設為背景中的圖像。
<div>
<b-card
overlay
img-src="https://picsum.photos/900/250/?image=3"
img-alt="Card Image"
text-variant="white"
title="Image Overlay"
sub-title="Subtitle"
>
<b-card-text>
Some quick example text to build on the card and make up the bulk of the card's content.
</b-card-text>
</b-card>
</div>
延遲載入的圖像
使用 <b-card-img-lazy>
子元件,可延遲載入捲動至顯示範圍內的圖像。 <b-card-img-lazy>
支援相同的 <b-card-img>
屬性,也支援許多 <b-img-lazy>
元件的屬性。
透過 header
/footer
屬性或命名區塊,將卡片內加入選用的標題或頁尾。您可以透過設定 header-tag
和 footer-tag
屬性來控制包裹區塊元件標籤的套用 (預設為 div
)
<div>
<b-card-group deck>
<b-card
header="featured"
header-tag="header"
footer="Card Footer"
footer-tag="footer"
title="Title"
>
<b-card-text>Header and footers using props.</b-card-text>
<b-button href="#" variant="primary">Go somewhere</b-button>
</b-card>
<b-card title="Title" header-tag="header" footer-tag="footer">
<template #header>
<h6 class="mb-0">Header Slot</h6>
</template>
<b-card-text>Header and footers using slots.</b-card-text>
<b-button href="#" variant="primary">Go somewhere</b-button>
<template #footer>
<em>Footer Slot</em>
</template>
</b-card>
</b-card-group>
</div>
混合式範例
混合搭配多種內容類型來建立所需的卡片,或是放進所有類型的內容。下方顯示圖片樣式、區塊、文字樣式和清單群組,全都包在固定寬度的卡片中。
<div>
<b-card
no-body
style="max-width: 20rem;"
img-src="https://placekitten.com/380/200"
img-alt="Image"
img-top
>
<template #header>
<h4 class="mb-0">Hello World</h4>
</template>
<b-card-body>
<b-card-title>Card Title</b-card-title>
<b-card-sub-title class="mb-2">Card Sub Title</b-card-sub-title>
<b-card-text>
Some quick example text to build on the card title and make up the bulk of the card's
content.
</b-card-text>
</b-card-body>
<b-list-group flush>
<b-list-group-item>Cras justo odio</b-list-group-item>
<b-list-group-item>Dapibus ac facilisis in</b-list-group-item>
<b-list-group-item>Vestibulum at eros</b-list-group-item>
</b-list-group>
<b-card-body>
<a href="#" class="card-link">Card link</a>
<a href="#" class="card-link">Another link</a>
</b-card-body>
<b-card-footer>This is a footer</b-card-footer>
<b-card-img src="https://placekitten.com/480/210" alt="Image" bottom></b-card-img>
</b-card>
</div>
水平卡片配置
透過結合網格元件、工具程式類別和個別卡片子元件,可以使卡片以對行動裝置友善且回應式的方式水平排列。
在以下的範例中,我們移除 <b-row>
中格狀欄位的空間,並在 <b-col>
中使用 md
屬性,在 md
斷點處讓卡片水平排列。rounded-0
類別會移除 <b-card-img>
角落的圓角,而 overflow-hidden
類別會根據卡片的邊框半徑對 <b-card>
適當地裁剪圖片的角落。可能需要根據卡片內容進行進一步的調整。
<div>
<b-card no-body class="overflow-hidden" style="max-width: 540px;">
<b-row no-gutters>
<b-col md="6">
<b-card-img src="https://picsum.photos/400/400/?image=20" alt="Image" class="rounded-0"></b-card-img>
</b-col>
<b-col md="6">
<b-card-body title="Horizontal Card">
<b-card-text>
This is a wider card with supporting text as a natural lead-in to additional content.
This content is a little bit longer.
</b-card-text>
</b-card-body>
</b-col>
</b-row>
</b-card>
</div>
文字變異
預設,卡片使用深色文字,假設為淺色背景。您可以透過屬性 text-variant
中調整文字的顏色,及卡片的子組成元素的顏色,來反轉上述設定。接著,指定深色背景的變異。
<b-card bg-variant="dark" text-variant="white" title="Card Title">
<b-card-text>
With supporting text below as a natural lead-in to additional content.
</b-card-text>
<b-button href="#" variant="primary">Go somewhere</b-button>
</b-card>
背景與邊框變異
卡片包括自己的變異樣式,可快速透過屬性 bg-variant
及 border-variant
變更卡片的背景顏色和邊框樣式。較深的實心變異可能需要設定屬性 text-variant
來調整文字顏色。
實心
<div>
<div>
<b-card-group deck>
<b-card bg-variant="primary" text-variant="white" header="Primary" class="text-center">
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
<b-card bg-variant="secondary" text-variant="white" header="Secondary" class="text-center">
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
<b-card bg-variant="success" text-variant="white" header="Success" class="text-center">
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
</b-card-group>
</div>
<div class="mt-3">
<b-card-group deck>
<b-card bg-variant="info" text-variant="white" header="Info" class="text-center">
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
<b-card bg-variant="warning" text-variant="white" header="Warning" class="text-center">
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
<b-card bg-variant="danger" text-variant="white" header="Danger" class="text-center">
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
</b-card-group>
</div>
<div class="mt-3">
<b-card-group deck>
<b-card bg-variant="light" header="Light" class="text-center">
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
<b-card bg-variant="dark" header="Dark" text-variant="white" class="text-center">
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
<b-card header="Default" class="text-center">
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
</b-card-group>
</div>
</div>
有邊框的
<div>
<div>
<b-card-group deck>
<b-card
border-variant="primary"
header="Primary"
header-bg-variant="primary"
header-text-variant="white"
align="center"
>
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
<b-card
border-variant="secondary"
header="Secondary"
header-border-variant="secondary"
align="center"
>
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
<b-card border-variant="success" header="Success" align="center">
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
</b-card-group>
</div>
<div class="mt-3">
<b-card-group deck>
<b-card border-variant="info" header="Info" align="center">
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
<b-card
border-variant="warning"
header="Warning"
header-bg-variant="transparent"
align="center"
>
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
<b-card
border-variant="danger"
header="Danger"
header-border-variant="danger"
header-text-variant="danger"
align="center"
>
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
</b-card-group>
</div>
<div class="mt-3">
<b-card-group deck class="mb-3">
<b-card border-variant="light" header="Light" class="text-center">
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
<b-card border-variant="dark" header="Dark" align="center">
<b-card-text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b-card-text>
</b-card>
</b-card-group>
</div>
</div>
變異到類別對應
BootstrapVue <b-card>
變異直接對應到 Bootstrap v4 卡片類別,在上述變異名稱前面附加 bg-
(實心) 或 border-
(有邊框)。
您也可以透過 header-bg-variant
、header-border-variant
、header-text-variant
、footer-bg-variant
、footer-border-variant
及 footer-text-variant
等屬性,將實心和有邊框的變異個別套用到卡片標題與頁尾。
<div>
<b-card
header="Card Header"
header-text-variant="white"
header-tag="header"
header-bg-variant="dark"
footer="Card Footer"
footer-tag="footer"
footer-bg-variant="success"
footer-border-variant="dark"
title="Title"
style="max-width: 20rem;"
>
<b-card-text>Header and footers variants.</b-card-text>
</b-card>
</div>
傳達輔助技術的涵義
使用顏色來增加涵義僅提供視覺指示,不會傳達給輔助技術的使用者,例如:螢幕閱讀器。請確定以顏色表示的資訊從內容本身(例如:可見文字)即可明顯看出,或透過其他方式包含在內,例如:使用 .sr-only
類別隱藏的其他文字。
導覽整合
輕鬆將 <b-nav>
整合到卡片標題中。
使用 header
插槽:
<div>
<b-card title="Card Title" body-class="text-center" header-tag="nav">
<template #header>
<b-nav card-header tabs>
<b-nav-item active>Active</b-nav-item>
<b-nav-item>Inactive</b-nav-item>
<b-nav-item disabled>Disabled</b-nav-item>
</b-nav>
</template>
<b-card-text>
With supporting text below as a natural lead-in to additional content.
</b-card-text>
<b-button variant="primary">Go somewhere</b-button>
</b-card>
</div>
使用 <b-card-header>
子組成元件
<div>
<b-card no-body>
<b-card-header header-tag="nav">
<b-nav card-header tabs>
<b-nav-item active>Active</b-nav-item>
<b-nav-item>Inactive</b-nav-item>
<b-nav-item disabled>Disabled</b-nav-item>
</b-nav>
</b-card-header>
<b-card-body class="text-center">
<b-card-title>Card Title</b-card-title>
<b-card-text>
With supporting text below as a natural lead-in to additional content.
</b-card-text>
<b-button variant="primary">Go somewhere</b-button>
</b-card-body>
</b-card>
</div>
有關在卡片標題中使用 <b-nav>
的更多資訊,請參閱 導覽文件。
卡片群組
除了為卡片中的內容設定樣式外,BootstrapVue 還包含一個 <b-card-group>
元件,用於配置一系列卡片的配置。目前,這些配置選項還沒有回應式。
預設卡片群組
使用卡片群組將卡片顯示為單一的附加元件,其寬度和高度的欄位相等。卡片群組使用 display: flex; 來達成其大小一致的效果。
在卡片群組中使用頁尾時,它們的內容將自動排列。
<div>
<b-card-group>
<b-card title="Title" img-src="https://placekitten.com/g/300/450" img-alt="Image" img-top>
<b-card-text>
This is a wider card with supporting text below as a natural lead-in to additional content.
This content is a little bit longer.
</b-card-text>
<template #footer>
<small class="text-muted">Last updated 3 mins ago</small>
</template>
</b-card>
<b-card title="Title" img-src="https://placekitten.com/g/300/450" img-alt="Image" img-top>
<b-card-text>
This card has supporting text below as a natural lead-in to additional content.
</b-card-text>
<template #footer>
<small class="text-muted">Last updated 3 mins ago</small>
</template>
</b-card>
<b-card title="Title" img-src="https://placekitten.com/g/300/450" img-alt="Image" img-top>
<b-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.
</b-card-text>
<template #footer>
<small class="text-muted">Last updated 3 mins ago</small>
</template>
</b-card>
</b-card-group>
</div>
卡片橫排群組
需要一系列寬度和高度相同的卡片,但彼此沒有連接?請使用 deck
屬性設定卡片橫排。如同一般卡片群組,橫排中的卡片頁尾也會自動對齊。
<div>
<b-card-group deck>
<b-card title="Title" img-src="https://picsum.photos/300/300/?image=41" img-alt="Image" img-top>
<b-card-text>
This is a wider card with supporting text below as a natural lead-in to additional content.
This content is a little bit longer.
</b-card-text>
<template #footer>
<small class="text-muted">Last updated 3 mins ago</small>
</template>
</b-card>
<b-card title="Title" img-src="https://picsum.photos/300/300/?image=41" img-alt="Image" img-top>
<b-card-text>
This card has supporting text below as a natural lead-in to additional content.
</b-card-text>
<template #footer>
<small class="text-muted">Last updated 3 mins ago</small>
</template>
</b-card>
<b-card title="Title" img-src="https://picsum.photos/300/300/?image=41" img-alt="Image" img-top>
<b-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.
</b-card-text>
<template #footer>
<small class="text-muted">Last updated 3 mins ago</small>
</template>
</b-card>
</b-card-group>
</div>
卡片欄位群組
卡片可以包覆在設定為有 columns
屬性的 <b-card-group>
中,並以類似 Masonry 的方式組織為欄位。卡片是使用 CSS 欄位屬性來建立,而非彈性框線,以便於對齊。卡片會從上到下、從左到右排列。
請注意!使用卡片欄位時,其效果可能有所不同。為防止卡片跨欄位斷裂,我們必須設定它們為 display: inline-block,因為 column-break-inside: avoid 尚未成為完美的解決方案。
<div>
<b-card-group columns>
<b-card
title="Card title that wraps to a new line"
img-src="https://placekitten.com/g/400/450"
img-alt="Image"
img-top
>
<b-card-text>
This is a wider card with supporting text below as a natural lead-in to additional content.
This content is a little bit longer.
</b-card-text>
</b-card>
<b-card header="Quote">
<blockquote class="blockquote mb-0">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p>
<footer class="blockquote-footer">
Someone famous in <cite title="Source Title">Source Title</cite>
</footer>
</blockquote>
</b-card>
<b-card title="Title" img-src="https://placekitten.com/500/350" img-alt="Image" img-top>
<b-card-text>
This card has supporting text below as a natural lead-in to additional content.
</b-card-text>
<b-card-text class="small text-muted">Last updated 3 mins ago</b-card-text>
</b-card>
<b-card bg-variant="primary" text-variant="white">
<blockquote class="card-blockquote">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p>
<footer>
<small>Someone famous in <cite title="Source Title">Source Title</cite></small>
</footer>
</blockquote>
</b-card>
<b-card>
<b-card-title>Title</b-card-title>
<b-card-text>
This card has supporting text below as a natural lead-in to additional content.
</b-card-text>
<b-card-text class="small text-muted">Last updated 3 mins ago</b-card-text>
</b-card>
<b-card img-src="https://picsum.photos/400/400/?image=41" img-alt="Image" overlay></b-card>
<b-card img-src="https://picsum.photos/400/200/?image=41" img-alt="Image" img-top>
<b-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.
</b-card-text>
<template #footer>
<small class="text-muted">Footer Text</small>
</template>
</b-card>
</b-card-group>
</div>