Card(卡片)
显示带页头、内容和页脚的卡片。
安装
npx shadcn@latest add card1. 将以下代码复制到你的项目中。
import * as React from "react"
import { cn } from "cn"
function Card({
className,
size = "default",
...props
}: React.ComponentProps<"div"> & { size?: "default" | "sm" }) {
return (
<div
data-slot="card"
data-size={size}
className={cn(
"group/card flex flex-col gap-(--card-spacing) overflow-hidden rounded-2xl bg-card py-(--card-spacing) text-sm text-card-foreground ring-1 ring-foreground/10 [--card-spacing:--spacing(6)] has-[>img:first-child]:pt-0 data-[size=sm]:[--card-spacing:--spacing(4)] *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl",
className
)}
{...props}
/>
)
}
function CardHeader({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-header"
className={cn(
"group/card-header @container/card-header grid auto-rows-min items-start gap-2 rounded-t-xl px-(--card-spacing) has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto] [.border-b]:pb-(--card-spacing)",
className
)}
{...props}
/>
)
}
function CardTitle({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-title"
className={cn("font-heading text-base font-medium", className)}
{...props}
/>
)
}
function CardDescription({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-description"
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
)
}
function CardAction({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-action"
className={cn(
"col-start-2 row-span-2 row-start-1 self-start justify-self-end",
className
)}
{...props}
/>
)
}
function CardContent({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-content"
className={cn("px-(--card-spacing)", className)}
{...props}
/>
)
}
function CardFooter({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-footer"
className={cn(
"flex items-center rounded-b-xl px-(--card-spacing) [.border-t]:pt-(--card-spacing)",
className
)}
{...props}
/>
)
}
export {
Card,
CardHeader,
CardFooter,
CardTitle,
CardAction,
CardDescription,
CardContent,
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
2. 根据你的项目配置调整导入路径。
用法
import {
Card,
CardAction,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card"2
3
4
5
6
7
8
9
<Card>
<CardHeader>
<CardTitle>Card Title</CardTitle>
<CardDescription>Card Description</CardDescription>
<CardAction>Card Action</CardAction>
</CardHeader>
<CardContent>
<p>Card Content</p>
</CardContent>
<CardFooter>
<p>Card Footer</p>
</CardFooter>
</Card>2
3
4
5
6
7
8
9
10
11
12
13
结构
按以下结构组合 Card:
Card
├── CardHeader
│ ├── CardTitle
│ ├── CardDescription
│ └── CardAction
├── CardContent
└── CardFooter2
3
4
5
6
7
尺寸
使用 size="sm" 属性将卡片设为小尺寸。小尺寸变体的间距更小。
间距
除了 size 属性,还可以使用 CSS 变量 --card-spacing 控制各区块之间的间距及卡片各部分的内缩距离。
使用 -mx-(--card-spacing) 设置负外边距,让内容延伸到卡片边缘,并保持与卡片内缩区域的对齐关系。贴边内容位于页脚上方时,在 CardContent 上使用 -mb-(--card-spacing) 可去掉区块间距。
图像
在卡片页头前添加图像,即可创建带图像的卡片。
RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
API 参考
Card
Card 组件是卡片内容的根容器。
| 属性 | 类型 | 默认值 |
|---|---|---|
size | "default" | "sm" | "default" |
className | string | - |
CardHeader
CardHeader 组件用于显示标题、说明和可选操作。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | - |
CardTitle
CardTitle 组件用于显示卡片标题。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | - |
CardDescription
CardDescription 组件用于显示标题下方的辅助文字。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | - |
CardAction
CardAction 组件将内容放在页头右上角,例如按钮或徽章。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | - |
CardContent
CardContent 组件用于显示卡片主体。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | - |
CardFooter
CardFooter 组件用于显示卡片底部的操作和次要内容。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | - |
更新日志
间距变量
如果你从旧版 Card 组件升级,需要应用以下更新才能使用 --card-spacing 变量:
1. 更新 Card 根元素的间距类。
使用 --card-spacing 替换硬编码的间距和纵向内边距,并在根元素上设置默认尺寸和小尺寸的值:
className={cn(
- "group/card flex flex-col gap-4 overflow-hidden rounded-xl bg-card py-4 text-sm text-card-foreground ring-1 ring-foreground/10 has-data-[slot=card-footer]:pb-0 has-[>img:first-child]:pt-0 data-[size=sm]:gap-3 data-[size=sm]:py-3 data-[size=sm]:has-data-[slot=card-footer]:pb-0 *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl",
+ "group/card flex flex-col gap-(--card-spacing) overflow-hidden rounded-xl bg-card py-(--card-spacing) text-sm text-card-foreground ring-1 ring-foreground/10 [--card-spacing:--spacing(4)] has-data-[slot=card-footer]:pb-0 has-[>img:first-child]:pt-0 data-[size=sm]:[--card-spacing:--spacing(3)] data-[size=sm]:has-data-[slot=card-footer]:pb-0 *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl",
className
)}2
3
4
5
2. 更新 CardHeader 的间距类。
使用共享变量替换横向内边距和边框间距:
className={cn(
- "group/card-header @container/card-header grid auto-rows-min items-start gap-1 rounded-t-xl px-4 group-data-[size=sm]/card:px-3 has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto] [.border-b]:pb-4 group-data-[size=sm]/card:[.border-b]:pb-3",
+ "group/card-header @container/card-header grid auto-rows-min items-start gap-1 rounded-t-xl px-(--card-spacing) has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto] [.border-b]:pb-(--card-spacing)",
className
)}2
3
4
5
3. 更新 CardContent 和 CardFooter 的间距类。
使用 --card-spacing 设置内容的内缩距离和页脚内边距:
function CardContent({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-content"
- className={cn("px-4 group-data-[size=sm]/card:px-3", className)}
+ className={cn("px-(--card-spacing)", className)}
{...props}
/>
)
}2
3
4
5
6
7
8
9
10
className={cn(
- "flex items-center rounded-b-xl border-t bg-muted/50 p-4 group-data-[size=sm]/card:p-3",
+ "flex items-center rounded-b-xl border-t bg-muted/50 p-(--card-spacing)",
className
)}2
3
4
5
应用这些更改后,可以在 Card 上使用任意属性类设置 --card-spacing,自定义卡片间距:
function Example() {
return <Card className="[--card-spacing:--spacing(6)]">...</Card>
}2
3