ResponsiveContainer component automatically adjusts chart dimensions based on the parent element’s size. This is essential for responsive designs.
Basic usage
Wrap your chart in aResponsiveContainer:
https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:244-254
Size configuration
Percentage dimensions
Use percentages to fill the parent container:https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:36-44
Fixed dimensions
Use numbers for fixed pixel sizes:Aspect ratio
Maintain a specific width-to-height ratio:aspect is set, height is calculated automatically from width / aspect.
Source: https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:29-32
Size constraints
Minimum dimensions
Prevent charts from getting too small:https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:46-53
Maximum height
Cap the maximum height:https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:62-63
Resize detection
ResponsiveContainer uses the ResizeObserver API to detect size changes. Source:https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:248-249
Debounced resize
Debounce resize events for better performance:0 (no debounce).
Source: https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:70-73
Resize callback
Execute custom logic when size changes:https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:84-86
Common patterns
Full-width chart with fixed height
Most common pattern for dashboard widgets:Full-size chart in a container
Fill a flex or grid container:Responsive with aspect ratio
Maintain proportions across screen sizes:Mobile-responsive chart
Adjust for different screen sizes:Nested ResponsiveContainers
Avoid nesting ResponsiveContainers—they don’t add responsiveness:https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:256-263
Initial dimensions
Set initial dimensions before the first resize:{ width: -1, height: -1 }.
Source: https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:55-61
Styling
Container styling
Apply custom styles to the wrapper:https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:79-82
ID attribute
Add an ID for targeting:https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:75-78
Browser compatibility
ResponsiveContainer requires the ResizeObserver API, which is supported in all modern browsers. For older browsers, include a polyfill:Server-side rendering
For SSR, ResponsiveContainer won’t have dimensions on first render. The chart will appear after the first resize event on the client. Provide initial dimensions to render something during SSR:Performance tips
Min-width in flexboxIn flexbox layouts, set This is the default behavior.Source:
minWidth={0} to prevent issues:https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:126-128Best practices
- Always use ResponsiveContainer for production charts
- Set explicit height when using
width="100%" - Use aspect ratio for proportional scaling
- Add min/max constraints for better control
- Test resize behavior on different screen sizes
- Consider debounce for performance-heavy charts