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
smfor a form tofullfor 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 clsxManual 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>| Part | Rendered as | Notes |
|---|---|---|
| Container | div (or the element you choose) | As wide as the screen, up to the maximum for its size, and centered. |
| Gutter | padding at both sides | Keeps the content off the edge of the screen. It is 16px on phones, 24px from the small breakpoint and 32px from the large one. |
| Content | children | Anything. The container adds no other styling. |
Sizes reference
The maximum width of each size. Below it, the container fills the screen.
| Size | Max width | In pixels | Good for |
|---|---|---|---|
| sm | 42rem | 672px | Forms, sign-in pages, a single column of text |
| md | 48rem | 768px | Articles and long reading |
| lg | 64rem | 1024px | Dashboards and settings pages |
| xl (default) | 80rem | 1280px | Most pages, and the width of this site |
| 2xl | 96rem | 1536px | Wide tables and data-heavy layouts |
| full | none | no limit | Edge to edge, with only the gutter |
Examples
The tinted area is the container, and the dashed box is its content.
Usage
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.
smmdlgxl2xlfull<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.
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.
Element
Use the element that matches the role of the content. It changes the HTML that is rendered, not the look.
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.
Styles only
containerStyles gives any element the same width and gutter, for when you cannot or do not want to render a Container.
Component API
Container takes the props below, plus any attribute of the element it renders (id, aria-label, data-*, ...).
| Prop | Type | Default |
|---|---|---|
| size | "sm" | "md" | "lg" | "xl" | "2xl" | "full" | "xl" |
| isPadded | boolean | true |
| isCentered | boolean | true |
| as | "div" | "section" | "main" | "article" | "header" | "footer" | "nav" | "aside" | "div" |
| className | string | - |
| children | ReactNode | - |