How to align the image within its bounds. It maps to css `object-position` property.
Image
The Image component is used to display images.
Image composes Box so you can use all the style props and add responsive
styles as well.
Import#
import { Image } from "@chakra-ui/react"
Usage#
<Box boxSize="sm"><Imagesrc="https://demos.creative-tim.com/vision-ui-dashboard-react/static/media/welcome-profile.7ff4f89a.png"alt="Vision UI Dashboard"/></Box>
<Box boxSize="sm"><Imagesrc="https://demos.creative-tim.com/vision-ui-dashboard-react/static/media/welcome-profile.7ff4f89a.png"alt="Vision UI Dashboard"/></Box>
Size#
The size of the image can be adjusted using the boxSize prop.
<Stack direction="row"><ImageboxSize="100px"objectFit="cover"src="https://demos.creative-tim.com/vision-ui-dashboard-react/static/media/welcome-profile.7ff4f89a.png"alt="Vision UI Dashboard"/><ImageboxSize="150px"objectFit="cover"src="https://demos.creative-tim.com/vision-ui-dashboard-react/static/media/welcome-profile.7ff4f89a.png"alt="Free Chakra UI Dashboard"/><ImageboxSize="200px"src="https://demos.creative-tim.com/vision-ui-dashboard-react/static/media/welcome-profile.7ff4f89a.png"alt="Free Chakra UI Dashboard"/></Stack>
<Stack direction="row"><ImageboxSize="100px"objectFit="cover"src="https://demos.creative-tim.com/vision-ui-dashboard-react/static/media/welcome-profile.7ff4f89a.png"alt="Vision UI Dashboard"/><ImageboxSize="150px"objectFit="cover"src="https://demos.creative-tim.com/vision-ui-dashboard-react/static/media/welcome-profile.7ff4f89a.png"alt="Free Chakra UI Dashboard"/><ImageboxSize="200px"src="https://demos.creative-tim.com/vision-ui-dashboard-react/static/media/welcome-profile.7ff4f89a.png"alt="Free Chakra UI Dashboard"/></Stack>
Image with border radius#
<ImageborderRadius="full"boxSize="150px"src="https://demos.creative-tim.com/vision-ui-dashboard-react/static/media/welcome-profile.7ff4f89a.png"alt="Vision UI Dashboard"/>
<ImageborderRadius="full"boxSize="150px"src="https://demos.creative-tim.com/vision-ui-dashboard-react/static/media/welcome-profile.7ff4f89a.png"alt="Vision UI Dashboard"/>
Fallback support#
You can provide a fallback image for when there is an error loading the src of
the image. You can also opt out of this behavior by passing the ignoreFallback
prop.
<Image src="gibbresh.png" fallbackSrc="https://via.placeholder.com/150" />
<Image src="gibbresh.png" fallbackSrc="https://via.placeholder.com/150" />
Improvements from v1#
Added support for the
fitprop to specify how to fit an image within its dimension. It uses theobject-fitproperty.Added support for the
alignprop to specify how to align the image within its dimension. It uses theobject-positionproperty.Added support for custom
fallbackcomponent.
Usage with SSR#
import { Img } from "@chakra-ui/react"
The Img Component provides a fallback for SSR applications which uses the
image directly without client side checks.
Props#
align
alignSystemProps["objectPosition"]crossOrigin
crossOriginThe key used to set the crossOrigin on the HTMLImageElement into which the image will be loaded. This tells the browser to request cross-origin access when trying to download the image data.
"" | "anonymous" | "use-credentials"fallback
fallbackFallback element to show if image is loading or image fails.
React.ReactElementfallbackSrc
fallbackSrcFallback image src to show if image is loading or image fails.
Note 🚨: We recommend you use a local image
stringfit
fitHow the image to fit within its bounds. It maps to css `object-fit` property.
SystemProps["objectFit"]htmlHeight
htmlHeightThe native HTML height attribute to the passed to the img
string | numberhtmlWidth
htmlWidthThe native HTML width attribute to the passed to the img
string | numberignoreFallback
ignoreFallbackIf true, opt out of the fallbackSrc logic and use as img
booleanonError
onErrorA callback for when there was an error loading the image src
((error: string | SyntheticEvent<HTMLImageElement, Event>) => void)onLoad
onLoadA callback for when the image src has been loaded
((event: SyntheticEvent<HTMLImageElement, Event>) => void)sizes
sizesThe image sizes attribute
stringsrc
srcThe image src attribute
stringsrcSet
srcSetThe image srcset attribute
string© 2021, Made with ❤️ by Creative Tim & Simmmple for a better web