# Card

Card that groups content with an optional title, description, body, and footer.

- Category: Data
- Status: stable
- Registry: https://markas.jenderalcorp.com/r/card.json
- CLI: `npx markasui@latest add card`

## When to use

Grouping related information, such as a plan summary, a profile, or a statistic.

- Never make the whole card clickable, put links or buttons in the footer
- Never nest a Card inside another Card
- Cards use a border, not a shadow

## Code

### Blade

Save at `resources/views/components/markas/card.blade.php`. No extra dependencies.

```blade
@props([
    'title' => null,
    'description' => null,
])

@php
    $base = 'grid gap-4 rounded-xl border border-line bg-surface p-6 text-ink';
    $header = 'grid gap-1.5';
    $titleClass = 'text-base font-semibold text-ink';
    $descriptionClass = 'text-sm text-muted';
    $body = 'text-sm text-ink';
    $footerClass = 'flex flex-wrap items-center gap-2';
@endphp

<article {{ $attributes->class([$base]) }}>
    @if ($title || $description)
        <header class="{{ $header }}">
            @if ($title)
                <h3 class="{{ $titleClass }}">{{ $title }}</h3>
            @endif
            @if ($description)
                <p class="{{ $descriptionClass }}">{{ $description }}</p>
            @endif
        </header>
    @endif
    @if ($slot->isNotEmpty())
        <div class="{{ $body }}">{{ $slot }}</div>
    @endif
    @isset($footer)
        <div class="{{ $footerClass }}">{{ $footer }}</div>
    @endisset
</article>
```

```blade
<x-markas.card title="Pro plan" description="For small teams that are growing.">
    Includes 10 team members and 50 GB of storage.

    <x-slot:footer>
        <x-markas.button size="sm">Choose plan</x-markas.button>
    </x-slot:footer>
</x-markas.card>
```

### React

Save at `src/components/markas/Card.tsx`. No extra dependencies.

```tsx
import type { ComponentProps, ReactNode } from "react";

const base = "grid gap-4 rounded-xl border border-line bg-surface p-6 text-ink";
const header = "grid gap-1.5";
const titleClass = "text-base font-semibold text-ink";
const descriptionClass = "text-sm text-muted";
const body = "text-sm text-ink";
const footerClass = "flex flex-wrap items-center gap-2";
const cx = (...c: (string | false | null | undefined)[]) =>
  c.filter(Boolean).join(" ");

export type CardProps = Omit<ComponentProps<"article">, "title"> & {
  title?: ReactNode;
  description?: ReactNode;
  footer?: ReactNode;
};

export function Card({
  title,
  description,
  footer,
  className,
  children,
  ...props
}: CardProps) {
  return (
    <article className={cx(base, className)} {...props}>
      {(title || description) && (
        <header className={header}>
          {title && <h3 className={titleClass}>{title}</h3>}
          {description && <p className={descriptionClass}>{description}</p>}
        </header>
      )}
      {children != null && children !== false && (
        <div className={body}>{children}</div>
      )}
      {footer && <div className={footerClass}>{footer}</div>}
    </article>
  );
}
```

```tsx
import { Button } from "@/components/markas/Button";
import { Card } from "@/components/markas/Card";

<Card
  title="Pro plan"
  description="For small teams that are growing."
  footer={<Button size="sm">Choose plan</Button>}
>
  Includes 10 team members and 50 GB of storage.
</Card>;
```

### Vue

Save at `src/components/markas/Card.vue`. No extra dependencies.

```vue
<script setup lang="ts">
const props = defineProps<{
  title?: string;
  description?: string;
}>();

const base = "grid gap-4 rounded-xl border border-line bg-surface p-6 text-ink";
const header = "grid gap-1.5";
const titleClass = "text-base font-semibold text-ink";
const descriptionClass = "text-sm text-muted";
const body = "text-sm text-ink";
const footerClass = "flex flex-wrap items-center gap-2";
</script>

<template>
  <article :class="base">
    <header v-if="props.title || props.description" :class="header">
      <h3 v-if="props.title" :class="titleClass">{{ props.title }}</h3>
      <p v-if="props.description" :class="descriptionClass">
        {{ props.description }}
      </p>
    </header>
    <div v-if="$slots.default" :class="body"><slot /></div>
    <div v-if="$slots.footer" :class="footerClass"><slot name="footer" /></div>
  </article>
</template>
```

```vue
<template>
  <Card title="Pro plan" description="For small teams that are growing.">
    Includes 10 team members and 50 GB of storage.

    <template #footer>
      <Button size="sm">Choose plan</Button>
    </template>
  </Card>
</template>
```

### Svelte

Save at `src/lib/components/markas/Card.svelte`. No extra dependencies.

```svelte
<script lang="ts">
  import type { Snippet } from "svelte";
  import type { HTMLAttributes } from "svelte/elements";

  const base =
    "grid gap-4 rounded-xl border border-line bg-surface p-6 text-ink";
  const header = "grid gap-1.5";
  const titleClass = "text-base font-semibold text-ink";
  const descriptionClass = "text-sm text-muted";
  const body = "text-sm text-ink";
  const footerClass = "flex flex-wrap items-center gap-2";

  type Props = Omit<HTMLAttributes<HTMLElement>, "title"> & {
    title?: string;
    description?: string;
    footer?: Snippet;
    children?: Snippet;
  };

  let {
    title,
    description,
    footer,
    class: className,
    children,
    ...rest
  }: Props = $props();
</script>

<article class={[base, className]} {...rest}>
  {#if title || description}
    <header class={header}>
      {#if title}<h3 class={titleClass}>{title}</h3>{/if}
      {#if description}<p class={descriptionClass}>{description}</p>{/if}
    </header>
  {/if}
  {#if children}
    <div class={body}>{@render children()}</div>
  {/if}
  {#if footer}
    <div class={footerClass}>{@render footer()}</div>
  {/if}
</article>
```

```svelte
<script lang="ts">
  import Button from "$lib/components/markas/Button.svelte";
  import Card from "$lib/components/markas/Card.svelte";
</script>

<Card title="Pro plan" description="For small teams that are growing.">
  Includes 10 team members and 50 GB of storage.

  {#snippet footer()}
    <Button size="sm">Choose plan</Button>
  {/snippet}
</Card>
```

### Astro

Save at `src/components/markas/Card.astro`. No extra dependencies.

```astro
---
import type { HTMLAttributes } from "astro/types";

const base = "grid gap-4 rounded-xl border border-line bg-surface p-6 text-ink";
const header = "grid gap-1.5";
const titleClass = "text-base font-semibold text-ink";
const descriptionClass = "text-sm text-muted";
const body = "text-sm text-ink";
const footerClass = "flex flex-wrap items-center gap-2";

type Props = Omit<HTMLAttributes<"article">, "title"> & {
  title?: string;
  description?: string;
};

const { title, description, class: className, ...rest } = Astro.props;
---

<article class:list={[base, className]} {...rest}>
  {
    (title || description) && (
      <header class={header}>
        {title && <h3 class={titleClass}>{title}</h3>}
        {description && <p class={descriptionClass}>{description}</p>}
      </header>
    )
  }
  {
    Astro.slots.has("default") && (
      <div class={body}>
        <slot />
      </div>
    )
  }
  {
    Astro.slots.has("footer") && (
      <div class={footerClass}>
        <slot name="footer" />
      </div>
    )
  }
</article>
```

```astro
---
import Button from "@/components/markas/Button.astro";
import Card from "@/components/markas/Card.astro";
---

<Card title="Pro plan" description="For small teams that are growing.">
  Includes 10 team members and 50 GB of storage.

  <Button slot="footer" size="sm">Choose plan</Button>
</Card>
```

### Angular

Save at `src/app/markas/card.component.ts`. No extra dependencies.

```ts
import {
  afterNextRender,
  ChangeDetectionStrategy,
  Component,
  type ElementRef,
  input,
  signal,
  viewChild,
} from "@angular/core";

const base = "grid gap-4 rounded-xl border border-line bg-surface p-6 text-ink";
const header = "grid gap-1.5";
const titleClass = "text-base font-semibold text-ink";
const descriptionClass = "text-sm text-muted";
const body = "text-sm text-ink";
const footerClass = "flex flex-wrap items-center gap-2";

const hasContent = (element: HTMLElement) =>
  element.childElementCount > 0 || (element.textContent ?? "").trim() !== "";

/**
 * Attribute selector on a native article: the user's class and attributes stay on the card.
 * An element with the `footer` attribute (an ng-container works) fills the footer, everything
 * else is the body. The host title attribute is cleared so it never becomes a tooltip.
 */
@Component({
  selector: "article[markas-card]",
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { "[class]": "base", "[attr.title]": "null" },
  template: `
    @if (title() || description()) {
      <header [class]="header">
        @if (title()) {
          <h3 [class]="titleClass">{{ title() }}</h3>
        }
        @if (description()) {
          <p [class]="descriptionClass">{{ description() }}</p>
        }
      </header>
    }
    <div #bodyEl [class]="body" [hidden]="!hasBody()"><ng-content /></div>
    <div #footerEl [class]="footerClass" [hidden]="!hasFooter()">
      <ng-content select="[footer]" />
    </div>
  `,
})
export class Card {
  readonly title = input<string>();
  readonly description = input<string>();

  protected readonly base = base;
  protected readonly header = header;
  protected readonly titleClass = titleClass;
  protected readonly descriptionClass = descriptionClass;
  protected readonly body = body;
  protected readonly footerClass = footerClass;
  protected readonly hasBody = signal(false);
  protected readonly hasFooter = signal(false);
  private readonly bodyRef =
    viewChild.required<ElementRef<HTMLElement>>("bodyEl");
  private readonly footerRef =
    viewChild.required<ElementRef<HTMLElement>>("footerEl");

  constructor() {
    // Projected content is only known after the first render.
    afterNextRender(() => {
      this.hasBody.set(hasContent(this.bodyRef().nativeElement));
      this.hasFooter.set(hasContent(this.footerRef().nativeElement));
    });
  }
}
```

```ts
import { Component } from "@angular/core";
import { Button } from "./markas/button.component";
import { Card } from "./markas/card.component";

@Component({
  selector: "app-example",
  imports: [Button, Card],
  template: `
    <article
      markas-card
      title="Pro plan"
      description="For small teams that are growing."
    >
      Includes 10 team members and 50 GB of storage.
      <button markas-button footer size="sm">Choose plan</button>
    </article>
  `,
})
export class Example {}
```

Angular uses an attribute selector on a native `article`. Elements with the `footer` attribute (an `ng-container` works) fill the footer.

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `title` | `string / ReactNode` | - | Card title, rendered as an h3. |
| `description` | `string / ReactNode` | - | Short text below the title. |
| `footer` | `slot / ReactNode / snippet` | - | Action area at the bottom of the card, such as buttons. Angular: an element with the footer attribute. |

## Accessibility

- Uses the article element, so the card reads as one self-contained piece of content.
- The title is an h3. Adjust the page heading structure if the card sits directly under an h1.
- No JavaScript and no built-in interactive elements.

## Notes

- Every part is optional. Empty parts are not rendered, so the spacing stays even.
- Extra classes (such as `max-w-sm`) are merged last and land on the `<article>` element.
- Card has no built-in text. All text comes from your props and slots, so it works in any language as is.
