Skip to Content
vitus·labs
Coolgrid

Container

Grid container with responsive max-width and centered content.

Container is the outermost grid wrapper. It establishes the grid context, applies responsive max-width, and centers content.

Props

PropTypeDefaultDescription
childrenReactNode—Grid content (typically Rows)
componentComponentTypeconfig.component ('div')Custom wrapper element
cssExtraStyles—Extra CSS for the container
widthContainerWidth | functionFrom themeOverride max-width (numbers converted to rem)
columnsValueTypetheme.grid.columnsTotal columns in grid
sizeValueType—Default column size (cascades)
gapValueType—Column gap (cascades)
paddingValueType—Column padding (cascades)
gutterValueType—Row spacing (cascades)
colCssExtraStyles—CSS for all columns (cascades)
rowCssExtraStyles—CSS for all rows (cascades)
colComponentComponentType—Custom Col component (cascades)
rowComponentComponentType—Custom Row component (cascades)
contentAlignXContentAlignX—Row alignment (cascades)

Context Shape

Container provides a ContainerContext to all descendant Rows and Cols:

{
  columns: number | number[] | Record<string, number>,
  size: ValueType,
  gap: ValueType,
  padding: ValueType,
  gutter: ValueType,
  colCss: ExtraStyles,
  colComponent: ComponentType,
  rowCss: ExtraStyles,
  rowComponent: ComponentType,
  contentAlignX: ContentAlignX,
}

All values are memoized and only recomputed when props change.

Styling

Container renders as a flex column container:

display: flex;
flex-direction: column;
width: 100%;
margin-right: auto;
margin-left: auto;
box-sizing: border-box;
/* per breakpoint, from the resolved width: */
max-width: 33.75rem; /* e.g. sm: 540 with rootSize 16 */

Width Resolution

Container max-width is resolved from (highest priority first):

  1. width prop (a function receives the theme widths)
  2. theme.grid.container
  3. theme.coolgrid.container

Static Width

<Container width={{ xs: '100%', sm: 540, md: 720, lg: 960, xl: 1140 }}>
  ...
</Container>

Function Width

Transform the theme container widths:

<Container width={(containerWidths) => ({
  ...containerWidths,
  xl: 1280,  // Override just the xl breakpoint
})}>
  ...
</Container>

Examples

Full-Width Container

<Container width="100%">
  <Row gap={16}>
    <Col size={6}>Left</Col>
    <Col size={6}>Right</Col>
  </Row>
</Container>

Container with Cascading Props

<Container
  columns={12}
  gap={16}
  padding={8}
  colCss={(css) => css`
    background: #f5f5f5;
    border-radius: 4px;
  `}
>
  {/* All Rows and Cols inherit these settings */}
  <Row>
    <Col size={4}>Card 1</Col>
    <Col size={4}>Card 2</Col>
    <Col size={4}>Card 3</Col>
  </Row>
</Container>

Custom Container Element

<Container component="main" css={(css) => css`padding: 24px;`}>
  <Row>
    <Col size={12}>Content</Col>
  </Row>
</Container>
Edit on GitHub

Last updated on

On this page