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.

Container

A block that limits how wide the page’s content gets, centred for the page and start-aligned for reading and forms. It is a plain layout box. It adds nothing for assistive technology such as screen readers, so the element you choose keeps its own meaning.

Package
@kvirn-ui/react
Testing
Automated. Core n/a, Release n/a

When to use it#

  • Use it to keep a page’s content inside a readable width: the whole page, a column of text or a form.
  • Pick the width with size: page for the page, reading for text and form for one form.
  • Use as when the container should be a landmark (a region people can jump to) such as main, or a named section.
  • Not for the text inside it: use Prose for headings, paragraphs and lists. Not for putting blocks side by side: use Grid. Not for stacking blocks with space between them: use Stack.

Example#

Kvirnby municipality

The content stays inside the page width.

Use cases#

A reading column

Long text is hard to read on a wide screen. A reading container keeps lines to a comfortable length, for guidance, news, case pages and lists of search results.

Waste collection

Bins are emptied every second week. Put the bin out by 7:00 on the day.

  • Household waste
  • Food waste
  • Paper packaging

Props in this example:

Information: Did you know?

A reading container has no side margin of its own, so on a phone the text reaches the edge of the screen. Put it inside a page container, as “The page and a column inside it” shows below.

A form column

A form is easier to fill in when its fields aren’t stretched across the page. A form container gives it a narrow column at the start of the line.

Report a move

Props in this example:

The page and a column inside it

The reading and form widths add no side padding and sit at the start of the line. Put them inside a page container, which keeps the page’s side margin.

Welcome to Kvirnby

Find services, news and contact details.

Props in this example:

A named region

Give the container the element a landmark needs with as, and name it. A page has one main, which a page-width container often is.

News

The library opens an hour earlier on weekdays.

Props in this example:

  • size on Container
  • as on Container

Warning: Don't forget

Make a container a landmark only for a region people want to jump to, and always name it with aria-labelledby or aria-label. The container itself adds no name (1.3.1 Info and Relationships).

Accessibility#

What it does for you#

  • APG pattern: none. Container is not a widget, so there is no APG pattern.
  • Deviations: none
  • Native elements used: <div> by default. The consumer picks another with as (allowed elements below), and the element's own semantics apply.

Container limits the width of the page's content. The page size is centred; reading and form start at the start edge. It adds no role, no ARIA, no text, no tabindex and no behaviour, and it has no data-* state. Everything a user perceives comes from the consumer's children, which keep their own semantics and focus order.

PartElement / roleARIANotes
Container<div> → genericnone. The consumer adds it with the elementclass="kv-container", with kv-container--reading or kv-container--form for size. as="main" or as="section" with aria-labelledby give it a role
Containerasthe chosen element's ownOne element, chosen with a string. A value outside the list is a type error and, in JavaScript, warns once in development and renders the default element. className joins and the ref reaches the element
Containerattributespassed througharia-*, id and every other attribute reach the element unchanged
Containerneverno role, tabindex, inert, aria-hiddenNo handler, no heading, no live region, no text

useContainer() gives the same frozen props object (only className) for your own element.

It announces nothing.

What you need to do#

  • Size. 'page' (default, a page width, padded) is the page; 'reading' (a comfortable line length) is prose; 'form' (a form width) is a form. reading and form are start-aligned and add no padding: nest a page container outside if the page needs the gutter.
  • Landmarks. as="main" or as="section" with aria-labelledby only for a region a user would want to jump to, always named. A page has one main.
  • DOM order is the visual order. Write children in the order they are read and focused (1.3.2 Meaningful Sequence (A), 2.4.3 Focus Order (A)). There is no way to reorder them visually.
  • Language. Text in another language goes in a nested KvirnProvider, with useLocale().localeProps on your own element (3.1.2 Language of Parts (AA)).

Information: Did you know?

A page-width Container with as="main" gives the page its main landmark. Landmarks let people jump past repeated content (2.4.1 Bypass Blocks).

Visual and modes#

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

  • Focus indicator, target size, contrast: not applicable. Container has no colour, no edge and no control of its own.
  • forced-colors behaviour: not applicable, nothing is drawn.
  • reduced-motion behaviour: no motion.
  • Reflow and text spacing: the container sets a maximum width, never a fixed one, so it is never wider than the screen and nothing scrolls sideways at 320px or at 400% zoom (1.4.10 Reflow (AA)). It has no fixed height (1.4.12 Text Spacing (AA)). The reflow sweep checks every story at 320 CSS pixels for sideways scrolling, and the text-spacing sweep checks that no box clips text.

Warning: Don't forget

If you change the widths in your own CSS, keep them as a maximum width and not a fixed one. Then nothing scrolls sideways at 320px or at 400% zoom (1.4.10 Reflow).

Focus management#

  • Strategy: native. Container never moves focus and renders no tabindex. Its children keep their own order, which is the DOM order.
  • Initial focus: not moved.
  • Trap: no.
  • Restore to: not applicable.
  • Never obscured by: Container renders no overlay and sets no overflow, so a child's focus ring is never clipped (2.4.11 Focus Not Obscured (Minimum) (AA); 2.4.13 Focus Appearance (AAA), not counted toward AA).

Allowed elements#

A tag outside the list changes the page's outline or meaning (1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A)). as is a string, so it works from a Server Component.

Part`as`Why
Containerdiv (default), main, section, articlemain (one per page) and section with a name are landmarks, article a self-contained piece. No nav or aside: use Section

WCAG success criteria#

  • 1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A): no role of its own, so the element you choose decides.
  • 1.3.2 Meaningful Sequence (A), 2.4.3 Focus Order (A): DOM order equals reading and focus order (the keyboard rows).
  • 1.4.10 Reflow (AA): a maximum width, never a fixed one (the reflow sweep checks that nothing scrolls sideways at 320 CSS pixels). 1.4.12 Text Spacing (AA): no fixed heights, and the text-spacing sweep applies the wider spacing to every story and fails on a box that clips text.

Keyboard#

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

API reference#

import { Container, useContainer } from '@kvirn-ui/react'

Container

Renders <div class="kv-container"> with no role. It takes every attribute of a <div> and passes ref to it. It has no state, so no data-*, and it renders on the server too. The default theme is described on Theming.

PropTypeDefaultDescription
size'page' | 'reading' | 'form''page'The measure. "page" is centred, at most 80rem wide, with inline padding. "reading" (45rem) and "form" (40rem) are start-aligned and add no padding.
as'div' | 'main' | 'section' | 'article''div'Changes the element: main (one per page), section with a name, or article. Container adds no role of its own: the element’s meaning applies. In JavaScript, an element outside the list is replaced by a div and warns once in development.
refRef<HTMLElement>–Reaches the element, whichever it is.
Class or attributeValuesMeaning
kv-containeralwaysThe part class. The default theme styles it. Your className joins it.
kv-container--readingsize="reading"A 45rem measure for prose.
kv-container--formsize="form"A 40rem measure for one form.

useContainer

Use it to give an element or component of your own the container classes. It adds no role, ARIA or tabindex, and it returns the same frozen object for each size.

Options

PropTypeDefaultDescription
size'page' | 'reading' | 'form''page'The measure, as on Container.

Result

PropTypeDefaultDescription
containerPropsContainerPartProps–Spread on your element: only a className. Join a class of your own with mergeProps.

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/apasscontainer.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apasscontainer.test.tsx (axe and role, name and state): adds no role, ARIA, tabindex, inert or data attribute
Keyboard contract rows (Vitest browser)passn/apass2 rows, each names a test in container.test.tsx
Focus order and managementpassn/apasscontainer.test.tsx asserts focus
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 + Windowsn/an/an/anative <div>, or the main, section or article the consumer picks, with its own semantics; landmark semantics are the element's own native mapping; Container adds none; no state, focus or key handling
VoiceOver + Safari + iOSn/an/an/anative <div>, or the main, section or article the consumer picks, with its own semantics; landmark semantics are the element's own native mapping; Container adds none; no state, focus or key handling
Release (before 1.0 and each minor)
JAWS + Chrome + Windowsn/an/an/anative <div>, or the main, section or article the consumer picks, with its own semantics; landmark semantics are the element's own native mapping; Container adds none; no state, focus or key handling
TalkBack + Chrome + Androidn/an/an/anative <div>, or the main, section or article the consumer picks, with its own semantics; landmark semantics are the element's own native mapping; Container adds none; no state, focus or key handling
VoiceOver + Safari + macOSn/an/an/anative <div>, or the main, section or article the consumer picks, with its own semantics; landmark semantics are the element's own native mapping; Container adds none; no state, focus or key handling