mode prop controls how SanityImage handles aspect ratio changes when you specify both width and height. Understanding the difference between cover and contain is essential for getting the exact image behavior you need.
The Two Modes
Contain Mode (Default)
Contain mode treats the dimensions you provide as boundaries, resizing the image to fit inside of them. The output image will match the aspect ratio of the original image—no cropping will occur.Cover Mode
Cover mode treats the dimensions you provide as a container, resizing the image to completely fill the dimensions. The output image will match the aspect ratio of the dimensions you provide.Visual Comparison
When to Use Each Mode
Use Contain When:
- You want to preserve the original aspect ratio of the image
- You’re displaying images with varying aspect ratios in a flexible layout
- Cropping important parts of the image is not acceptable
- You only provide one dimension (
widthorheight, but not both)
Use Cover When:
- You need all images to match a specific aspect ratio
- You’re creating a grid of images that should have consistent heights
- You want images to completely fill their container
- You have hotspot data from Sanity Studio to control cropping
If you only provide one dimension (
width or height), the mode doesn’t matter—both modes behave the same way since there’s no aspect ratio change.Cover Mode with Grid Layouts
A common use case forcover mode is creating uniform grid layouts. Here’s an example of a 3-column grid where all images have the same height:
Automatic Intelligent Cropping
When usingcover mode without a hotspot value, SanityImage automatically sets crop=entropy in the Sanity Image API. This tells Sanity to crop to the most “interesting” part of the image based on visual analysis.
Next Steps
Image Styling
Learn CSS best practices for responsive images
GROQ Queries
Fetch hotspot and crop data from Sanity