Using Chakra UI's useBreakpointValue Hook for Responsive Layouts Without Media Queries
Learn how Chakra UI's useBreakpointValue hook lets you define responsive values inline, eliminating media‑query boilerplate while keeping styles co‑located with component code.
11 Oct 2025, 17:22 UTC

Problem: Writing Media Queries for Every Responsive Value
When building a layout component, you often need different widths, margins, or font sizes at each breakpoint. The usual approach is to create a CSS module or styled component with a block of media queries. This splits the responsive logic from the component that uses it, making updates harder and increasing the chance of mismatched breakpoints.
Thesis: useBreakpointValue Keeps Responsive Logic Inline
Chakra UI's useBreakpointValue hook lets you pass an object where keys are breakpoint names and the hook returns the value that matches the current viewport width. The returned value can be fed directly into any style prop (width, margin, fontSize, etc.) or into the sx prop for fine-grained control. Because the hook lives inside the component file, the responsive definition stays next to the JSX that consumes it.
Worked Example: Responsive Box Width
Create a simple component file, for example src/components/ResponsiveBox.jsx. No special permissions are needed; just run the project with your usual dev server (npm start or yarn dev).
import { Box, useBreakpointValue } from '@chakra-ui/react';
export const ResponsiveBox = () => {
const width = useBreakpointValue({
base: '100%', // < 640px
sm: '50%', // >= 640px
md: '25%', // >= 768px
lg: '12.5%', // >= 1024px
});
return (
Responsive width: {width}
);
};
As you resize the browser, the hook returns the value that matches the current breakpoint and the Box’s inline style attribute updates accordingly. You can verify this by opening the browser’s dev tools, selecting the rendered Box element, and checking that its style attribute contains width: 100% (or the appropriate value) at each breakpoint.
Customizing Breakpoints
If your design system uses different breakpoint names or values, you can extend the Chakra theme and the hook will respect the new configuration.
// src/theme.js
import { extendTheme } from '@chakra-ui/react';
const customTheme = extendTheme({
breakpoints: {
base: '0em',
sm: '40em', // 640px
md: '48em', // 768px
lg: '62em', // 992px
xl: '80em', // 1280px
},
});
export default customTheme;
Then wrap your app with ChakraProvider passing customTheme. The same useBreakpointValue call from the example will now trigger at the new widths. To confirm, resize the viewport and watch for the width change at 640px, 768px, 992px, and 1280px instead of Chakra's defaults.
Trade‑offs and Limitations
- Fallback behavior: If the breakpoint object does not contain a key for the current width (e.g., you omitted
lg), the hook returns the first defined value. This can produce unexpected layouts if you rely on the missing breakpoint. Always define a value for every breakpoint you intend to support, or include abasefallback that matches your intent. - Bundle size considerations: Each unique combination of hook returns creates a new style object. When many components use different inline values, the runtime may generate numerous distinct style attributes, which can increase DOM size and reduce the benefit of CSS deduplication. For large‑scale projects, consider extracting repeated responsive patterns into a styled component or a utility function when the same set of values appears in many places.
- Debugging opacity: Because the style is applied inline, it overrides any CSS class definitions with higher specificity. If you later add a stylesheet rule expecting to win, it may be silently overridden. Use the
!importantflag sparingly or keep inline styles as the single source of truth for those properties.
Practical way to check the result: after rendering, open the Elements panel, locate the component, and verify that the style attribute updates as you cross each breakpoint. If you see the value staying constant, double‑check that the breakpoint keys match the theme’s names and that no key is missing.
Actionable Closing
Use useBreakpointValue when you need a handful of responsive values that are tightly coupled to a single component—such as width, margin, or font size. Keep the breakpoint object complete, test the output in the browser’s inspector, and extract the pattern into a reusable styled component only when you notice the same responsive map duplicated across many files. This keeps your code readable, reduces media‑query boilerplate, and aligns the component’s styling with your design system’s breakpoints.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.