Card for Astro
Astro card sections with shared spacing, framed layouts, and optional actions.
Installation
bunx --bun shadcn@latest add @gnostic/cardnpx shadcn@latest add @gnostic/cardpnpm dlx shadcn@latest add @gnostic/cardComponent classes: Default Gnostic.
Complete the Installation guide first. Then copy the component files below.
No extra packages are required.
Copy the files
src/components/ui/card/card-action.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div data-slot="card-action" class={cn("col-start-2 row-span-2 row-start-1 inline-flex self-start justify-self-end", className)} {...props}>
<slot />
</div>
src/components/ui/card/card-content.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div
data-slot="card-content"
class={cn("flex flex-1 flex-col gap-3 p-(--card-spacing) in-[[data-slot=card]:has(>[data-slot=card-footer]:not(.border-t))]:pb-0 in-[[data-slot=card]:has(>[data-slot=card-header]:not(.border-b))]:pt-0", className)}
{...props}
>
<slot />
</div>
src/components/ui/card/card-description.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div
data-slot="card-description"
class={cn("text-muted-foreground text-sm", className)}
{...props}
>
<slot />
</div>
src/components/ui/card/card-footer.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div data-slot="card-footer" class={cn("flex items-center p-(--card-spacing) in-[[data-slot=card]:has(>[data-slot=card-content])]:pt-[calc(var(--card-spacing)*2/3)]", className)} {...props}>
<slot />
</div>
src/components/ui/card/card-frame-action.astro
---
/**
* Header action for a framed card. Place it after the title and description.
*/
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div
data-slot="card-frame-action"
class={cn(
"col-start-2 inline-flex self-center justify-self-end nth-3:row-span-2 nth-3:row-start-1",
className,
)}
{...props}
>
<slot />
</div>
src/components/ui/card/card-frame-description.astro
---
/**
* Supporting text under a framed card title.
*/
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div
data-slot="card-frame-description"
class={cn("text-muted-foreground self-center text-sm", className)}
{...props}
>
<slot />
</div>
src/components/ui/card/card-frame-footer.astro
---
/**
* Footer band for a framed card. Body actions stay in CardFooter.
*/
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div
data-slot="card-frame-footer"
class={cn("px-(--card-spacing) py-4", className)}
{...props}
>
<slot />
</div>
src/components/ui/card/card-frame-header.astro
---
/**
* Header grid for a framed card.
* An action in the third slot sits on the right.
*/
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div
data-slot="card-frame-header"
class={cn(
"relative grid auto-rows-min grid-rows-[auto_auto] items-start gap-x-4 px-(--card-spacing) py-4 has-data-[slot=card-frame-action]:grid-cols-[1fr_auto]",
className,
)}
{...props}
>
<slot />
</div>
src/components/ui/card/card-frame-title.astro
---
/**
* Title for a framed card header. Size matches the Coss frame title.
*/
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div
data-slot="card-frame-title"
class={cn("self-center text-sm font-semibold", className)}
{...props}
>
<slot />
</div>
src/components/ui/card/card-frame.astro
---
/**
* Framed card shell from Coss UI.
* The muted fill sits on a pseudo-element so the inner card can clip into the border.
*/
import type { HTMLTag, Polymorphic } from "astro/types"
import { cn } from "cn"
import type { CardFrameProps } from "./types.ts"
type Props<Tag extends HTMLTag = "div"> = Polymorphic<{ as: Tag }> &
CardFrameProps
const { as: Tag = "div", class: className, ...props } = Astro.props
---
<Tag
data-slot="card-frame"
class={cn(
"text-card-foreground bg-card before:bg-muted/72 relative flex flex-col rounded-2xl border shadow-xs/5 [--card-spacing:--spacing(6)] [--clip-bottom:-1rem] [--clip-top:-1rem] not-dark:bg-clip-padding before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-2xl)-1px)] before:shadow-[0_1px_--theme(--color-black/4%)] has-data-[slot=table-container]:overflow-hidden *:data-[slot=card]:-m-px *:data-[slot=card]:bg-clip-padding *:data-[slot=card]:shadow-none *:data-[slot=card]:[clip-path:inset(var(--clip-top)_1px_var(--clip-bottom)_1px_round_calc(var(--radius-2xl)-1px))] *:not-first:data-[slot=card]:rounded-t-xl *:not-last:data-[slot=card]:rounded-b-xl *:data-[slot=card]:before:hidden *:not-first:data-[slot=card]:before:rounded-t-[calc(var(--radius-xl)-1px)] *:not-last:data-[slot=card]:before:rounded-b-[calc(var(--radius-xl)-1px)] *:data-[slot=card]:first:[--clip-top:1px] *:data-[slot=card]:last:[--clip-bottom:1px] *:data-[slot=table-container]:-m-px *:data-[slot=table-container]:w-[calc(100%+2px)] dark:before:shadow-[0_-1px_--theme(--color-white/6%)]",
className,
)}
{...props}
>
<slot />
</Tag>
src/components/ui/card/card-header.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div
data-slot="card-header"
class={cn("grid auto-rows-min grid-rows-[auto_auto] items-start gap-1.5 p-(--card-spacing) in-[[data-slot=card]:has(>[data-slot=card-content])]:pb-[calc(var(--card-spacing)*2/3)] has-data-[slot=card-action]:grid-cols-[1fr_auto] group/card-header", className)}
{...props}
>
<slot />
</div>
src/components/ui/card/card-title.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div data-slot="card-title" class={cn("font-heading text-lg leading-none font-semibold group-data-[size=sm]/card:text-base", className)} {...props}>
<slot />
</div>
src/components/ui/card/card.astro
---
import type { HTMLTag, Polymorphic } from "astro/types"
import { cn } from "cn"
import type { CardProps } from "./types.ts"
type Props<Tag extends HTMLTag = "div"> = Polymorphic<{ as: Tag }> & CardProps
const {
as: Tag = "div",
class: className,
size = "default",
...props
} = Astro.props
---
<Tag
data-slot="card"
data-size={size}
class={cn("font-sans bg-card text-card-foreground relative flex flex-col overflow-hidden rounded-2xl border shadow-xs/5 [--card-spacing:--spacing(6)] not-dark:bg-clip-padding before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-2xl)-1px)] before:shadow-[0_1px_--theme(--color-black/4%)] has-[>img:first-child]:pt-0 data-[size=sm]:[--card-spacing:--spacing(4)] dark:before:shadow-[0_-1px_--theme(--color-white/6%)] *:[img:first-child]:rounded-t-2xl *:[img:last-child]:rounded-b-2xl group/card", className)}
{...props}
>
<slot />
</Tag>
src/components/ui/card/index.ts
export { default as Card } from './card.astro'
export type { CardFrameProps, CardProps } from './types.ts'
export { default as CardAction } from './card-action.astro'
export { default as CardContent } from './card-content.astro'
export { default as CardDescription } from './card-description.astro'
export { default as CardFooter } from './card-footer.astro'
export { default as CardFrame } from './card-frame.astro'
export { default as CardFrameAction } from './card-frame-action.astro'
export { default as CardFrameDescription } from './card-frame-description.astro'
export { default as CardFrameFooter } from './card-frame-footer.astro'
export { default as CardFrameHeader } from './card-frame-header.astro'
export { default as CardFrameTitle } from './card-frame-title.astro'
export { default as CardHeader } from './card-header.astro'
export { default as CardTitle } from './card-title.astro'
src/components/ui/card/types.ts
import type { HTMLTag } from 'astro/types'
/** Root card container spacing and polymorphic element. */
export interface CardProps {
/**
* The HTML tag for the card root.
*
* @default 'div'
*/
as?: HTMLTag
/**
* Section inset scale through `--card-spacing`.
*
* @default 'default'
*/
size?: 'default' | 'sm'
}
/** Framed card shell with optional polymorphic root. */
export interface CardFrameProps {
/**
* The HTML tag for the frame root.
*
* @default 'div'
*/
as?: HTMLTag
}
Usage
import {
Card,
CardAction,
CardContent,
CardDescription,
CardFooter,
CardFrame,
CardHeader,
CardTitle,
} from "@/components/ui/card"
<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>
Polymorphic props
<Card as="article">
<CardHeader>
<CardTitle>Project summary</CardTitle>
</CardHeader>
<CardContent>
<p>Review the project scope and schedule.</p>
</CardContent>
</Card>
<CardFrame as="a" href="/projects" class="focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-ring">
<Card>
<CardContent>View projects</CardContent>
</Card>
</CardFrame>
Spacing
--card-spacing controls the section insets in a
Card. Use size="sm" for less spacing, or set the variable on the card:
<Card style="--card-spacing: 1rem;">
<CardContent>Card content</CardContent>
</Card>
CardFrame uses the same variable for the horizontal padding in its header
and footer. Their vertical padding stays at 1rem. The frame and its inner
card each have their own spacing value.
Composition
Arrange the parts as shown for a Card:
Card
├── CardHeader
│ ├── CardTitle
│ ├── CardDescription
│ └── CardAction
├── CardContent
└── CardFooter
CardFrame
├── CardFrameHeader
│ ├── CardFrameTitle
│ ├── CardFrameDescription
│ └── CardFrameAction
├── Card
│ ├── CardContent
│ └── CardFooter (inside frame)
└── CardFrameFooter (outside frame)
Examples
Card frame
CardFrame keeps its Gnostic frame styling with every preset. Its colors follow
the selected theme. The inner Card uses the selected preset’s Card styles.
CardFrame is the framed card shell. Put the title and the description in CardFrameHeader. The inner Card is the body.
Card frame with footer
Put the title and the description in CardFrameHeader. Put the buttons in CardFooter.
Card frame with framed footer
Put the CardFrameFooter outside the Card to create a framed footer.
Image Card
Add an image before the card header to create a card with an image.
Sizes and states
API Reference
CardProps
as?HTMLTag
The HTML tag for the card root.
HTMLTag'div'size?'default' | 'sm'
Section inset scale through `--card-spacing`.
'default' | 'sm''default'CardFrameProps
as?HTMLTag
The HTML tag for the frame root.
HTMLTag'div'Read the installed source for the complete prop types.
When to use Card
Use Card for one related unit of content, such as a project summary or account setting. Compose a header, content, and footer through Astro slots. CardContent is the body part. A CardAction belongs in the header when it acts on the whole card.
Keyboard and behavior
Card adds no custom click or keyboard behavior. With as="a" and href, the root uses native link behavior. Give card links a visible keyboard focus style, as in the example above. For a card with several actions, keep the default container and use links and buttons inside it. A title does not make the entire surface interactive. Check the title element in your page heading order, and give each action a label that identifies its purpose.
Theme and layout
The default surface uses shared card colors, a border, and a small shadow. Section parts own their padding. Use size="sm" or --card-spacing for a denser composition. A CardFrame adds a surrounding surface; its spacing is independent of the inner card. Long titles and footers with several buttons need a narrow-screen check.
