Hero Section Example
Learn how to use sanity-image as a background image for hero sections and other full-width layouts.Full Example
This example shows a hero section with a background image positioned absolutely behind content:How It Works
-
Container positioning: The
sectionis set toposition: relativeto contain the absolutely-positioned image -
Image positioning: The image uses
position: absolutewithtop: 0andleft: 0to fill the entire section -
Object fit: The
objectFit: "cover"CSS property ensures the image fills the container while maintaining aspect ratio -
Z-index layering: The image has
zIndex: 1while the content haszIndex: 2to ensure content appears above the image -
User interaction:
userSelect: "none"prevents the image from being selected when users click and drag
Using Cover Mode for Better Control
If you know the approximate height of your section, you can usemode="cover" to prevent portrait images from being retrieved and then cropped by the browser:
This example uses
mode="contain" for SanityImage but relies on CSS object-fit: cover to handle the final rendering. Using mode="cover" with a height value can be more efficient if you know the section height.Section Sizing
Thesection element is sized based on the content inside the div. The image will automatically fill the entire section regardless of its height. The padding (paddingBlock: 100) creates space around the content.