Menu

Components

Heading

The titles of a page and its sections: a real h1 to h6 for the outline, with a look you choose separately.

Introduction

Heading is the title of a page or a part of it. It is a real h1 to h6, which is what gives a page its outline: screen readers list the headings, and many people move through a page by jumping from one to the next.

  • level is what a heading is in the structure. size is how big it looks. They are separate, so the design can be right without breaking the outline. With no size, it follows the level.
  • Eight sizes. The large ones get smaller on a phone, and a heading that wraps is balanced, so its last line is not a lone word.
  • Weight, tone, alignment, a limit of lines, and a # link that appears on hover so a section can be linked to.
  • HeadingGroup adds what goes around a title at the top of a page: a small label above, a line below, and actions at the end.
  • It has no client code, so it works in Server Components.

Use one h1 a page, and do not skip levels: an h2 is followed by an h3, not an h4. To make text look like a heading without being one, use as.

Installation

centerUI has no CLI or registry yet: components are copied into your project.

# bun
bun add tailwind-variants tailwind-merge clsx

# pnpm
pnpm add tailwind-variants tailwind-merge clsx

# npm
npm install tailwind-variants tailwind-merge clsx

Manual installation

Copy these files into your project and keep the paths, or adjust the imports.

1. Add the styles

src/types/heading-styles.tsShow code
import { tv } from "tailwind-variants";

// Styles for Heading. `size` is how big it looks, and is a separate choice from
// `level`, which is what it is in the outline of the page: an h2 can look like
// a small heading, and a small heading can be an h1. The larger sizes get
// smaller on a phone so a long title does not wrap onto five lines.
export const headingStyles = tv({
  base: "m-0 scroll-mt-24 text-fg",
  variants: {
    size: {
      xs: "text-sm leading-5",
      sm: "text-base leading-6",
      md: "text-lg leading-7",
      lg: "text-xl leading-7 tracking-tight",
      xl: "text-2xl leading-8 tracking-tight",
      "2xl": "text-2xl leading-8 tracking-tight sm:text-3xl sm:leading-9",
      "3xl": "text-3xl leading-9 tracking-tight sm:text-4xl sm:leading-10",
      "4xl": "text-4xl leading-tight tracking-tighter sm:text-5xl lg:text-6xl",
    },
    weight: {
      normal: "font-normal",
      medium: "font-medium",
      semibold: "font-semibold",
      bold: "font-bold",
    },
    tone: {
      default: "text-fg",
      muted: "text-muted-fg",
    },
    align: {
      left: "text-left",
      center: "text-center",
      right: "text-right",
    },
    isBalanced: {
      true: "text-balance",
      false: "",
    },
  },
  defaultVariants: {
    size: "xl",
    weight: "semibold",
    tone: "default",
    isBalanced: true,
  },
});

export const headingGroupStyles = tv({
  slots: {
    root: "flex flex-wrap items-end justify-between gap-x-6 gap-y-4",
    text: "flex min-w-0 flex-1 flex-col gap-2",
    eyebrow:
      "m-0 text-xs font-semibold tracking-widest text-muted-fg uppercase",
    description: "m-0 max-w-2xl text-pretty text-muted-fg",
    actions: "flex shrink-0 items-center gap-2",
  },
  variants: {
    align: {
      left: { text: "items-start text-left" },
      center: { text: "items-center text-center", description: "mx-auto" },
      right: { text: "items-end text-right", description: "ml-auto" },
    },
    // How big the line under the title is, following the title.
    size: {
      xs: { description: "text-sm" },
      sm: { description: "text-sm" },
      md: { description: "text-base" },
      lg: { description: "text-base" },
      xl: { description: "text-base" },
      "2xl": { description: "text-base sm:text-lg" },
      "3xl": { description: "text-lg" },
      "4xl": { description: "text-lg sm:text-xl" },
    },
  },
  defaultVariants: { align: "left", size: "2xl" },
});

/** The size a heading has when it is given none, by its level. */
export const defaultHeadingSize = {
  1: "3xl",
  2: "2xl",
  3: "xl",
  4: "lg",
  5: "md",
  6: "sm",
} as const;

2. Add the Heading file

src/components/ui/heading/heading.tsxShow code
import type { ElementType, ReactNode } from "react";
import { cn } from "@/lib/cn";
import {
  defaultHeadingSize,
  headingGroupStyles,
  headingStyles,
} from "@/types/heading-styles";

type Level = 1 | 2 | 3 | 4 | 5 | 6;
type Size = "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" | "4xl";

export interface HeadingProps {
  children: ReactNode;
  /**
   * What it is in the outline of the page: 1 for the title of the page, then 2
   * for its sections, 3 for their parts, and so on, without skipping one. It
   * decides the element (`h1` to `h6`) and, unless you set `size`, how it looks.
   * Defaults to 2.
   */
  level?: Level;
  /**
   * How big it looks, separate from its level. Without it, the size follows the
   * level: 3xl, 2xl, xl, lg, md and sm for levels 1 to 6.
   */
  size?: Size;
  weight?: "normal" | "medium" | "semibold" | "bold";
  /** `muted` is the softer color of secondary text. */
  tone?: "default" | "muted";
  align?: "left" | "center" | "right";
  /** Even out the lines of a heading that wraps, so the last is not a lone word. Defaults to `true`. */
  isBalanced?: boolean;
  /** Keep it to this many lines and end it with an ellipsis. `1` also stops it from wrapping. */
  lineClamp?: 1 | 2 | 3 | 4;
  /**
   * Show a `#` link beside it on hover and focus, that points at the heading,
   * so a section can be linked to. Needs an `id`.
   */
  isLinkable?: boolean;
  /** Names the heading as a target for links and skip links. */
  id?: string;
  /**
   * Render another element and keep the look of the level, for text that
   * should look like a heading but is not one in the outline, such as the title
   * of a card.
   */
  as?: "div" | "p" | "span" | "strong";
  className?: string;
}

const clamps = {
  1: "line-clamp-1",
  2: "line-clamp-2",
  3: "line-clamp-3",
  4: "line-clamp-4",
} as const;

/**
 * A heading: the title of a page, of a section, of a card. It is a real `h1` to
 * `h6`, so the outline of the page is there for screen readers and for people
 * who jump by heading, and it is styled apart from that: how big it looks does
 * not have to follow its level.
 *
 * Keep the levels in order and use one `h1` a page. Choose `size` to fit the
 * design, and `level` to fit the structure.
 *
 * @example
 * <Heading level={1}>Account settings</Heading>
 * <Heading level={2} size="lg" tone="muted">Notifications</Heading>
 */
export function Heading({
  children,
  level = 2,
  size,
  weight,
  tone,
  align,
  isBalanced,
  lineClamp,
  isLinkable = false,
  id,
  as,
  className,
}: HeadingProps) {
  const Tag: ElementType = as ?? (`h${level}` as const);
  const styles = headingStyles({
    size: size ?? defaultHeadingSize[level],
    weight,
    tone,
    align,
    isBalanced,
  });

  return (
    <Tag
      id={id}
      className={cn(
        styles,
        isLinkable && id && "group/heading",
        lineClamp && clamps[lineClamp],
        className,
      )}
    >
      {children}
      {isLinkable && id ? (
        <a
          href={`#${id}`}
          aria-label="Link to this section"
          className="ml-2 rounded-sm font-normal text-muted-fg no-underline opacity-0 outline-none transition-opacity hover:text-fg focus-visible:opacity-100 focus-visible:ring-2 focus-visible:ring-ring group-hover/heading:opacity-100"
        >
          #
        </a>
      ) : null}
    </Tag>
  );
}

export interface HeadingGroupProps {
  /** The title. It is a `Heading`, with the level and size below. */
  children: ReactNode;
  /** A short label above the title, in small capitals: a category, a step, a date. */
  eyebrow?: ReactNode;
  /** A line under the title that says more. */
  description?: ReactNode;
  /** Controls at the end of the group, such as a button. */
  actions?: ReactNode;
  level?: Level;
  /** How big the title looks. Defaults to `2xl`. */
  size?: Size;
  align?: "left" | "center" | "right";
  id?: string;
  className?: string;
}

/**
 * A heading with what usually goes around it: a small label above, a line of
 * description below, and actions at the end. It is the top of a page or a
 * section.
 *
 * @example
 * <HeadingGroup level={1} eyebrow="Billing" description="Manage your plan and invoices.">
 *   Plans and billing
 * </HeadingGroup>
 */
export function HeadingGroup({
  children,
  eyebrow,
  description,
  actions,
  level = 2,
  size = "2xl",
  align = "left",
  id,
  className,
}: HeadingGroupProps) {
  const styles = headingGroupStyles({ align, size });

  return (
    <div className={cn(styles.root(), className)}>
      <div className={styles.text()}>
        {eyebrow ? <p className={styles.eyebrow()}>{eyebrow}</p> : null}
        <Heading level={level} size={size} id={id} align={align}>
          {children}
        </Heading>
        {description ? (
          <p className={styles.description()}>{description}</p>
        ) : null}
      </div>
      {actions ? <div className={styles.actions()}>{actions}</div> : null}
    </div>
  );
}

3. Use it

import { Heading, HeadingGroup } from "@/components/ui/heading/heading";

Anatomy

A heading, and a group that puts a label, a description and actions around one.

<HeadingGroup
  level={1}
  eyebrow="Billing"
  description="Manage your plan and download invoices."
  actions={<Button>Upgrade</Button>}
>
  Plans and billing
</HeadingGroup>
PartRendered asNotes
Headingh1 to h6The title. Its level is its place in the outline of the page, and its size is how big it looks.
HeadingGroupdivA heading with a small label above, a line of description below and actions at the end.
EyebrowpThe small label above the title, in capitals.
AnchoraA # link that appears beside the heading on hover, with isLinkable and an id.

Examples

Usage

Account settings

Levels

Each level is the matching h1 to h6, and looks its part when it is given no size: 3xl, 2xl, xl, lg, md and sm.

Level 1

Level 2

Level 3

Level 4

Level 5

Level 6

Sizes

Eight sizes, from xs to 4xl. The larger ones shrink on a small screen.

xs — The quick brown fox

sm — The quick brown fox

md — The quick brown fox

lg — The quick brown fox

xl — The quick brown fox

2xl — The quick brown fox

3xl — The quick brown fox

4xl — The quick brown fox

Level and size apart

level is the place in the outline and size is the look, so a section can look small and a part can look big without the levels going out of order.

A section that looks small (h2)

A part that looks big (h4)

Weight

normal

medium

semibold

bold

Tone

Muted is the softer color of secondary text, for a heading that should not compete.

Default

Muted

Alignment

left

center

right

Balanced lines

A heading that wraps has its lines evened out by default, so the last is not a lone word. isBalanced={false} turns it off.

A heading long enough to wrap onto a second line here

A heading long enough to wrap onto a second line here

Line clamp

lineClamp keeps a heading to that many lines and ends it with an ellipsis. With 1 it does not wrap at all.

A very long heading that ends in an ellipsis instead of wrapping

A very long heading that is kept to two lines and ends in an ellipsis when there is more of it than fits inside those two lines of the space it has

Linkable

isLinkable adds a # link beside the heading, shown on hover and on focus, that points at its id. Hover the heading below.

Installation#

As another element

as renders a p, div, span or strong that looks like the heading, for text that is not part of the outline, such as the title of a card.

The title of a card

Heading group

HeadingGroup is the top of a page or a section: a small label, the title, a line of description, and actions.

Eyebrow and description

The eyebrow is a category, a step or a date. The description says more than the title can.

Billing

Plans and billing

Manage your plan, payment methods and invoices in one place.

With actions

Actions sit at the end and drop below the text on a narrow screen.

Team

Members

People with access to this project.

Centered

Pricing

Simple, honest pricing

Pick a plan that fits your team. You can change it at any time.

Component API

Heading and HeadingGroup take the props below.

Heading

PropTypeDefault
childrenReactNode- (required)
level1 | 2 | 3 | 4 | 5 | 62
size"xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" | "4xl"by level
weight"normal" | "medium" | "semibold" | "bold""semibold"
tone"default" | "muted""default"
align"left" | "center" | "right""left"
isBalancedbooleantrue
lineClamp1 | 2 | 3 | 4-
isLinkablebooleanfalse
idstring-
as"div" | "p" | "span" | "strong"an h1 to h6
classNamestring-

HeadingGroup

PropTypeDefault
childrenReactNode- (the title, required)
eyebrowReactNode-
descriptionReactNode-
actionsReactNode-
level1 | 2 | 3 | 4 | 5 | 62
sizeas Heading"2xl"
align"left" | "center" | "right""left"
idstring-
classNamestring-