Skip to main content
The 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 a ResponsiveContainer:
Source: https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:244-254

Size configuration

Percentage dimensions

Use percentages to fill the parent container:
Source: 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:
When 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:
Source: https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:46-53

Maximum height

Cap the maximum height:
Source: 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:
Default is 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:
Source: 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:
Source: https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:256-263

Initial dimensions

Set initial dimensions before the first resize:
Default is { 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:
Source: https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:79-82

ID attribute

Add an ID for targeting:
Source: 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

Use debounce for complex chartsIf your chart is slow to render, add debounce to reduce redraws during resize:
Parent must have defined dimensionsResponsiveContainer needs a parent with explicit width/height. If the parent has no size, the chart won’t render:
Min-width in flexboxIn flexbox layouts, set minWidth={0} to prevent issues:
This is the default behavior.Source: https://github.com/recharts/recharts/blob/main/src/component/ResponsiveContainer.tsx:126-128

Best practices

  1. Always use ResponsiveContainer for production charts
  2. Set explicit height when using width="100%"
  3. Use aspect ratio for proportional scaling
  4. Add min/max constraints for better control
  5. Test resize behavior on different screen sizes
  6. Consider debounce for performance-heavy charts