Menu

Components

Container

Keeps page content at a comfortable width, centered, with a gutter at both sides.

Introduction

On a wide screen, text that runs edge to edge is hard to read, and a layout that is stuck to the sides looks unfinished. A container fixes both: the content is as wide as the screen up to a maximum, it sits in the middle, and it keeps a gutter at each side.

  • Six maximum widths, from sm for a form to full for no limit.
  • A gutter that grows with the screen, which you can turn off with isPadded.
  • Renders any of eight elements with as, so the page keeps a meaningful structure.
  • It has no state and no client code, so it works in Server Components.

Installation

centerUI has no CLI or registry yet: components are copied into your project. Container needs no react-aria, only the styling helpers.

# 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/container-styles.tsShow code
import { tv } from "tailwind-variants";

// Kept out of container.tsx so anything can use it to give another element the
// same width and gutters, without rendering a Container.
export const containerStyles = tv({
  base: "w-full",
  variants: {
    // The widest the content can get. Below that, it fills the screen.
    size: {
      sm: "max-w-2xl",
      md: "max-w-3xl",
      lg: "max-w-5xl",
      xl: "max-w-7xl",
      "2xl": "max-w-[96rem]",
      full: "max-w-none",
    },
    // Space at the left and right, so the content never touches the edge of
    // the screen. It grows with the screen.
    isPadded: {
      true: "px-4 sm:px-6 lg:px-8",
      false: "",
    },
    isCentered: {
      true: "mx-auto",
      false: "",
    },
  },
  defaultVariants: { size: "xl", isPadded: true, isCentered: true },
});

2. Add the Container file

src/components/ui/container/container.tsxShow code
import type { ComponentPropsWithoutRef, ReactNode } from "react";
import type { VariantProps } from "tailwind-variants";
import { cn } from "@/lib/cn";
import { containerStyles } from "@/types/container-styles";

type ContainerElement =
  | "div"
  | "section"
  | "main"
  | "article"
  | "header"
  | "footer"
  | "nav"
  | "aside";

export interface ContainerProps
  extends Omit<ComponentPropsWithoutRef<"div">, "className" | "children">,
    VariantProps<typeof containerStyles> {
  /** The HTML element to render. Defaults to a `div`. */
  as?: ContainerElement;
  className?: string;
  children?: ReactNode;
}

/**
 * Keeps page content at a comfortable width. It is as wide as the screen up to
 * a maximum (`size`), sits in the middle, and leaves a gutter at both sides
 * that grows with the screen.
 *
 * It is a plain element with no state, so it works in Server Components.
 *
 * @example
 * <Container size="lg" as="main">
 *   <h1>Dashboard</h1>
 * </Container>
 */
export function Container({
  as: Component = "div",
  size,
  isPadded,
  isCentered,
  className,
  ...props
}: ContainerProps) {
  return (
    <Component
      {...props}
      className={cn(containerStyles({ size, isPadded, isCentered }), className)}
    />
  );
}

3. Use it

import { Container } from "@/components/ui/container/container";

Anatomy

A container is a centered box with a maximum width and a gutter.

<Container size="lg">
  <h1>Dashboard</h1>
  <p>Everything here stays within the maximum width.</p>
</Container>
PartRendered asNotes
Containerdiv (or the element you choose)As wide as the screen, up to the maximum for its size, and centered.
Gutterpadding at both sidesKeeps the content off the edge of the screen. It is 16px on phones, 24px from the small breakpoint and 32px from the large one.
ContentchildrenAnything. The container adds no other styling.

Sizes reference

The maximum width of each size. Below it, the container fills the screen.

SizeMax widthIn pixelsGood for
sm42rem672pxForms, sign-in pages, a single column of text
md48rem768pxArticles and long reading
lg64rem1024pxDashboards and settings pages
xl (default)80rem1280pxMost pages, and the width of this site
2xl96rem1536pxWide tables and data-heavy layouts
fullnoneno limitEdge to edge, with only the gutter

Examples

The tinted area is the container, and the dashed box is its content.

Usage

Content

Sizes

Each size caps the width. This preview is narrower than most of them, so here they are drawn to scale against each other instead. On a real page each container fills the screen until it reaches its cap.

sm
42rem
md
48rem
lg
64rem
xl
80rem
2xl
96rem
full
no limit
<Container size="sm">…</Container>
<Container size="md">…</Container>
<Container size="lg">…</Container>
<Container size="xl">…</Container>
<Container size="2xl">…</Container>
<Container size="full">…</Container>

Padding

The gutter keeps the content off the edge of the screen. Turn it off with isPadded={false} when the content brings its own spacing.

With a gutter
Without a gutter

Alignment

The container is centered. Set isCentered to false to keep it at the left. The max-w-xs class here only makes the containers narrower than this preview, so the difference shows.

Centered
At the left

Element

Use the element that matches the role of the content. It changes the HTML that is rendered, not the look.

<header>
<main>
<footer>

Full-width band

A background that spans the whole width, with the content kept in a container. Put the container inside the band. In this preview the container is also narrowed with max-w-sm so it is visibly smaller than the band.

Content in a container

Styles only

containerStyles gives any element the same width and gutter, for when you cannot or do not want to render a Container.

A plain div with containerStyles

Component API

Container takes the props below, plus any attribute of the element it renders (id, aria-label, data-*, ...).

PropTypeDefault
size"sm" | "md" | "lg" | "xl" | "2xl" | "full""xl"
isPaddedbooleantrue
isCenteredbooleantrue
as"div" | "section" | "main" | "article" | "header" | "footer" | "nav" | "aside""div"
classNamestring-
childrenReactNode-