Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Card

A container for one thing on the page, such as a service, a news item or a case. It groups what belongs together on a raised surface. It adds no role (the name a screen reader gives an element), no text and no behaviour of its own.

Package
@kvirn-ui/react
Testing
Automated. Core 0 of 2, Release 0 of 3

When to use it#

  • Use a card for one thing that a resident or a case worker looks at as a unit: a service, a news item, a case.
  • Put a heading at the top and, if the card leads somewhere, one link in the heading. Actions go in the footer as buttons.
  • A list of cards is a list: render each card as a list item, so a screen reader (software that reads the screen aloud) says how many there are.
  • Not for a region of the page, such as a sidebar or a band of content: use a Section. A card on a section is fine, a section inside a card isn’t.

Example#

Waste collection at Storgatan 12

Food waste and residual waste are collected every other week.

Use cases#

A service with actions

Use it when a card is a service the user can act on. The text goes in the body, with the heading first, and the actions go in the footer: one primary button, the rest secondary.

Waste collection at Storgatan 12

Food waste and residual waste are collected every other week.

Props in this example:

A list of news items

Several cards of the same kind are a list. Make each card a list item with as, so a screen reader announces how many items there are. Give each card one link, in its heading, with text that makes sense on its own.

Props in this example:

  • as on Card.Root

Warning: Don't forget

Put role="list" on a hand-written <ul>. The default theme draws no marker on a card, and Safari with VoiceOver can then read the list as a plain group (1.3.1 Info and Relationships). A Stack as a <ul> adds it for you.

A story that stands on its own

Render the card as an <article> when it is a self-contained item, such as a news story, and name it with aria-labelledby pointing at its heading. An <article> is not a landmark, so many of them don’t crowd a screen reader’s list of landmarks.

New opening hours at the recycling centre

From 1 November the recycling centre is open until 19:00 on weekdays. On Saturdays it closes at 15:00.

Props in this example:

  • as on Card.Root

A case with a divider between its parts

A card for staff or a dense page can mark its parts with a line and use less padding. Put the heading in the header, the content in the body and the action in the footer.

Housing adaptation grant

Waiting for a decision.

Latest event: the occupational therapist’s certificate arrived.

Props in this example:

Accessibility#

What it does for you#

  • APG pattern: none. A card is not a widget, so there is no APG pattern.
  • Deviations: none
  • Native elements used: <div> for every part by default. Card.Root takes as (allowed elements below), and the element's own semantics apply.

Card is a plain container for content on a surface. It adds no role, no ARIA, no text, no tabindex and no behaviour. Everything a user perceives inside a card comes from the consumer's children, which keep their own semantics and focus order.

PartElement / roleARIANotes
Card.Root<div> → genericnone. The consumer adds it with the elementclass="kv-card". as="article", "section" with aria-labelledby, "figure" or "li" give it a role. Also exported as CardRoot
Card.Header<div> → genericnoneclass="kv-card-header". Never <header>: at the top level it would be a banner landmark. Also exported as CardHeader
Card.Body<div> → genericnoneclass="kv-card-body". Also exported as CardBody
Card.Footer<div> → genericnoneclass="kv-card-footer". Never <footer>: at the top level it would be a contentinfo landmark. Also exported as CardFooter
Card.Rootasthe chosen element's ownOne element, chosen with a string. A tag outside the list is a type error. In plain JavaScript it warns once in development and renders a <div>. Header, Body and Footer have no as: they are always a <div>. className joins, style and refs pass
every partattributespassed througharia-*, id, data-* and every other attribute reach the element unchanged. The part's class is its own: a className prop joins it, never replaces it
every partneverno role, aria-*, tabindex, inert, aria-hiddenNo click handler, no heading, no live region, no text

useCard() gives the same rootProps, headerProps, bodyProps and footerProps (only className) for your own elements.

It announces nothing.

What you need to do#

  • Heading level. Put a heading at the top of Card.Body (or the Root), at the level the page outline needs: h2 for a card on My pages, h3 for cards under an h2 list heading. Card can't know it (2.4.6 Headings and Labels, 1.3.1 Info and Relationships).
  • Alt text. Use alt="" for a decorative image, which most card images are when the heading names the topic. Give an informative image real alt text. Don't put text in images. The image comes first in the page's order, as it does on screen (1.1.1 Non-text Content, 1.3.2 Meaningful Sequence).
  • Links. One link per card, in the heading, with text that makes sense on its own (2.4.4 Link Purpose (In Context)). No "Read more", and no second link on the image.
  • Buttons. Write them as verbs, and use one primary button per view. Going to another page is a Link, not a Button.
  • Landmarks. Use as="section" with aria-labelledby={headingId} only for a region a user would want to jump to. A section without a name isn't a landmark. Never make every card in a list a landmark. <article> is for a self-contained item such as a news story.
  • Lists. A list of cards is a <ul> with each card rendered as <li> (as="li"), so screen readers say how many items there are. The default theme draws no marker on a card, and Safari can then treat a <ul> without visible markers as a plain group. Put role="list" on a hand-written <ul> to keep it a list there (see Known issues). Stack and Grid add it themselves.
  • Card or Section. A card is one identifiable thing. A region of the page (a sidebar, a band, a group of controls) is a Section, and a form section is a heading or a <fieldset>. A Card on a Section is fine. Don't put a Section inside a Card.
  • Language. Text in another language goes in a nested KvirnProvider, with useLocale().localeProps on your own element (3.1.2 Language of Parts).
  • Parts are direct children of the Root. The default theme's padding relies on it. Don't render an empty part.

Warning: Don't forget

Choose the heading level the page’s outline needs, because Card can’t know it (2.4.6 Headings and Labels). Give a decorative image alt="", and put one link per card in the heading, with text that makes sense on its own (1.1.1 Non-text Content, 2.4.4 Link Purpose (In Context)).

Visual and modes#

Headless: Card ships no CSS. With @kvirn-ui/theme/theme.css:

  • Focus indicator: none of its own, because a card is never focused. Children keep their own rings, and the card doesn't clip them.
  • Target size: not applicable. Footer buttons keep their own sizes (2.5.8 Target Size (Minimum), AA).
  • Contrast: the default theme checks that text, links, muted text and button edges meet their minimum contrast on the card's surface and on the surfaces a card sits on (1.4.3 Contrast (Minimum), AA; 1.4.11 Non-text Contrast, AA). The card's own edge is decorative and low in contrast on purpose: the heading and the spacing group the content, not the edge.
  • Forced colours: the card keeps a visible border in the system's text colour, so its boundary and dividers stay.
  • Reduced motion: nothing moves. There is no hover effect, transition or pointer style.
  • Reflow and text spacing: no fixed sizes, no clipping, long words break, and the card can shrink in a grid. The reflow sweep runs every story at 320 CSS px, with Finnish text, and fails on horizontal overflow, and the text-spacing sweep applies the 1.4.12 spacing to every story and fails on a box that clips more content than it holds (1.4.10 Reflow, 1.4.12 Text Spacing). Media directly in a part or the Root never get wider than it.

Focus management#

  • Initial focus: not moved. Card never moves focus.
  • Trap: no.
  • Restore to: not applicable.
  • Never hidden by: Card draws nothing over its content. The default theme never crops a card, so a child's focus ring is not clipped (2.4.11 Focus Not Obscured (Minimum), AA). Media that touch a rounded corner get the corner's radius themselves instead.

Allowed elements#

A tag outside the list changes the page's outline or semantics (1.3.1, 4.1.2). as is a string, so it works from a Server Component.

Part`as`Why
Card.Rootdiv (default), li, article, figure, sectionA list item, a self-contained item, media with a caption, or a named region. No aside (a card is an object, not tangential content)
Header, Body, FooternoneAlways a <div>: a <header> or <footer> would become a landmark

WCAG success criteria#

  • 1.3.1 Info and Relationships (A): the card has no role of its own, so your element and children decide the meaning. Header and Footer are never landmarks.
  • 1.3.2 Meaningful Sequence (A) and 2.4.3 Focus Order (A): the order in the page is the reading order and the focus order. The Tab rows prove the card is skipped.
  • 1.4.3 Contrast (Minimum) (AA) and 1.4.11 Non-text Contrast (AA): the default theme's colour pairs on every card surface are checked automatically.
  • 1.4.10 Reflow (AA) and 1.4.12 Text Spacing (AA): the card doesn't crop or fix its size. The reflow and text-spacing sweeps check it: the reflow sweep runs every story at 320 CSS px and fails on horizontal overflow; the text-spacing sweep applies the 1.4.12 spacing to every story and fails on a box that clips more content than it holds.
  • 2.4.11 Focus Not Obscured (Minimum) (AA): the card doesn't clip a child's focus ring. 2.4.13 Focus Appearance (AAA) is not counted toward AA.
  • 4.1.2 Name, Role, Value (A): the card has no role or name of its own to get wrong.

Keyboard#

Card has no keys of its own. What you put in it keeps its own keys.

API reference#

import { Card, useCard } from '@kvirn-ui/react'

Card.Root

Renders <div> with no role, or the element as picks. It takes every attribute of that element and passes ref to it. Also exported as CardRoot. The default theme is described on Theming.

PropTypeDefaultDescription
classNamestring–Your own classes. They join the part’s class and never replace it, so the theme keeps styling the card.
as'div' | 'li' | 'article' | 'figure' | 'section''div'Changes the element: li in a list of cards, article for a self-contained item, figure with a caption, or section with aria-labelledby. Card adds no role. Another tag warns once in development.
Class or attributeValuesMeaning
kv-cardalwaysThe part class. The default theme styles it.
kv-card--radius-lg | -md | -noneclass you addThe radius. lg is the default, md suits a card inside a card.
kv-card--padding-none | -sm | -md | -lgclass you addThe padding of every part. md is the default.
kv-card--dividersclass you addA subtle line between the parts.

Card.Header

Renders <div> with no role. It takes every attribute of a <div> and passes ref to it. The default theme is described on Theming. Never a <header>, which would be a page landmark. Also exported as CardHeader.

PropTypeDefaultDescription
classNamestring–Your own classes. They join the part’s class and never replace it, so the theme keeps styling the card.
Class or attributeValuesMeaning
kv-card-headeralwaysThe part class. The default theme styles it.
kv-card-header--padding-none | -sm | -md | -lgclass you addThis part’s padding, instead of the Root’s. none is for full-bleed media.

Card.Body

Renders <div> with no role. It takes every attribute of a <div> and passes ref to it. The default theme is described on Theming. Also exported as CardBody.

PropTypeDefaultDescription
classNamestring–Your own classes. They join the part’s class and never replace it, so the theme keeps styling the card.
Class or attributeValuesMeaning
kv-card-bodyalwaysThe part class. The default theme styles it.
kv-card-body--padding-none | -sm | -md | -lgclass you addThis part’s padding, instead of the Root’s.

Renders <div> with no role. It takes every attribute of a <div> and passes ref to it. The default theme is described on Theming. Never a <footer>, which would be a page landmark. Also exported as CardFooter.

PropTypeDefaultDescription
classNamestring–Your own classes. They join the part’s class and never replace it, so the theme keeps styling the card.
Class or attributeValuesMeaning
kv-card-footeralwaysThe part class. The default theme styles it.
kv-card-footer--padding-none | -sm | -md | -lgclass you addThis part’s padding, instead of the Root’s.

useCard

Takes no options. Use it for your own elements, such as a <section> with aria-labelledby. You pick the element and the heading level.

Result

PropTypeDefaultDescription
rootProps{ className: 'kv-card' }–Spread on the Root’s element.
headerProps{ className: 'kv-card-header' }–Spread on the Header’s element.
bodyProps{ className: 'kv-card-body' }–Spread on the Body’s element.
footerProps{ className: 'kv-card-footer' }–Spread on the Footer’s element.
const card = useCard()
<section {...card.rootProps} aria-labelledby={headingId}>
  <div {...card.bodyProps}>…</div>
</section>

Known issues#

  • WebKit is not automated. Keyboard rows run in Vitest browser mode on Chromium. WebKit is not covered by the automated tests, so check Safari with your own users and assistive technology.
  • A list of cards can lose its list role in Safari. Safari with VoiceOver drops the list role when list-style: none removes the markers, which the default theme does. Write role="list" on a hand-written <ul>, or use Stack or Grid as the list: they add it themselves. If your linter calls role="list" redundant, relax that rule for ul only. Check it with VoiceOver in Safari.
  • Prose and nested cards. Prose turned on inside a card (kv-prose) stops at a nested card, for two levels of nesting. The default theme doesn't support deeper nesting.

Testing#

The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.

AT + browser + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apasscard.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apasscard.test.tsx (axe and role, name and state): adds no role, ARIA or tabindex
Keyboard contract rows (Vitest browser)passn/apass3 rows, each names a test in card.test.tsx
Focus order and managementpassn/apasscard.test.tsx: the card is skipped by Tab: focus goes through its children in DOM order
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

What to check#

With a screen reader and the keyboard, try:

  • Tab through a card with a heading link and two buttons: focus lands on the link, then each button, never the card.
  • A list of cards in VoiceOver on Safari is announced as a list with its count.
  • The heading is read before the actions.