Container
Containers are used to constrain a content's width to the current breakpoint, while keeping it fluid.
The Container
component is a single part component. All of the styling is
applied directly to the div
element.
To learn more about styling single part components, visit the Component Style page.
Theming properties#
The properties that affect the theming of theĀ Container
Ā component are:
variant
: The visual variant of the component. Variants for this component are not implemented in the default theme.colorScheme
: The color scheme of the component. Color schemes for this component are not implemented in the default theme.size
: The size of the component. Sizes for this component are not implemented in the default theme.
Theming utilities#
defineStyle
: a function used to create style objects.defineStyleConfig
: a function used to define the style configuration for a single part component.
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'
Customizing the default theme#
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'// define the base component stylesconst baseStyle = {borderRadius: 'xl', // add a border radiusfontWeight: 'medium', // change the font weight}// export the component themeexport const containerTheme = defineStyleConfig({ baseStyle })
After customizing the default theme, we can import it in our theme file and add
it in the components
property.
import { extendTheme } from '@chakra-ui/react'import { containerTheme } from './components/Container'const theme = extendTheme({components: {Container: containerTheme,},})export default theme
This is a crucial step to make sure that any changes that we make to the container theme are applied.
Adding a custom size#
Let's assume we want to create a small, medium, and large size. For demonstration, we will use a couple of different methods to define the max width. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'// define custom sizesconst sizes = {sm: defineStyle({maxW: '45ch',p: '4',}),md: defineStyle({maxW: 'container.sm',p: '6',fontSize: 'lg',}),lg: defineStyle({maxW: '75ch',p: '8',fontSize: 'xl',}),}// export the component themeexport const containerTheme = defineStyleConfig({ sizes })
Every time you're adding anything new to the theme, you need to run the CLI command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Adding a custom variant#
Let's assume we want to include a couple of variants: one that can use a color scheme and a bold one that is black and white. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'// define styles for custom variantconst colorfulVariant = defineStyle((props) => {const { colorScheme: c } = props // add color scheme as a propreturn {_light: {bg: `${c}.200`,color: `${c}.800`,},_dark: {bg: `${c}.700`,color: `${c}.200`,},}})const boldVariant = defineStyle((props) => {return {borderRadius: 'none',border: '2px solid',fontFamily: 'mono',_light: {bg: 'white',color: `black`,},_dark: {bg: 'black',color: 'white',},}})// define custom variantsconst variants = {colorful: colorfulVariant,bold: boldVariant,}// export the component themeexport const containerTheme = defineStyleConfig({ variants })
Using a custom color scheme#
Let's assume we want to use our own custom color scale based on our brand. First, we need to define the color scale in the main theme file:
import { extendTheme } from '@chakra-ui/react';import { containerTheme } from './components/Container';const theme = extendTheme({// add a custom color schemecolors: {brand: {50: '#dafff3',...500: '#13e4b1',...900: '#001b0e',},},});export default theme;
Then, we can use the custom color scale as the color scheme for the container:
<Container colorPalette='brand'>...</Container>
Changing the default properties#
Let's assume we want to add a default size, variant, and color scheme of every container in our app. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'// define which sizes, variants, and color schemes are applied by defaultconst defaultProps = {size: 'md',variant: 'colorful',colorScheme: 'brand',}// export the component themeexport const containerTheme = defineStyleConfig({ defaultProps })
Showcase#
import React from 'react'; import { ChakraProvider, Box, Container, VStack } from '@chakra-ui/react'; import theme from './theme'; import { ColorModeSwitcher } from './ColorModeSwitcher'; export default function App() { return ( <ChakraProvider theme={theme}> <Box position="relative" h="100vh" p={4}> <VStack> <Container> This container has the new theme default properties applied of medium size, brand color scheme, and colorful variant. </Container> <Container size="sm" colorScheme="gray"> This container has the small size, gray color scheme, and new default colorful variant applied. </Container> <Container size="lg" variant="bold"> This container has the large size, no color scheme, and the bold variant applied. </Container> <Container size="sm" variant="bold"> This container has the small size, no color scheme, and the bold variant applied. </Container> <Container size="lg" variant="colorful" colorScheme="blue"> This container has the new default colorful variant with the large size and blue color scheme applied. </Container> </VStack> <ColorModeSwitcher position="fixed" bottom={3} left={3} /> </Box> </ChakraProvider> ); }
Props#
Container composes Box
so you can pass all Box
related props in addition to
this.
The Container
component is a single part component. All of the styling is
applied directly to the div
element.
To learn more about styling single part components, visit the Component Style page.
Theming properties#
The properties that affect the theming of theĀ Container
Ā component are:
variant
: The visual variant of the component. Variants for this component are not implemented in the default theme.colorScheme
: The color scheme of the component. Color schemes for this component are not implemented in the default theme.size
: The size of the component. Sizes for this component are not implemented in the default theme.
Theming utilities#
defineStyle
: a function used to create style objects.defineStyleConfig
: a function used to define the style configuration for a single part component.
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'
Customizing the default theme#
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'// define the base component stylesconst baseStyle = {borderRadius: 'xl', // add a border radiusfontWeight: 'medium', // change the font weight}// export the component themeexport const containerTheme = defineStyleConfig({ baseStyle })
After customizing the default theme, we can import it in our theme file and add
it in the components
property.
import { extendTheme } from '@chakra-ui/react'import { containerTheme } from './components/Container'const theme = extendTheme({components: {Container: containerTheme,},})export default theme
This is a crucial step to make sure that any changes that we make to the container theme are applied.
Adding a custom size#
Let's assume we want to create a small, medium, and large size. For demonstration, we will use a couple of different methods to define the max width. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'// define custom sizesconst sizes = {sm: defineStyle({maxW: '45ch',p: '4',}),md: defineStyle({maxW: 'container.sm',p: '6',fontSize: 'lg',}),lg: defineStyle({maxW: '75ch',p: '8',fontSize: 'xl',}),}// export the component themeexport const containerTheme = defineStyleConfig({ sizes })
Every time you're adding anything new to the theme, you need to run the CLI command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Adding a custom variant#
Let's assume we want to include a couple of variants: one that can use a color scheme and a bold one that is black and white. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'// define styles for custom variantconst colorfulVariant = defineStyle((props) => {const { colorScheme: c } = props // add color scheme as a propreturn {_light: {bg: `${c}.200`,color: `${c}.800`,},_dark: {bg: `${c}.700`,color: `${c}.200`,},}})const boldVariant = defineStyle((props) => {return {borderRadius: 'none',border: '2px solid',fontFamily: 'mono',_light: {bg: 'white',color: `black`,},_dark: {bg: 'black',color: 'white',},}})// define custom variantsconst variants = {colorful: colorfulVariant,bold: boldVariant,}// export the component themeexport const containerTheme = defineStyleConfig({ variants })
Using a custom color scheme#
Let's assume we want to use our own custom color scale based on our brand. First, we need to define the color scale in the main theme file:
import { extendTheme } from '@chakra-ui/react';import { containerTheme } from './components/Container';const theme = extendTheme({// add a custom color schemecolors: {brand: {50: '#dafff3',...500: '#13e4b1',...900: '#001b0e',},},});export default theme;
Then, we can use the custom color scale as the color scheme for the container:
<Container colorPalette='brand'>...</Container>
Changing the default properties#
Let's assume we want to add a default size, variant, and color scheme of every container in our app. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'// define which sizes, variants, and color schemes are applied by defaultconst defaultProps = {size: 'md',variant: 'colorful',colorScheme: 'brand',}// export the component themeexport const containerTheme = defineStyleConfig({ defaultProps })
Showcase#
import React from 'react'; import { ChakraProvider, Box, Container, VStack } from '@chakra-ui/react'; import theme from './theme'; import { ColorModeSwitcher } from './ColorModeSwitcher'; export default function App() { return ( <ChakraProvider theme={theme}> <Box position="relative" h="100vh" p={4}> <VStack> <Container> This container has the new theme default properties applied of medium size, brand color scheme, and colorful variant. </Container> <Container size="sm" colorScheme="gray"> This container has the small size, gray color scheme, and new default colorful variant applied. </Container> <Container size="lg" variant="bold"> This container has the large size, no color scheme, and the bold variant applied. </Container> <Container size="sm" variant="bold"> This container has the small size, no color scheme, and the bold variant applied. </Container> <Container size="lg" variant="colorful" colorScheme="blue"> This container has the new default colorful variant with the large size and blue color scheme applied. </Container> </VStack> <ColorModeSwitcher position="fixed" bottom={3} left={3} /> </Box> </ChakraProvider> ); }