Skip to content
WebJs
Menu

Displays a card with header, content, and footer.

Login to your account
Enter your email below to login to your account
import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { buttonClass } from '#components/ui/button.ts';
import {
  cardClass, cardHeaderClass, cardTitleClass, cardDescriptionClass,
  cardActionClass, cardContentClass, cardFooterClass,
} from '#components/ui/card.ts';
import { inputClass } from '#components/ui/input.ts';
import { labelClass } from '#components/ui/label.ts';

export default function CardDemo() {
  return html`
    <div data-slot="card" data-size="default" class=${cn(cardClass(), 'w-full max-w-sm')}>
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-title" class=${cardTitleClass()}>Login to your account</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>
          Enter your email below to login to your account
        </div>
        <div data-slot="card-action" class=${cardActionClass()}>
          <button type="button" data-slot="button" data-variant="link" data-size="default" class=${buttonClass({ variant: 'link' })}>Sign Up</button>
        </div>
      </div>
      <div data-slot="card-content" class=${cardContentClass()}>
        <form>
          <div class="flex flex-col gap-6">
            <div class="grid gap-2">
              <label data-slot="label" for="email" class=${labelClass()}>Email</label>
              <input
                data-slot="input"
                id="email"
                type="email"
                placeholder="[email protected]"
                required
                class=${inputClass()}
              >
            </div>
            <div class="grid gap-2">
              <div class="flex items-center">
                <label data-slot="label" for="password" class=${labelClass()}>Password</label>
                <a
                  href="#"
                  class="ml-auto inline-block text-sm underline-offset-4 hover:underline"
                >
                  Forgot your password?
                </a>
              </div>
              <input data-slot="input" id="password" type="password" required class=${inputClass()}>
            </div>
          </div>
        </form>
      </div>
      <div data-slot="card-footer" class=${cn(cardFooterClass(), 'flex-col gap-2')}>
        <button type="submit" data-slot="button" data-variant="default" data-size="default" class=${cn(buttonClass(), 'w-full')}>
          Login
        </button>
        <button type="button" data-slot="button" data-variant="outline" data-size="default" class=${cn(buttonClass({ variant: 'outline' }), 'w-full')}>
          Login with Google
        </button>
      </div>
    </div>
  `;
}
View Code

Installation

pnpm dlx @webjsdev/ui add card

Usage

import {
  cardClass,
  cardActionClass,
  cardContentClass,
  cardDescriptionClass,
  cardFooterClass,
  cardHeaderClass,
  cardTitleClass,
} from '#components/ui/card.ts';
<div data-slot="card" data-size="default" class=${cardClass()}>
  <div data-slot="card-header" class=${cardHeaderClass()}>
    <div data-slot="card-title" class=${cardTitleClass()}>Card Title</div>
    <div data-slot="card-description" class=${cardDescriptionClass()}>Card Description</div>
    <div data-slot="card-action" class=${cardActionClass()}>Card Action</div>
  </div>
  <div data-slot="card-content" class=${cardContentClass()}>
    <p>Card Content</p>
  </div>
  <div data-slot="card-footer" class=${cardFooterClass()}>
    <p>Card Footer</p>
  </div>
</div>

Composition

Use the following composition to build a card:

cardClass()
├── cardHeaderClass()
│   ├── cardTitleClass()
│   ├── cardDescriptionClass()
│   └── cardActionClass()
├── cardContentClass()
└── cardFooterClass()

Size

Pass size: 'sm' to cardClass() and set data-size="sm" on the same element to set the size of the card to small. The small size variant uses smaller spacing.

Small Card
This card uses the small size variant.

The card component supports a size prop that can be set to "sm" for a more compact appearance.

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { buttonClass } from '#components/ui/button.ts';
import {
  cardClass, cardHeaderClass, cardTitleClass, cardDescriptionClass,
  cardContentClass, cardFooterClass,
} from '#components/ui/card.ts';

export default function CardSmall() {
  return html`
    <div data-slot="card" data-size="sm" class=${cn(cardClass({ size: 'sm' }), 'mx-auto w-full max-w-sm')}>
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-title" class=${cardTitleClass()}>Small Card</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>
          This card uses the small size variant.
        </div>
      </div>
      <div data-slot="card-content" class=${cardContentClass()}>
        <p>
          The card component supports a size prop that can be set to
          &quot;sm&quot; for a more compact appearance.
        </p>
      </div>
      <div data-slot="card-footer" class=${cardFooterClass()}>
        <button type="button" data-slot="button" data-variant="outline" data-size="sm" class=${cn(buttonClass({ variant: 'outline', size: 'sm' }), 'w-full')}>
          Action
        </button>
      </div>
    </div>
  `;
}
View Code

Spacing

In addition to the size option, you can use the --card-spacing CSS variable to control the spacing between sections and the inset of card parts. Every style carries it except new-york.

16px 20px 24px 32px
Login to your account
Enter your email below to login to your account
import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { buttonClass } from '#components/ui/button.ts';
import {
  cardClass, cardHeaderClass, cardTitleClass, cardDescriptionClass,
  cardActionClass, cardContentClass, cardFooterClass,
} from '#components/ui/card.ts';
import { inputClass } from '#components/ui/input.ts';
import { labelClass } from '#components/ui/label.ts';

const spacingOptions = [
  { label: '16px', value: '4' },
  { label: '20px', value: '5' },
  { label: '24px', value: '6' },
  { label: '32px', value: '8' },
];

// The toggle group reflects pressed onto the chosen item, so the card reads
// the choice with :has() and sets --card-spacing from it, with no script.
const spacingFromChoice = [
  'group-has-[[value="5"][pressed]]/spacing:[--card-spacing:--spacing(5)]',
  'group-has-[[value="6"][pressed]]/spacing:[--card-spacing:--spacing(6)]',
  'group-has-[[value="8"][pressed]]/spacing:[--card-spacing:--spacing(8)]',
];

export default function CardSpacing() {
  return html`
    <div class="group/spacing mx-auto grid w-full max-w-sm gap-4">
      <ui-toggle-group type="single" value="4" variant="outline" size="sm" spacing="default" class="justify-center">
        ${spacingOptions.map((option) => html`
          <ui-toggle-group-item value=${option.value}>${option.label}</ui-toggle-group-item>
        `)}
      </ui-toggle-group>
      <div data-slot="card" data-size="default" class=${cn(cardClass(), spacingFromChoice)}>
        <div data-slot="card-header" class=${cardHeaderClass()}>
          <div data-slot="card-title" class=${cardTitleClass()}>Login to your account</div>
          <div data-slot="card-description" class=${cardDescriptionClass()}>
            Enter your email below to login to your account
          </div>
          <div data-slot="card-action" class=${cardActionClass()}>
            <button type="button" data-slot="button" data-variant="link" data-size="default" class=${buttonClass({ variant: 'link' })}>Sign Up</button>
          </div>
        </div>
        <div data-slot="card-content" class=${cardContentClass()}>
          <form>
            <div class="flex flex-col gap-6">
              <div class="grid gap-2">
                <label data-slot="label" for="email-spacing" class=${labelClass()}>Email</label>
                <input data-slot="input" id="email-spacing" type="email" placeholder="[email protected]" required class=${inputClass()}>
              </div>
              <div class="grid gap-2">
                <div class="flex items-center">
                  <label data-slot="label" for="password-spacing" class=${labelClass()}>Password</label>
                  <a href="#" class="ml-auto inline-block text-sm underline-offset-4 hover:underline">
                    Forgot your password?
                  </a>
                </div>
                <input data-slot="input" id="password-spacing" type="password" required class=${inputClass()}>
              </div>
            </div>
          </form>
        </div>
        <div data-slot="card-footer" class=${cn(cardFooterClass(), 'flex-col gap-2')}>
          <button type="submit" data-slot="button" data-variant="default" data-size="default" class=${cn(buttonClass(), 'w-full')}>
            Login
          </button>
          <button type="button" data-slot="button" data-variant="outline" data-size="default" class=${cn(buttonClass({ variant: 'outline' }), 'w-full')}>
            Login with Google
          </button>
        </div>
      </div>
    </div>
  `;
}
View Code

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 the card content to remove the section gap.

Terms of Service
Review the terms before accepting the agreement.

These terms govern your use of the workspace, including access to shared documents, project files, and collaboration tools.

You are responsible for the content you upload and for ensuring that your team has the appropriate permissions to view or edit it.

We may update features or limits as the service evolves. When those changes materially affect your workflow, we will notify your workspace administrators.

By continuing, you agree to keep your account credentials secure and to follow your organization's acceptable use policies.

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { buttonClass } from '#components/ui/button.ts';
import {
  cardClass, cardHeaderClass, cardTitleClass, cardDescriptionClass,
  cardContentClass, cardFooterClass,
} from '#components/ui/card.ts';

export default function CardEdgeToEdge() {
  return html`
    <div data-slot="card" data-size="default" class=${cn(cardClass(), 'mx-auto w-full max-w-sm')}>
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-title" class=${cardTitleClass()}>Terms of Service</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>
          Review the terms before accepting the agreement.
        </div>
      </div>
      <div data-slot="card-content" class=${cn(cardContentClass(), '-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>
      </div>
      <div data-slot="card-footer" class=${cn(cardFooterClass(), 'justify-end gap-2')}>
        <button type="button" data-slot="button" data-variant="outline" data-size="default" class=${buttonClass({ variant: 'outline' })}>Decline</button>
        <button type="button" data-slot="button" data-variant="default" data-size="default" class=${buttonClass()}>Accept</button>
      </div>
    </div>
  `;
}
View Code

Image

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

Event cover
Featured
Design systems meetup
A practical talk on component APIs, accessibility, and shipping faster.
import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { badgeClass } from '#components/ui/badge.ts';
import { buttonClass } from '#components/ui/button.ts';
import {
  cardClass, cardHeaderClass, cardTitleClass, cardDescriptionClass,
  cardActionClass, cardFooterClass,
} from '#components/ui/card.ts';

export default function CardImage() {
  return html`
    <div data-slot="card" data-size="default" class=${cn(cardClass(), 'relative mx-auto w-full max-w-sm pt-0')}>
      <div class="absolute inset-0 z-30 aspect-video bg-black/35"></div>
      <img
        src="https://avatar.vercel.sh/shadcn1"
        alt="Event cover"
        class="relative z-20 aspect-video w-full object-cover brightness-60 grayscale dark:brightness-40"
      >
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-action" class=${cardActionClass()}>
          <span data-slot="badge" data-variant="secondary" class=${badgeClass({ variant: 'secondary' })}>Featured</span>
        </div>
        <div data-slot="card-title" class=${cardTitleClass()}>Design systems meetup</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>
          A practical talk on component APIs, accessibility, and shipping
          faster.
        </div>
      </div>
      <div data-slot="card-footer" class=${cardFooterClass()}>
        <button type="button" data-slot="button" data-variant="default" data-size="default" class=${cn(buttonClass(), 'w-full')}>View Event</button>
      </div>
    </div>
  `;
}
View Code

RTL

To enable RTL support, see the RTL configuration guide.

تسجيل الدخول إلى حسابك
أدخل بريدك الإلكتروني أدناه لتسجيل الدخول إلى حسابك
نسيت كلمة المرور؟
import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { buttonClass } from '#components/ui/button.ts';
import {
  cardClass, cardHeaderClass, cardTitleClass, cardDescriptionClass,
  cardActionClass, cardContentClass, cardFooterClass,
} from '#components/ui/card.ts';
import { inputClass } from '#components/ui/input.ts';
import { labelClass } from '#components/ui/label.ts';

const translations = {
  en: {
    dir: 'ltr',
    values: {
      title: 'Login to your account',
      description: 'Enter your email below to login to your account',
      signUp: 'Sign Up',
      email: 'Email',
      emailPlaceholder: '[email protected]',
      password: 'Password',
      forgotPassword: 'Forgot your password?',
      login: 'Login',
      loginWithGoogle: 'Login with Google',
    },
  },
  ar: {
    dir: 'rtl',
    values: {
      title: 'تسجيل الدخول إلى حسابك',
      description: 'أدخل بريدك الإلكتروني أدناه لتسجيل الدخول إلى حسابك',
      signUp: 'إنشاء حساب',
      email: 'البريد الإلكتروني',
      emailPlaceholder: '[email protected]',
      password: 'كلمة المرور',
      forgotPassword: 'نسيت كلمة المرور؟',
      login: 'تسجيل الدخول',
      loginWithGoogle: 'تسجيل الدخول باستخدام Google',
    },
  },
  he: {
    dir: 'rtl',
    values: {
      title: 'התחבר לחשבון שלך',
      description: 'הזן את האימייל שלך למטה כדי להתחבר לחשבון שלך',
      signUp: 'הירשם',
      email: 'אימייל',
      emailPlaceholder: '[email protected]',
      password: 'סיסמה',
      forgotPassword: 'שכחת את הסיסמה?',
      login: 'התחבר',
      loginWithGoogle: 'התחבר עם Google',
    },
  },
};

// The provider is the dir attribute: every part styles itself with logical
// properties, so dir="rtl" on the card flips the whole layout.
export default function CardRtl() {
  const lang = 'ar';
  const { dir, values: t } = translations[lang];

  return html`
    <div data-slot="card" data-size="default" dir=${dir} lang=${lang} class=${cn(cardClass(), 'w-full max-w-sm')}>
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-title" class=${cardTitleClass()}>${t.title}</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>${t.description}</div>
        <div data-slot="card-action" class=${cardActionClass()}>
          <button type="button" data-slot="button" data-variant="link" data-size="default" class=${buttonClass({ variant: 'link' })}>${t.signUp}</button>
        </div>
      </div>
      <div data-slot="card-content" class=${cardContentClass()}>
        <form>
          <div class="flex flex-col gap-6">
            <div class="grid gap-2">
              <label data-slot="label" for="email-rtl" class=${labelClass()}>${t.email}</label>
              <input data-slot="input" id="email-rtl" type="email" placeholder=${t.emailPlaceholder} required class=${inputClass()}>
            </div>
            <div class="grid gap-2">
              <div class="flex items-center">
                <label data-slot="label" for="password-rtl" class=${labelClass()}>${t.password}</label>
                <a href="#" class="ms-auto inline-block text-sm underline-offset-4 hover:underline">${t.forgotPassword}</a>
              </div>
              <input data-slot="input" id="password-rtl" type="password" required class=${inputClass()}>
            </div>
          </div>
        </form>
      </div>
      <div data-slot="card-footer" class=${cn(cardFooterClass(), 'flex-col gap-2')}>
        <button type="submit" data-slot="button" data-variant="default" data-size="default" class=${cn(buttonClass(), 'w-full')}>${t.login}</button>
        <button type="button" data-slot="button" data-variant="outline" data-size="default" class=${cn(buttonClass({ variant: 'outline' }), 'w-full')}>${t.loginWithGoogle}</button>
      </div>
    </div>
  `;
}
View Code

API Reference

Every part is a class helper you put on a native element, with the data-slot its styles key on.

cardClass

cardClass() is the root container for card content. Put it on a <div> and set data-size to the same size, which is what the parts read.

Option / AttributeTypeDefault
size / data-size"default" | "sm""default"

cardHeaderClass

cardHeaderClass() is used for a title, description, and optional action.

ElementAttributesOptions
<div>data-slot="card-header"none

cardTitleClass

cardTitleClass() is used for the card title. Put it on a real heading when the card has a meaningful title, at the level that fits the page.

ElementAttributesOptions
<div> or <h2>-<h6>data-slot="card-title"none

cardDescriptionClass

cardDescriptionClass() is used for helper text under the title.

ElementAttributesOptions
<div>data-slot="card-description"none

cardActionClass

cardActionClass() places content in the top-right of the header (for example, a button or a badge). Keep it after the title and description in the markup, so it is announced after them.

ElementAttributesOptions
<div>data-slot="card-action"none

cardContentClass

cardContentClass() is used for the main card body.

ElementAttributesOptions
<div>data-slot="card-content"none

cardFooterClass

cardFooterClass() is used for actions and secondary content at the bottom of the card.

ElementAttributesOptions
<div>data-slot="card-footer"none

Changelog

Spacing Variable

If you added the card before your style carried the --card-spacing variable, run webjs ui diff card to see the drift, then re-add it with webjs ui add card --overwrite or apply the following updates to the class strings in components/ui/card.ts:

Update the card root spacing classes.

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

  const base =
-   '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';

Update the card header spacing classes.

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

  export const cardHeaderClass = (): string =>
-   '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)';

Update the card content and footer spacing classes.

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

  export const cardContentClass = (): string =>
-   'px-4 group-data-[size=sm]/card:px-3';
+   'px-(--card-spacing)';
  export const cardFooterClass = (): string =>
-   '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)';

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

<div data-slot="card" data-size="default" class=${cn(cardClass(), '[--card-spacing:--spacing(6)]')}>...</div>