SanityImage component is a polymorphic React component that renders optimized images from Sanity’s CDN with automatic URL generation, srcset creation, and responsive image support.
Import
Props
TheSanityImage component accepts all standard HTML <img> attributes plus the following props:
Configuration Props
The Sanity project ID to use. If preferred, provide
baseUrl instead.The Sanity dataset to use. If preferred, provide
baseUrl instead.The base URL for the Sanity CDN. If not provided, the
projectId and dataset props will be used to construct the URL.Image Query Props
The Sanity Image ID (
_id or _ref field value).Use
cover to crop the image to match the requested aspect ratio (based on width and height). Use contain to fit the image to the boundaries provided without altering the aspect ratio.The target width of the image in pixels. Only used for determining the dimensions of the generated assets, not for layout. Use CSS to specify how the browser should render the image instead.
The target height of the image in pixels. Only used for determining the dimensions of the generated assets, not for layout. Use CSS to specify how the browser should render the image instead.
The hotspot coordinates to use for the image. Coordinates are between 0 and 1. Note: hotspot
width and height are not used.The crop coordinates to use for the image.
Query string params to pass to Sanity’s image CDN directly. Note that this is only a subset of the params supported by the Sanity image CDN.
Component Props
A low-quality image placeholder (LQIP) to display while the full image loads. Used with
ImageWithPreview component.HTML Attribute Overrides
Passed through to the rendered element as
height, overriding the default behavior of setting the height property automatically based on the computed output image dimensions.Passed through to the rendered element as
width, overriding the default behavior of setting the width property automatically based on the computed output image dimensions.Passed through to the rendered element as
id. The id prop is used to specify the Sanity Image ID, so this is the only way to set the id attribute on the rendered element.Polymorphic Props
Allows rendering as a different element type. Defaults to
img. When specified, the component will accept all props valid for that element type.