Responsive Design
Learn how to adjust layouts and styles according to viewport size.
Crispy provides a mobile-first responsive system. When you specify a value at a specific breakpoint, that same value is applied to wider viewports.
Breakpoints
| Name | min-width |
|---|---|
base | 0px |
sm | 480px |
md | 768px |
lg | 1280px |
xl | 1440px |
Using Responsive Props in Layout Components
Box and Box-based components (Flex, Grid, VStack, HStack, etc.) can accept objects with breakpoint names as keys for layout property values.
| Category | Property |
|---|---|
| Layout | display, flexDirection, gap |
| Sizing | width, minWidth, maxWidth, height, minHeight, maxHeight |
| Padding | padding, paddingX, paddingY, paddingTop, paddingRight, paddingBottom, paddingLeft, p, px, py, pt, pr, pb, pl |
| Bleed | bleedX, bleedY, bleedTop, bleedRight, bleedBottom, bleedLeft |
Box
Box component is the most basic layout component. It helps you use design tokens in JSX.
Flex
Flex component uses flexbox and helps you use design tokens in JSX.
Grid
Grid component uses CSS Grid and helps you use design tokens in JSX.
VStack
Creates a vertically stacking layout. It helps you use design tokens in JSX.
HStack
Creates a horizontally stacking layout. It helps you use design tokens in JSX.
Hiding & Showing Elements
You can pass responsive values to display to show or hide elements at specific breakpoints.
hideFrom
Use hideFrom to hide elements at a specific breakpoint and above. (display: none;)
Can be used in Box and Box-based components (Flex, Grid, VStack, HStack, etc.).
hideFrom="md" is equivalent to display={{ md: "none" }}.
Grid
You can specify columns and rows attributes for each breakpoint.
Grid
Grid component uses CSS Grid and helps you use design tokens in JSX.
Hooks
Provides useBreakpoint and useBreakpointValue hooks implemented based on window.matchMedia.
Whenever possible, use responsive props that work CSS-based, and use hooks only when JS logic is needed.
useBreakpoint
Returns the current breakpoint name.
useBreakpointValue
Resolves the responsive value object passed as a parameter to the value that matches the current viewport.
Server-Side Rendering
To define a default breakpoint value to be applied when using hooks in an environment where viewport information is unknown (such as SSR), specify defaultBreakpoint via BreakpointProvider.
If you call a hook outside BreakpointProvider but window.matchMedia is not available, useBreakpoint returns base, and useBreakpointValue returns the value of the base key.