# Image

An image component displays visual content such as photographs, illustrations, or diagrams within an interface. The Image component provides a headless wrapper that ensures proper accessibility through required alt text. For a caption, put it in a `figure` with a `caption`.

**Status:** stable — exercised in composed page flows under e2e and axe, beyond the per-component checks.

The component renders a plain `<img>` element; a caption belongs to the `figure` around it, as a `caption`, which associates the two for assistive technology. The component also supports native lazy loading for performance optimization.

## Implementation Notes

- Renders a plain `<img>` element; for a caption, wrap it in `figure` with a `caption`
- The `alt` prop is required to enforce accessibility
- Rest props are spread onto the `<img>` element
- Supports the native `loading` attribute for lazy/eager loading control

## Props

- `src`: string (required) -- the image source URL
- `alt`: string (required) -- alternative text describing the image for screen readers and when the image fails to load
- `loading`: "lazy" | "eager" (optional) -- controls the native browser loading behavior
- `...restProps`: unknown -- additional attributes spread onto the root element

## Usage

```html
<image
  src="/images/stretch-marks.jpg"
  alt="Close-up of a person's skin showing pink and silver stretch marks across the abdomen"
/>
```

```html
<figure>
  <image
    src="/images/chickenpox-rash.jpg"
    alt="Small red spots on a child's chest and arms, typical of chickenpox"
  />
  <caption>
    Chickenpox starts with small, itchy spots. They can appear anywhere on the body.
  </caption>
</figure>
```

```html
<!-- Lazy-loaded image below the fold -->
<image
  src="/images/physiotherapy-exercise.jpg"
  alt="A person performing a seated hamstring stretch with one leg extended"
  loading="lazy"
/>
```

## Keyboard Interactions

None -- this component is a passive content element. Images are not interactive by default.

## ARIA

- `alt="..."` -- provides the accessible name for the image, required for all non-decorative images
- `figure` and `caption` (a `<figcaption>`) -- wrap the image when it needs a caption; the browser associates the two

## When to Use

- Use when an image meets a genuine user need, such as helping users identify a health condition or understand a procedure
- Use with descriptive alt text that conveys the same information as the image
- Use with a caption (via Figure and Caption) to explain what users should conclude from the image
- Use when users with access needs like dyslexia benefit from visual content alongside text

## When Not to Use

- Do not use decorative images that add no informational value -- they frustrate users, especially on transactional pages; use a CSS background image instead
- Do not use images containing embedded text -- screen readers cannot read text within images
- Do not arrange images side-by-side in a gallery layout -- stack vertically so users do not miss content
- Do not use when the information can be conveyed equally well through text alone

## Headless

This headless component provides an `<img>` element with required `alt` text, and native `loading` attribute support. The consumer provides all visual styling including sizing, borders, rounded corners, and responsive behavior.

## Styles

The consumer provides all CSS styling. The component renders with a `.image` class for targeting. No default styles are included — this is a fully headless component.

## Testing

- Verify the component renders an `<img>` element with class `image`
- Verify pass-through attributes are applied

## Advice

- **Designers**: Define consistent aspect ratios and sizing for images across the application. Provide placeholder or skeleton states for slow-loading images.
- **Developers**: Always write meaningful `alt` text that describes the image content. Use `loading="lazy"` for below-the-fold images to improve page performance.
- **Consumer CSS, three rules for `.image`**: `display: block` removes the small gap under an inline image (the line's baseline space); `max-inline-size: 100%` stops the image overflowing its container on narrow screens; `block-size: auto` keeps the aspect ratio when the width is limited. The component ships none of these, so add them in your own stylesheet.
- **Explicit `width` and `height` attributes**: set both on the image, in pixels, to the file's real dimensions. The browser uses them to reserve the right space before the file arrives, so the page does not shift as images load (the Cumulative Layout Shift measure). With `block-size: auto` above, the attributes set the aspect ratio only and the image still scales. Pass them through the rest props.
- For several sources, art direction or modern formats with a fallback, use `Picture`.

## Related components

- `image-input` — a graphical submit button that displays an image <input type="image">
- `image-file-input` — an input for selecting image files with preview
- `picture` — a responsive image wrapper that lets the browser choose among several sources

## Known limitations

None known.

## References

- WAI Images Tutorial: https://www.w3.org/WAI/tutorials/images/
- HTML figure element: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure

---

Lily™ and Lily Design System™ are trademarks.
