Plan comparison table

A real HTML table comparing four plans across grouped features: a plan row with prices and actions that sticks while you scroll, group headings, check and minus marks with screen-reader text, and plain values for limits. On phones the table scrolls sideways with the feature column pinned; on large screens the plan row sticks to the page, with an offset prop for fixed navbars. The recommended plan's column is tinted from top to bottom.

PricingBlockTailwind v4

Source

import type { CSSProperties } from "react";
import { Check, Minus } from "lucide-react";
import { cn } from "@/lib/utils";

export type ComparisonPlan = {
  id: string;
  name: string;
  price: string;
  period?: string;
  cta: { label: string; href: string };
  highlighted?: boolean;
};

/** true = included, false = not included, a string = the plan's own value or limit. */
export type ComparisonValue = boolean | string;

export type ComparisonGroup = {
  name: string;
  features: { name: string; values: Record<string, ComparisonValue> }[];
};

export type PricingComparisonProps = {
  title?: string;
  description?: string;
  plans?: ComparisonPlan[];
  groups?: ComparisonGroup[];
  /** Distance from the top of the page where the plan header sticks on large screens, for example under a fixed navbar. */
  stickyOffset?: string;
  className?: string;
};

const DEFAULT_PLANS: ComparisonPlan[] = [
  { id: "starter", name: "Starter", price: "$0", cta: { label: "Start free", href: "#starter" } },
  { id: "team", name: "Team", price: "$12", period: "/ seat / mo", cta: { label: "Start trial", href: "#team" }, highlighted: true },
  { id: "business", name: "Business", price: "$20", period: "/ seat / mo", cta: { label: "Start trial", href: "#business" } },
  { id: "enterprise", name: "Enterprise", price: "Custom", cta: { label: "Contact sales", href: "#sales" } },
];

const row = (name: string, starter: ComparisonValue, team: ComparisonValue, business: ComparisonValue, enterprise: ComparisonValue) => ({
  name,
  values: { starter, team, business, enterprise },
});

const DEFAULT_GROUPS: ComparisonGroup[] = [
  {
    name: "Workspace",
    features: [
      row("Members", "Up to 5", "Unlimited", "Unlimited", "Unlimited"),
      row("Guests", false, "20", "100", "Unlimited"),
      row("Page history", "7 days", "90 days", "Unlimited", "Unlimited"),
      row("File upload size", "10 MB", "250 MB", "1 GB", "5 GB"),
      row("Offline apps", false, true, true, true),
    ],
  },
  {
    name: "Collaboration",
    features: [
      row("Comments and mentions", true, true, true, true),
      row("Page-level permissions", false, true, true, true),
      row("Approval flows", false, false, true, true),
      row("Shared spaces with clients", false, true, true, true),
    ],
  },
  {
    name: "Security and admin",
    features: [
      row("SAML single sign-on", false, false, true, true),
      row("SCIM provisioning", false, false, true, true),
      row("Audit log", false, false, "90 days", "Unlimited"),
      row("Data residency", false, false, "EU or US", "Any region"),
    ],
  },
  {
    name: "Support",
    features: [
      row("Help centre and forum", true, true, true, true),
      row("Email support", false, "Within 2 days", "Within 1 day", "Within 4 hours"),
      row("Uptime SLA", false, false, "99.9%", "99.95%"),
      row("Named success manager", false, false, false, true),
    ],
  },
];

const focusRing = "outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background";
// Opaque tints, so rows scrolling under the sticky header and pinned column never show through.
const highlightCell = "bg-[color-mix(in_oklab,var(--muted)_65%,var(--background))]";

function Value({ value }: { value: ComparisonValue }) {
  if (value === true)
    return (
      <>
        <Check className="size-4 text-foreground" aria-hidden="true" />
        <span className="sr-only">Included</span>
      </>
    );
  if (value === false)
    return (
      <>
        <Minus className="size-4 text-muted-foreground/50" aria-hidden="true" />
        <span className="sr-only">Not included</span>
      </>
    );
  return <span className="text-foreground">{value}</span>;
}

export function PricingComparison({
  title = "Compare every plan",
  description = "All plans include unlimited pages, the desktop and mobile apps, and imports from any wiki.",
  plans = DEFAULT_PLANS,
  groups = DEFAULT_GROUPS,
  stickyOffset = "0px",
  className,
}: PricingComparisonProps) {
  return (
    <section aria-labelledby="pricing-comparison-title" className={cn("bg-background px-4 py-20 sm:px-6 lg:px-8 lg:py-28", className)}>
      <div className="mx-auto max-w-6xl">
        <div className="max-w-2xl">
          <h2 id="pricing-comparison-title" className="text-balance text-4xl font-semibold tracking-[-0.035em] text-foreground sm:text-5xl">
            {title}
          </h2>
          <p className="mt-4 text-lg leading-relaxed text-muted-foreground">{description}</p>
        </div>

        {/* Small screens: the table scrolls inside this box both ways, so the plan row and feature column stay pinned.
            Large screens: no inner scroll, and the plan row sticks to the page instead. */}
        <div
          role="region"
          aria-labelledby="pricing-comparison-title"
          tabIndex={0}
          style={{ "--chm-sticky-top": stickyOffset } as CSSProperties}
          className={cn("mt-12 max-h-[75vh] overflow-auto rounded-lg lg:mt-16 lg:max-h-none lg:overflow-visible", focusRing)}
        >
          <table className="w-full min-w-[42rem] border-separate border-spacing-0 text-left text-sm">
            <caption className="sr-only">Features included in each plan</caption>
            <thead>
              <tr>
                <td className="sticky left-0 top-0 z-30 w-36 bg-background sm:w-56 lg:top-[var(--chm-sticky-top)] lg:w-[28%]" />
                {plans.map((plan) => (
                  <th
                    key={plan.id}
                    scope="col"
                    style={{ width: `${72 / plans.length}%` }}
                    className={cn(
                      "sticky top-0 z-20 min-w-32 px-3 pb-5 pt-4 align-bottom font-normal sm:px-5 lg:top-[var(--chm-sticky-top)]",
                      plan.highlighted ? cn(highlightCell, "rounded-t-lg") : "bg-background",
                    )}
                  >
                    <span className="block text-base font-semibold text-foreground">{plan.name}</span>
                    <span className="mt-1 block text-muted-foreground">
                      <span className="text-2xl font-semibold tracking-tight text-foreground">{plan.price}</span>
                      {plan.period && <span className="ml-1 text-xs">{plan.period}</span>}
                    </span>
                    <a
                      href={plan.cta.href}
                      className={cn(
                        "mt-4 inline-flex h-9 w-full items-center justify-center whitespace-nowrap rounded-md px-3 text-sm font-medium transition-colors",
                        plan.highlighted ? "bg-primary text-primary-foreground hover:bg-primary/90" : "bg-background text-foreground ring-1 ring-border hover:bg-accent",
                        focusRing,
                      )}
                    >
                      {plan.cta.label}
                    </a>
                  </th>
                ))}
              </tr>
            </thead>
            {groups.map((group, groupIndex) => (
              <tbody key={group.name}>
                <tr>
                  <th scope="rowgroup" className="sticky left-0 z-10 bg-background pb-3 pt-10 text-left text-base font-semibold text-foreground">
                    {group.name}
                  </th>
                  {plans.map((plan) => (
                    <td key={plan.id} className={plan.highlighted ? highlightCell : undefined} />
                  ))}
                </tr>
                {group.features.map((feature, featureIndex) => (
                  <tr key={feature.name}>
                    <th scope="row" className="sticky left-0 z-10 border-b border-border bg-background py-3.5 pr-4 font-normal text-foreground">
                      {feature.name}
                    </th>
                    {plans.map((plan) => (
                      <td
                        key={plan.id}
                        className={cn(
                          "border-b border-border px-3 py-3.5 sm:px-5",
                          plan.highlighted && highlightCell,
                          plan.highlighted && groupIndex === groups.length - 1 && featureIndex === group.features.length - 1 && "rounded-b-lg",
                        )}
                      >
                        <Value value={feature.values[plan.id] ?? false} />
                      </td>
                    ))}
                  </tr>
                ))}
              </tbody>
            ))}
          </table>
        </div>
      </div>
    </section>
  );
}