Skip to content

Card

Displays content in a contained card format with various styling options and layouts.

This is some text within a card body.

Installation

Add Card to your project with the CK-UI CLI. It copies the source into your project, so the code is yours to change.

Terminal
npx ck-ui-cli@latest add card
Adds
card.tsxto your CK-UI folder (src/components/ck-ui by default)
Installs
@radix-ui/react-slotclass-variance-authorityif your project does not have them yet

First time with CK-UI in this project? Run npx ck-ui-cli@latest init once before adding components. The installation guide explains what it sets up.

Manual installationInstall the packages and copy the files yourself.

Install the following dependencies

Run these commands in your terminal to install the required packages

Terminal
npm install @radix-ui/react-slot class-variance-authority

Create or update your card component

Copy this code to create your card component

/components/ui/card.tsx
import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";

// Server-compatible: no hooks or context, so Card can be rendered from server components.
// Card.Root is a padded container; the optional parts (Header, Title, Description, Content, Footer)
// space themselves with --card-spacing, which Root sets per size. Plain children are left untouched.

const cardRootVariants = cva("rounded-xl", {
    variants: {
        variant: {
            default: "border border-border",
            // No token matches neutral-800 in dark mode, so the palette classes stay.
            ghost: "hover:bg-neutral-100 dark:hover:bg-neutral-800",
        },
        size: {
            sm: "p-3 size-p-sm [--card-spacing:0.75rem]",
            md: "p-4 size-p-md [--card-spacing:1rem]",
            lg: "p-6 size-p-lg [--card-spacing:1.5rem]",
        },
        width: {
            auto: "w-auto",
            full: "w-full",
            fit: "w-fit",
            /** @deprecated Same as `auto`. */
            constrained: "w-auto",
        },
    },
    defaultVariants: {
        variant: "default",
        size: "md",
    },
});

// Every part after the first is separated from the previous one by the Root's spacing.
const partSpacing = "[&:not(:first-child)]:mt-[var(--card-spacing,1rem)]";

export interface CardRootProps extends React.HTMLAttributes<HTMLDivElement>, VariantProps<typeof cardRootVariants> {
    /** Render the single child element instead of a <div>, merging Card's props onto it. */
    asChild?: boolean;
}

const CardRoot = React.forwardRef<HTMLDivElement, CardRootProps>(({ className, variant, size, width, asChild = false, ...props }, ref) => {
    const Component = asChild ? Slot : "div";
    return <Component ref={ref} className={cn(cardRootVariants({ variant, size, width }), className)} {...props} />;
});
CardRoot.displayName = "Card.Root";

interface CardPartProps extends React.HTMLAttributes<HTMLElement> {
    /** Render the single child element instead of the default element, merging the part's props onto it. */
    asChild?: boolean;
}

export type CardHeaderProps = CardPartProps;

const CardHeader = React.forwardRef<HTMLDivElement, CardHeaderProps>(({ className, asChild = false, ...props }, ref) => {
    const Component = asChild ? Slot : "div";
    return <Component ref={ref} className={cn("flex flex-col gap-1", partSpacing, className)} {...props} />;
});
CardHeader.displayName = "Card.Header";

export type CardTitleProps = CardPartProps;

/** Renders an <h3> by default; use `asChild` to pick the heading level that fits the page outline. */
const CardTitle = React.forwardRef<HTMLHeadingElement, CardTitleProps>(({ className, asChild = false, ...props }, ref) => {
    const Component = asChild ? Slot : "h3";
    return <Component ref={ref} className={cn("size-label-lg text-foreground", className)} {...props} />;
});
CardTitle.displayName = "Card.Title";

export type CardDescriptionProps = CardPartProps;

const CardDescription = React.forwardRef<HTMLParagraphElement, CardDescriptionProps>(({ className, asChild = false, ...props }, ref) => {
    const Component = asChild ? Slot : "p";
    return <Component ref={ref} className={cn("size-p-sm text-muted-foreground", className)} {...props} />;
});
CardDescription.displayName = "Card.Description";

export type CardContentProps = CardPartProps;

const CardContent = React.forwardRef<HTMLDivElement, CardContentProps>(({ className, asChild = false, ...props }, ref) => {
    const Component = asChild ? Slot : "div";
    return <Component ref={ref} className={cn(partSpacing, className)} {...props} />;
});
CardContent.displayName = "Card.Content";

export type CardFooterProps = CardPartProps;

const CardFooter = React.forwardRef<HTMLDivElement, CardFooterProps>(({ className, asChild = false, ...props }, ref) => {
    const Component = asChild ? Slot : "div";
    return <Component ref={ref} className={cn("flex items-center gap-2", partSpacing, className)} {...props} />;
});
CardFooter.displayName = "Card.Footer";

export {
    CardRoot as Root,
    CardHeader as Header,
    CardTitle as Title,
    CardDescription as Description,
    CardContent as Content,
    CardFooter as Footer,
    cardRootVariants,
};

Test Your Component

  • 1. Add the component to a test page
  • 2. Check all props and variants work as expected
  • 3. Verify responsive behavior
  • 4. Test with different content/data
  • 5. Ensure accessibility features work correctly

Composition

Card.Root is a padded container, so any content works inside it. For the common layout, use the optional parts: Card.Header with Card.Title and Card.Description, then Card.Content and Card.Footer. Each part after the first is spaced according to the card size.

Team plan

Billed monthly. Cancel anytime.

Up to 10 seats, shared components and priority support.

Sizes

This is some text within a card body.
This is some text within a card body.
This is some text within a card body.

API Reference

The Card component provides a flexible container for grouping related content and actions. Every part forwards its ref and accepts asChild.

Card.Root

Main container (a div). Uses Radix UI's Slot primitive when asChild is true.

PropTypeDefaultDescription
variant
"default""ghost"
"default"

default has a 1px border; ghost has no border and a hover background

size
"sm""md""lg"
"md"

Padding, text size and the spacing between card parts

width
"auto""full""fit"
"undefined"

Width class. Unset adds no width class; constrained is a deprecated alias of auto.

asChild"boolean""false"

Render the single child element instead of a div, merging the card props onto it (Radix Slot)

className"string""undefined"

Additional CSS classes, merged last

Card.Header

Groups the title and description (a div, stacked with a small gap).

PropTypeDefaultDescription
asChild"boolean""false"

Render the single child element instead of the default element

className"string""undefined"

Additional CSS classes, merged last

Card.Title

Renders an h3. Use asChild to choose the heading level that fits your page outline, e.g. asChild with an h2 child.

PropTypeDefaultDescription
asChild"boolean""false"

Render the single child element instead of the default element

className"string""undefined"

Additional CSS classes, merged last

Card.Description

Supporting text in the muted foreground colour (a p).

PropTypeDefaultDescription
asChild"boolean""false"

Render the single child element instead of the default element

className"string""undefined"

Additional CSS classes, merged last

Card.Content

Main content area (a div).

PropTypeDefaultDescription
asChild"boolean""false"

Render the single child element instead of the default element

className"string""undefined"

Additional CSS classes, merged last

Bottom row for actions (a div laid out as a horizontal flex row).

PropTypeDefaultDescription
asChild"boolean""false"

Render the single child element instead of the default element

className"string""undefined"

Additional CSS classes, merged last

Used in these blocks

Block families with at least one block built on this component. Each page previews its blocks and lists the components and packages they use.