Chakra UI supports responsive styles out of the box. Instead of manually adding
@media
queries and adding nested styles throughout your code, Chakra UI allows
you provide object and array values to add mobile-first responsive styles.
We use the
@media(min-width)
media query to ensure your interfaces are mobile-first.
Responsive syntax relies on the breakpoints defined in the theme object. Chakra UI provides default breakpoints, here's what it looks like:
To make styles responsive, you can use either the array or object syntax.
All style props that arrays as values for mobile-first responsive styles. This is the recommended method.
Let's say you have a Box
with the following properties:
To make the width
or w
responsive using the array syntax, here's what you
need to do:
To interpret array responsive values, Chakra UI converts the values defined in
theme.breakpoints
and sorts them in ascending order. Afterward, we map the
values defined in the array to the breakpoints
Here's how to interpret this syntax:
300px
: From 0em
upwards400px
: From 30em
upwards500px
: From 48em
upwardsTo skip certain breakpoints, you can pass
null
to any position in the array to avoid generating unnecessary CSS.
You can also define responsive values with breakpoint aliases in an object. Any undefined alias key will define the base, non-responsive value.
Let's say you have a Box
that looks like this:
To make the fontSize
responsive using the object syntax, here's what you need
to do:
Remember, Chakra UI uses the min-width media query for responsive design. The breakpoints are:
sm = 30em
,md = 48em
,lg = 62em
,xl = 80em
Here's how to interpret this syntax:
base
: From 0em
upwardsmd
: From 48em
upwardslg
: From 62em
upwardsThis works for every style prop in the theme specification, which means you can change the style of most properties at a given breakpoint.
This shortcut is an alternative to writing media queries out by hand. Given the following:
It'll generate a CSS that looks like this
The equivalent of this style if you passed it as an object.
In some scenarios, you might need to define custom breakpoints for your
application. We recommended using common aliases like sm
, md
, lg
, and
xl
.
To define custom breakpoints, install @chakra-ui/theme-tools
, and use the
createBreakpoints
utility we provide.
Note: Ensure the css unit of your breakpoints are the same. Use all
px
or allem
, don't mix them.
Here's a simple example of a marketing page component that uses a stacked layout on small screens, and a side-by-side layout on larger screens (resize your browser to see it in action):
Marketing
Finding customers for your new businessGetting a new business off the ground is a lot of hard work. Here are five ideas you can use to find your first customers.