---
title: "Card"
description: "Displays a card with header, content, and footer."
component: true
---

```tsx
import { Button } from "~/components/ui/button"
import {
  Card,
  CardAction,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle
} from "~/components/ui/card"
import { Input } from "~/components/ui/input"
import { Label } from "~/components/ui/label"

export default function CardDemo() {
  return (
    <Card class="w-full max-w-sm">
      <CardHeader>
        <CardTitle>Login to your account</CardTitle>
        <CardDescription>
          Enter your email below to login to your account
        </CardDescription>
        <CardAction>
          <Button variant="link">Sign Up</Button>
        </CardAction>
      </CardHeader>
      <CardContent>
        <form>
          <div class="flex flex-col gap-6">
            <div class="grid gap-2">
              <Label for="email">Email</Label>
              <Input
                id="email"
                placeholder="m@example.com"
                required
                type="email"
              />
            </div>
            <div class="grid gap-2">
              <div class="flex items-center">
                <Label for="password">Password</Label>
                <a
                  class="ml-auto inline-block text-sm underline-offset-4 hover:underline"
                  href="#"
                >
                  Forgot your password?
                </a>
              </div>
              <Input id="password" required type="password" />
            </div>
          </div>
        </form>
      </CardContent>
      <CardFooter class="flex-col gap-2">
        <Button class="w-full" type="submit">
          Login
        </Button>
        <Button class="w-full" variant="outline">
          Login with Google
        </Button>
      </CardFooter>
    </Card>
  )
}
```

## Installation

<CodeTabs>

<TabsList>
  <TabsTrigger value="cli">Command</TabsTrigger>
  <TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>

<TabsContent value="cli">

```bash
npx solidui-cli@latest add card
```

</TabsContent>

<TabsContent value="manual">

<Steps class="mb-0 pt-2">

<Step>Copy and paste the following code into your project.</Step>

```tsx title="components/ui/card.tsx"
import { type ComponentProps } from "@solidjs/web"
import { merge, omit } from "solid-js"

import { cn } from "~/lib/utils"

type CardProps = ComponentProps<"div"> & {
  size?: "default" | "sm"
}

const Card = (props: CardProps) => {
  const local = merge({ size: "default" } as const, props)
  const others = omit(props, "class", "size")
  return (
    <div
      class={cn("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 group/card flex flex-col", local.class)}
      data-size={local.size}
      data-slot="card"
      {...others}
    />
  )
}

const CardHeader = (props: ComponentProps<"div">) => (
  <div
    data-slot="card-header"
    {...props}
    class={cn(
      "gap-1 rounded-t-xl px-(--card-spacing) [.border-b]:pb-(--card-spacing) group/card-header @container/card-header grid auto-rows-min items-start has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto]",
      props.class
    )}
  />
)

const CardTitle = (props: ComponentProps<"div">) => (
  <div
    data-slot="card-title"
    {...props}
    class={cn("text-base leading-snug font-medium group-data-[size=sm]/card:text-sm", props.class)}
  />
)

const CardDescription = (props: ComponentProps<"div">) => (
  <div
    data-slot="card-description"
    {...props}
    class={cn("text-sm text-muted-foreground", props.class)}
  />
)

const CardAction = (props: ComponentProps<"div">) => (
  <div
    data-slot="card-action"
    {...props}
    class={cn(
      "col-start-2 row-span-2 row-start-1 self-start justify-self-end",
      props.class
    )}
  />
)

const CardContent = (props: ComponentProps<"div">) => (
  <div
    data-slot="card-content"
    {...props}
    class={cn("px-(--card-spacing)", props.class)}
  />
)

const CardFooter = (props: ComponentProps<"div">) => (
  <div
    data-slot="card-footer"
    {...props}
    class={cn("rounded-b-xl border-t bg-muted/50 p-(--card-spacing) flex items-center", props.class)}
  />
)

export {
  Card,
  CardAction,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle,
  type CardProps
}
```

<Step>Update the import paths to match your project setup.</Step>

</Steps>

</TabsContent>

</CodeTabs>

## Usage

```tsx showLineNumbers
import {
  Card,
  CardAction,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle
} from "~/components/ui/card"
```

```tsx showLineNumbers
<Card class="w-full max-w-sm">
  <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>
```

## Composition

Use the following composition to build a `Card`:

```text
Card
├── CardHeader
│   ├── CardTitle
│   ├── CardDescription
│   └── CardAction
├── CardContent
└── CardFooter
```

## Size

Use the `size="sm"` prop to set the size of the card to small. The small size variant uses smaller spacing.

```tsx
import { Button } from "~/components/ui/button"
import {
  Card,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle
} from "~/components/ui/card"

export default function CardSmall() {
  return (
    <Card class="mx-auto w-full max-w-sm" size="sm">
      <CardHeader>
        <CardTitle>Small Card</CardTitle>
        <CardDescription>
          This card uses the small size variant.
        </CardDescription>
      </CardHeader>
      <CardContent>
        <p>
          The card component supports a size prop that can be set to
          &quot;sm&quot; for a more compact appearance.
        </p>
      </CardContent>
      <CardFooter>
        <Button class="w-full" size="sm" variant="outline">
          Action
        </Button>
      </CardFooter>
    </Card>
  )
}
```

## Spacing

In addition to the `size` prop, you can use the `--card-spacing` CSS variable to control the spacing between sections and the inset of card parts.

```tsx
import { createSignal, For } from "solid-js"

import { Button } from "~/components/ui/button"
import {
  Card,
  CardAction,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle
} from "~/components/ui/card"
import { Input } from "~/components/ui/input"
import { Label } from "~/components/ui/label"
import { ToggleGroup, ToggleGroupItem } from "~/components/ui/toggle-group"

const spacingOptions = [
  {
    className: "[--card-spacing:--spacing(4)]",
    label: "16px",
    value: "4"
  },
  {
    className: "[--card-spacing:--spacing(5)]",
    label: "20px",
    value: "5"
  },
  {
    className: "[--card-spacing:--spacing(6)]",
    label: "24px",
    value: "6"
  },
  {
    className: "[--card-spacing:--spacing(8)]",
    label: "32px",
    value: "8"
  }
]

export default function CardSpacing() {
  const [spacing, setSpacing] = createSignal("4")
  const selectedSpacing = () =>
    spacingOptions.find((option) => option.value === spacing())

  return (
    <div class="mx-auto grid w-full max-w-sm gap-4">
      <ToggleGroup
        deselectable={false}
        value={[spacing()]}
        onValueChange={(value) => {
          if (value[0]) {
            setSpacing(value[0])
          }
        }}
        variant="outline"
        size="sm"
        class="justify-center"
      >
        <For each={spacingOptions}>
          {(option) => (
            <ToggleGroupItem value={option.value}>
              {option.label}
            </ToggleGroupItem>
          )}
        </For>
      </ToggleGroup>
      <Card class={selectedSpacing()?.className}>
        <CardHeader>
          <CardTitle>Login to your account</CardTitle>
          <CardDescription>
            Enter your email below to login to your account
          </CardDescription>
          <CardAction>
            <Button variant="link">Sign Up</Button>
          </CardAction>
        </CardHeader>
        <CardContent>
          <form>
            <div class="flex flex-col gap-6">
              <div class="grid gap-2">
                <Label for="email-spacing">Email</Label>
                <Input
                  id="email-spacing"
                  type="email"
                  placeholder="m@example.com"
                  required
                />
              </div>
              <div class="grid gap-2">
                <div class="flex items-center">
                  <Label for="password-spacing">Password</Label>
                  <a
                    href="#"
                    class="ml-auto inline-block text-sm underline-offset-4 hover:underline"
                  >
                    Forgot your password?
                  </a>
                </div>
                <Input id="password-spacing" type="password" required />
              </div>
            </div>
          </form>
        </CardContent>
        <CardFooter class="flex-col gap-2">
          <Button type="submit" class="w-full">
            Login
          </Button>
          <Button variant="outline" class="w-full">
            Login with Google
          </Button>
        </CardFooter>
      </Card>
    </div>
  )
}
```

Use negative margins with `-mx-(--card-spacing)` to make content go edge to edge while keeping it aligned with the card inset. When the edge-to-edge content sits above a footer, use `-mb-(--card-spacing)` on `CardContent` to remove the section gap.

```tsx
import { Button } from "~/components/ui/button"
import {
  Card,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle
} from "~/components/ui/card"

export default function CardEdgeToEdge() {
  return (
    <Card class="mx-auto w-full max-w-sm">
      <CardHeader>
        <CardTitle>Terms of Service</CardTitle>
        <CardDescription>
          Review the terms before accepting the agreement.
        </CardDescription>
      </CardHeader>
      <CardContent class="-mb-(--card-spacing)">
        <div class="-mx-(--card-spacing) max-h-48 space-y-4 overflow-y-scroll border-t bg-muted/50 px-(--card-spacing) py-4 text-sm leading-relaxed">
          <p>
            These terms govern your use of the workspace, including access to
            shared documents, project files, and collaboration tools.
          </p>
          <p>
            You are responsible for the content you upload and for ensuring that
            your team has the appropriate permissions to view or edit it.
          </p>
          <p>
            We may update features or limits as the service evolves. When those
            changes materially affect your workflow, we will notify your
            workspace administrators.
          </p>
          <p>
            By continuing, you agree to keep your account credentials secure and
            to follow your organization&apos;s acceptable use policies.
          </p>
        </div>
      </CardContent>
      <CardFooter class="justify-end gap-2">
        <Button variant="outline">Decline</Button>
        <Button>Accept</Button>
      </CardFooter>
    </Card>
  )
}
```

## Image

Add an image before the card header to create a card with an image.

```tsx
import { Badge } from "~/components/ui/badge"
import { Button } from "~/components/ui/button"
import {
  Card,
  CardAction,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle
} from "~/components/ui/card"

export default function CardImage() {
  return (
    <Card class="relative mx-auto w-full max-w-sm pt-0">
      <div class="absolute inset-0 z-30 aspect-video bg-black/35" />
      <img
        alt="Event cover"
        class="relative z-20 aspect-video w-full object-cover brightness-60 grayscale dark:brightness-40"
        src="/solid-brand/launch-light.svg"
      />
      <CardHeader>
        <CardAction>
          <Badge variant="secondary">Featured</Badge>
        </CardAction>
        <CardTitle>Design systems meetup</CardTitle>
        <CardDescription>
          A practical talk on component APIs, accessibility, and shipping
          faster.
        </CardDescription>
      </CardHeader>
      <CardFooter>
        <Button class="w-full">View Event</Button>
      </CardFooter>
    </Card>
  )
}
```

## API Reference

### Card

| Prop    | Type                |
| ------- | ------------------- |
| `size`  | `"default" \| "sm"` |
| `class` | `string`            |

### CardHeader

The `CardHeader` component is used for a title, description, and optional action.

| Prop    | Type     | Default |
| ------- | -------- | ------- |
| `class` | `string` | -       |

### CardTitle

The `CardTitle` component is used for the card title.

| Prop    | Type     | Default |
| ------- | -------- | ------- |
| `class` | `string` | -       |

### CardDescription

The `CardDescription` component is used for helper text under the title.

| Prop    | Type     | Default |
| ------- | -------- | ------- |
| `class` | `string` | -       |

### CardAction

The `CardAction` component places content in the top-right of the header (for example, a button or a badge).

| Prop    | Type     | Default |
| ------- | -------- | ------- |
| `class` | `string` | -       |

### CardContent

The `CardContent` component is used for the main card body.

| Prop    | Type     | Default |
| ------- | -------- | ------- |
| `class` | `string` | -       |

### CardFooter

The `CardFooter` component is used for actions and secondary content at the bottom of the card.

| Prop    | Type     | Default |
| ------- | -------- | ------- |
| `class` | `string` | -       |

## Changelog

### Spacing Variable

If you're upgrading from a previous version of the `Card` component, you'll need to apply the following updates to use the `--card-spacing` variable:

<Steps>

<Step>Update the Card root spacing classes.</Step>

Replace the hard-coded gap and vertical padding with `--card-spacing`, and set the default and small size values on the root:

```diff
  class={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",
    class
  )}
```

<Step>Update CardHeader spacing classes.</Step>

Replace the horizontal padding and border spacing with the shared variable:

```diff
  class={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)",
    class
  )}
```

<Step>Update CardContent and CardFooter spacing classes.</Step>

Use `--card-spacing` for the content inset and footer padding:

```diff
  function CardContent({ class, ...props }: ComponentProps<"div">) {
    return (
      <div
        data-slot="card-content"
-       class={cn("px-4 group-data-[size=sm]/card:px-3", class)}
+       class={cn("px-(--card-spacing)", class)}
        {...props}
      />
    )
  }
```

```diff
  class={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)",
    class
  )}
```

</Steps>

After applying these changes, you can customize card spacing by setting `--card-spacing` on the `Card` with an arbitrary property class:
