Install the package
Add
sanity-image to your project using your preferred package manager:Sanity Image requires React 18+ or 19+ as a peer dependency.
Get your Sanity project details
You’ll need your Sanity project ID and dataset name. Find these in your Sanity project settings, or construct your base URL:For example:
https://cdn.sanity.io/images/abcd1234/production/Import and use the component
The simplest way to use Sanity Image is to pass the image ID, base URL, and alt text:This will render the image at half its original width with an automatically generated
srcSet.Add size and mode options (optional)
For more control, specify the expected display dimensions and mode:
Expected output
Sanity Image renders a single<img> tag with automatically calculated dimensions and a responsive srcSet:
- Generates a
srcSetwith optimal multipliers based on image size - Sets
widthandheightattributes to prevent layout shifts - Applies
loading="lazy"for native lazy loading - Uses
auto=formatto serve AVIF when supported
Full-featured example
Here’s a complete example showing all the main features:Next steps
Cover vs Contain Modes
Learn when to use cover or contain mode
Styling Images
Best practices for responsive image styling
Create a Wrapper
Simplify usage with a custom wrapper component
API Reference
Explore all available props and options