# Image (/docs/image)





`Image` renders two elements. A root `<span>` owns the box: its aspect ratio, its radius and the clipping. It also paints the placeholder. The `<img>` inside it fades in over the placeholder once it has loaded **and** decoded, so you never see a half-painted frame. The placeholder is plain CSS, so it's already in the server HTML: either a color, or a tiny `data:` image blurred through an SVG filter. With neither, the box pulses like a [`Skeleton`](/docs/skeleton).

```tsx
import { Image } from '@42-central/ui-react/image';

<Image src={photo.url} alt={photo.description} ratio="3/2" placeholder={photo.blur} className="w-full" />
```

## Playground [#playground]

<story.Playground />

### Full prop browser [#full-prop-browser]

<story.WithControl />

## Status [#status]

The status is `empty` (no `src`), `loading`, `loaded` or `error`. It's set as `data-state` on both the root and the `<img>`, and reported through `onStatusChange`. The status is tracked per source: when `src` changes, the new source reads as `loading` from its very first render, and a source that has already loaded reads as `loaded` again straight away. This demo loads through a deliberately slow endpoint, so you can watch every change.

<Preview code="&#x22;const [status, setStatus] = useState<ImageStatus>('loading');\n\n<Image\n  src={slowUrl}\n  alt=\&#x22;A mountain lake at dawn\&#x22;\n  ratio=\&#x22;3/2\&#x22;\n  placeholder={blurDataUri}\n  onStatusChange={setStatus}\n/>&#x22;">
  <StatusDemo />
</Preview>

Style the states from CSS with `data-[state=loaded]:…` on the root, or with `classNames.image` on the image.

## Placeholders [#placeholders]

`placeholder` takes a CSS color, or a `data:` URI. A data URI is drawn into an SVG through an `feGaussianBlur` and used as the root's `background-image`, so a 10px thumbnail scales up into a soft wash rather than a mosaic. This is the same technique next/image uses, and it needs no dependency. Leave `placeholder` out, and the box pulses with the `Skeleton` tokens.

<Preview code="&#x22;<Image src={src} alt=\&#x22;…\&#x22; ratio=\&#x22;3/2\&#x22; placeholder=\&#x22;data:image/png;base64,iVBORw0KGgo…\&#x22; />\n<Image src={src} alt=\&#x22;…\&#x22; ratio=\&#x22;3/2\&#x22; placeholder=\&#x22;#4b5f80\&#x22; />\n<Image src={src} alt=\&#x22;…\&#x22; ratio=\&#x22;3/2\&#x22; />&#x22;">
  <PlaceholderDemo />
</Preview>

Generate the data URI where the image is stored or built (a CMS field, or a build step with `sharp` or `plaiceholder`), at about 10px wide. The two in this page are 254 bytes each. A color works well when you already have a dominant color for the image.

The placeholder is removed once the image is fully opaque, not when it starts fading in. Removing it at the start would flash the page through the fade. Removing it afterwards means it never shows through a transparent PNG.

## Sizing [#sizing]

Reserve the box before the image arrives, or the page shifts when it does. There are two ways:

* **`width` and `height`**: a fixed-size image. The root takes `width` in pixels, gets the aspect ratio from both values, and still shrinks with `max-width: 100%`.
* **`ratio`**: a fluid image. Give the root a width with `className` (`w-full`, a grid cell…) and `ratio` sets the height. `ratio` takes a number (`16 / 9`) or the CSS string form (`"16/9"`), and it overrides the shape `width` and `height` would give.

<Preview align="block" code="&#x22;<Image src={src} alt=\&#x22;…\&#x22; ratio=\&#x22;1/1\&#x22; className=\&#x22;w-full\&#x22; />\n<Image src={src} alt=\&#x22;…\&#x22; ratio=\&#x22;4/3\&#x22; className=\&#x22;w-full\&#x22; />\n<Image src={src} alt=\&#x22;…\&#x22; ratio=\&#x22;16/9\&#x22; className=\&#x22;w-full\&#x22; />&#x22;">
  <div className="grid w-full grid-cols-3 items-start gap-3">
    {["1/1", "4/3", "16/9"].map((ratio) => (
          <Image
            key={ratio}
            src={`${PHOTO.lake}?w=640&q=70`}
            alt="A mountain lake at dawn"
            ratio={ratio}
            placeholder={BLUR.lake}
            className="w-full"
          />
        ))}
  </div>
</Preview>

A consumer `w-*` class always wins over `width`, so `width={1200} height={800} className="w-full"` gives a fluid image with the right shape and a srcset capped for 1200px.

## Fit and position [#fit-and-position]

`fit` is `object-fit`, and `cover` by default. `position` picks which part of the image stays visible when it's cropped. It accepts any CSS `object-position` value. The logical keywords `start`, `end`, `top-start`, `top-end`, `bottom-start` and `bottom-end` follow the text direction (see [Right-to-left](#right-to-left)). The blurred placeholder uses the same fit and position as the image, so the crossfade doesn't shift the picture.

<Preview align="block" code="&#x22;<Image src={src} alt=\&#x22;…\&#x22; ratio=\&#x22;1/1\&#x22; fit=\&#x22;cover\&#x22; />\n<Image src={src} alt=\&#x22;…\&#x22; ratio=\&#x22;1/1\&#x22; fit=\&#x22;contain\&#x22; />\n<Image src={src} alt=\&#x22;…\&#x22; ratio=\&#x22;1/1\&#x22; position=\&#x22;top\&#x22; />&#x22;">
  <div className="grid w-full grid-cols-3 items-start gap-3">
    <Image src="`${PHOTO.mountains}?w=640&q=70`" alt="Snow-capped mountains above the clouds" ratio="1/1" className="w-full" />

    <Image src="`${PHOTO.mountains}?w=640&q=70`" alt="Snow-capped mountains above the clouds" ratio="1/1" fit="contain" className="w-full" />

    <Image src="`${PHOTO.mountains}?w=640&q=70`" alt="Snow-capped mountains above the clouds" ratio="1/1" position="top" className="w-full" />
  </div>
</Preview>

## Radius [#radius]

`radius` uses the kit's radius scale and defaults to `xs`, like a button or an input. Use `radius="none"` for a square edge, for example when the image sits flush against a card's border.

<Preview align="block" code="&#x22;<Image radius=\&#x22;none\&#x22; … />\n<Image radius=\&#x22;xs\&#x22; … />\n<Image radius=\&#x22;md\&#x22; … />\n<Image radius=\&#x22;xl\&#x22; … />\n<Image radius=\&#x22;full\&#x22; … />&#x22;">
  <div className="flex flex-wrap items-center gap-3">
    {["none", "xs", "md", "xl", "full"].map((radius) => (
          <Image
            key={radius}
            src={`${PHOTO.lake}?w=192&q=70`}
            alt="A mountain lake at dawn"
            ratio="1/1"
            radius={radius}
            placeholder={BLUR.lake}
            className="w-20"
          />
        ))}
  </div>
</Preview>

## Loader [#loader]

The kit doesn't resize images. A `loader` tells it how your image service builds the URL for one width, and `Image` turns that into a srcset:

| Props              | srcset                                                                                                       |
| ------------------ | ------------------------------------------------------------------------------------------------------------ |
| No `loader`        | None: `src` is used as is                                                                                    |
| `loader` + `sizes` | `w` descriptors from 16px to 3840px, up to twice `width` when you set it. The browser picks one from `sizes` |
| `loader` + `width` | `1x` and `2x`                                                                                                |
| `loader` alone     | As with `sizes="100vw"`                                                                                      |

<Preview code="&#x22;import { Image, type ImageLoader } from '@42-central/ui-react/image';\n\nconst unsplash: ImageLoader = ({ src, width, quality = 75 }) =>\n  `${src}?w=${width}&q=${quality}&auto=format`;\n\n<Image\n  src=\&#x22;https://images.unsplash.com/photo-1506905925346-21bda4d32df4\&#x22;\n  alt=\&#x22;Snow-capped mountains above the clouds\&#x22;\n  loader={unsplash}\n  sizes=\&#x22;(min-width: 768px) 640px, 100vw\&#x22;\n  quality={70}\n  ratio=\&#x22;16/9\&#x22;\n/>&#x22;">
  <LoaderDemo />
</Preview>

The loader is passed per image. Wrap `Image` in your own component when every image in an app goes through the same service. `fallbackSrc` never goes through the loader, so a misconfigured loader can't break the fallback too.

## Priority [#priority]

Set `priority` on the image that's likely to be the largest thing in the first viewport, such as a hero or the main product photo. That image:

* is preloaded from the document head through `ReactDOM.preload`, with its srcset and sizes,
* is fetched eagerly with `fetchpriority="high"`,
* is never hidden and never fades in.

```tsx
<Image src={hero.url} alt={hero.alt} priority loader={cdn} sizes="100vw" ratio="21/9" className="w-full" />
```

Every other image is `loading="lazy"` and `decoding="async"`. Leave `priority` off everywhere else: preloading everything is the same as preloading nothing.

<Callout type="info" title="Why priority skips the fade">
  The fade needs React: the image stays transparent until React has hydrated and seen it load. So a non-priority image appears at hydration or at load, whichever comes later. The placeholder makes that wait look like loading rather than a blank space. The largest image in the first viewport is the one your [LCP](https://web.dev/articles/lcp) is measured on, so it should never wait for JavaScript. That's what `priority` does.
</Callout>

## Fallbacks [#fallbacks]

When `src` fails, `fallbackSrc` is tried once, as is. When that fails too, or when there's no `src` at all, the root shows the `fallback`: an icon by default, or any node you pass. A failed source is remembered, so the fallback never loops.

When the image fails and you passed a `placeholder`, the placeholder stays under the fallback: a blur stays blurred and a color stays the color, as a hint of what should be there. The fallback's content then sits on a small chip, so it stays legible over a light or dark blur in either theme. Without a placeholder, the fallback is a plain translucent surface. The skeleton pulse is never kept, because a shimmer means "loading", not "failed". With no `src` at all there's nothing to hint at, so the placeholder isn't shown.

<Preview code="&#x22;<Image src={broken} fallbackSrc=\&#x22;/placeholder.jpg\&#x22; alt=\&#x22;A mountain lake at dawn\&#x22; ratio=\&#x22;1/1\&#x22; />\n<Image src={broken} alt=\&#x22;A mountain lake at dawn\&#x22; ratio=\&#x22;1/1\&#x22; placeholder={blurDataUri} />\n<Image src={broken} alt=\&#x22;A mountain lake at dawn\&#x22; ratio=\&#x22;1/1\&#x22; placeholder=\&#x22;#4b5f80\&#x22; fallback={<span>Photo unavailable</span>} />\n<Image src={broken} alt=\&#x22;A mountain lake at dawn\&#x22; ratio=\&#x22;1/1\&#x22; />\n<Image alt=\&#x22;Profile photo\&#x22; ratio=\&#x22;1/1\&#x22; placeholder=\&#x22;#4b5f80\&#x22; />&#x22;">
  <FallbackDemo />
</Preview>

### Icons [#icons]

`icons` replaces the kit's icons. Image has one, `fallback`: the broken picture drawn when there's nothing to show. Set it to `null` to leave the box empty, so a kept placeholder shows on its own, with no chip. A `fallback` node wins over `icons.fallback`: use `fallback` for content, such as a line of text, and `icons` to swap only the glyph. The default draws the kit's 1.5px line; yours is drawn as given, so add `className={iconStroke}` (from `@42/ui-react`) to match it.

<Preview code="&#x22;<Image src={broken} alt=\&#x22;…\&#x22; ratio=\&#x22;1/1\&#x22; placeholder=\&#x22;#4b5f80\&#x22; icons={{ fallback: <CameraOff className={iconStroke} /> }} />\n<Image alt=\&#x22;…\&#x22; ratio=\&#x22;1/1\&#x22; icons={{ fallback: <MountainSnow className={iconStroke} /> }} />\n<Image src={broken} alt=\&#x22;…\&#x22; ratio=\&#x22;1/1\&#x22; placeholder={blurDataUri} icons={{ fallback: null }} />&#x22;">
  <IconsDemo />
</Preview>

## Right-to-left [#right-to-left]

`position="start"` crops to the inline-start edge: the left in a left-to-right page, and the right in a right-to-left one. The page's direction toggle flips every demo on this page; this one shows both directions at once.

<Preview code="&#x22;<div dir=\&#x22;ltr\&#x22;><Image src={src} alt=\&#x22;…\&#x22; ratio=\&#x22;1/1\&#x22; position=\&#x22;start\&#x22; /></div>\n<div dir=\&#x22;rtl\&#x22;><Image src={src} alt=\&#x22;…\&#x22; ratio=\&#x22;1/1\&#x22; position=\&#x22;start\&#x22; /></div>&#x22;">
  <DirectionDemo />
</Preview>

## API [#api]

<TypeTable
  type="{
  src: {
    type: &#x22;string&#x22;,
    description: 'Image URL. Without one, the fallback shows and the status is &#x22;empty&#x22;.',
  },
  alt: {
    type: &#x22;string&#x22;,
    required: true,
    description:
      'Describes the image. Required: pass &#x22;&#x22; for a decorative image. Also labels the fallback.',
  },
  width: {
    type: &#x22;number&#x22;,
    description:
      &#x22;Intrinsic width in px. Sizes the box, and caps a sizes-driven srcset at twice this width.&#x22;,
  },
  height: {
    type: &#x22;number&#x22;,
    description: &#x22;Intrinsic height in px. With width, gives the box its aspect ratio.&#x22;,
  },
  ratio: {
    type: &#x22;number | `${number}/${number}`&#x22;,
    description: &#x22;Aspect ratio of the box. Overrides the shape from width and height.&#x22;,
  },
  fit: {
    type: &#x22;'cover' | 'contain' | 'fill' | 'none' | 'scale-down'&#x22;,
    default: &#x22;'cover'&#x22;,
    description: &#x22;How the image fills its box (object-fit).&#x22;,
  },
  position: {
    type: &#x22;'center' | 'top' | 'bottom' | 'start' | 'end' | 'top-start' | 'top-end' | 'bottom-start' | 'bottom-end' | (string & {})&#x22;,
    default: &#x22;'center'&#x22;,
    description:
      &#x22;Which part stays visible when cropped. Logical keywords follow dir; any object-position value works.&#x22;,
  },
  sizes: {
    type: &#x22;string&#x22;,
    description: &#x22;The rendered width per breakpoint, for the loader's w-descriptor srcset.&#x22;,
  },
  quality: {
    type: &#x22;number&#x22;,
    description: &#x22;Passed to the loader.&#x22;,
  },
  loader: {
    type: &#x22;({ src, width, quality }) => string&#x22;,
    description: &#x22;Builds a CDN URL for one width. Turns on the srcset.&#x22;,
  },
  priority: {
    type: &#x22;boolean&#x22;,
    default: &#x22;false&#x22;,
    description:
      &#x22;For the main image in the first viewport: preloaded, fetched eagerly with high priority, never hidden or faded.&#x22;,
  },
  placeholder: {
    type: &#x22;string&#x22;,
    description:
      &#x22;A CSS color, or a tiny data: URI shown blurred. Kept under the fallback when the image fails. Without one, a skeleton pulse.&#x22;,
  },
  fallbackSrc: {
    type: &#x22;string&#x22;,
    description: &#x22;Tried once, as is (no loader), when src fails.&#x22;,
  },
  fallback: {
    type: &#x22;ReactNode&#x22;,
    default: &#x22;icons.fallback&#x22;,
    description: &#x22;Shown when there's no src, or when every source failed. Wins over icons.fallback.&#x22;,
  },
  icons: {
    type: &#x22;{ fallback?: ReactNode }&#x22;,
    default: &#x22;{ fallback: <ImageOff /> }&#x22;,
    description: &#x22;Replaces the default icons; a missing key keeps its default. null leaves the box empty.&#x22;,
  },
  radius: {
    type: &#x22;'none' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'full'&#x22;,
    default: &#x22;'xs'&#x22;,
    description: &#x22;Corner radius.&#x22;,
  },
  onStatusChange: {
    type: &#x22;(status: 'empty' | 'loading' | 'loaded' | 'error') => void&#x22;,
    description: &#x22;Called on every status change after the first render.&#x22;,
  },
  className: {
    type: &#x22;string&#x22;,
    description: &#x22;Applies to the root, which is the visible box. Size the image here.&#x22;,
  },
  style: {
    type: &#x22;CSSProperties&#x22;,
    description: &#x22;Applies to the root.&#x22;,
  },
  classNames: {
    type: &#x22;{ root?, image?, fallback? }&#x22;,
    description: &#x22;Per-part class overrides.&#x22;,
  },
  ref: {
    type: &#x22;Ref<HTMLImageElement>&#x22;,
    description: &#x22;The <img>. Null while the fallback shows.&#x22;,
  },
  testId: {
    type: &#x22;string&#x22;,
    description:
      &#x22;Renders as data-testid on the root, which is always there, even while the fallback shows. See [Testing](/docs/testing).&#x22;,
  },
}"
/>

Any other `<img>` attribute (`crossOrigin`, `referrerPolicy`, `draggable`, `onLoad`, `onError`…) is passed to the `<img>`. `loading`, `fetchPriority`, `srcSet` and `decoding` are set by the component.

No `asChild`: `Image` renders two elements, and a child could only replace one of them.

## Accessibility [#accessibility]

* **`alt` is required.** Leaving it out is a type error. Describe what the image shows in its context, or pass `alt=""` when the image is decorative, so screen readers skip it.
* **The fallback keeps the label.** When the image fails or there's no `src`, the fallback has `role="img"` and `aria-label` set to `alt`, so the image is still announced. With `alt=""`, the fallback is `aria-hidden`.
* **No broken-image icon or alt-text flash.** The `<img>` is transparent while it loads, and its text is transparent too, so a browser never paints its alt text over the placeholder. A failed `<img>` is removed and replaced by the fallback.
* **Not interactive.** `Image` has no role of its own and takes no focus. Wrap it in a link or a button when it should do something, and give that the accessible name.
* **Reduced motion.** With `prefers-reduced-motion: reduce`, the image appears without a fade and the skeleton doesn't pulse.
* **Forced colors.** Backgrounds are dropped in Windows High Contrast, so the fallback gets a `CanvasText` border, and its icon uses the text color. Over a kept placeholder, the chip behind the icon becomes an opaque `Canvas` surface.
* **Right-to-left.** The box uses logical properties only, and `position`'s `start`/`end` keywords follow the direction.

## Internationalization [#internationalization]

`Image` renders no text of its own, so it has no `translations` prop. Its only user-facing string is `alt`, which is your content. Translate it like the rest of the page, for example from your message catalog:

```tsx
<Image src={photo.url} alt={t('gallery.photoAlt', { place: photo.place })} ratio="3/2" />
```

A custom `fallback` that contains text is your content too, so translate it the same way.
