<!--
  GENERATED FILE - do not edit by hand.
  Source of truth: src/components/mds-img/ in the magma monorepo (usage/*.md and the
  component's JSDoc). Regenerated on every stencil build (scripts/component-docs.ts).
-->

# mds-img

> `@maggioli-design-system/magma` 2.15.0. Shadow DOM: style it only through the public CSS custom properties and the parts below.

The `<mds-img>` web component is the responsive image primitive of the Magma Design System. It wraps the native `<img>` element, adding accessible defaults, a built-in error/fallback state, and data-consumption-aware source selection driven by the user's bandwidth preference.

#### Semantic Behavior

- **Automatic alt fallback**: When `alt` is omitted, it is derived from the trailing filename segment of `src`. An empty `alt` (`alt=""`) marks the image as decorative: the host takes `role="presentation"` and no name, and assistive technologies skip it.
- **Error state**: A failed load swaps the picture for a broken-image icon plus the `alt` text, emitting `mdsImgLoadError`; changing `src` clears the error and retries.
- **Load events**: `mdsImgLoadSuccess` and `mdsImgLoadError` both carry the underlying `HTMLImageElement` in their detail, letting consumers read natural dimensions or react to failures.
- **Consumption-aware loading**: When `srcsetConsumption` is set, the component reads the consumption preference the page applies, the one `mds-pref-consumption` publishes, and follows a change; on a page without that controller it reads the stored choice (the `mdsPrefConsumption` key). It picks the matching source; under the `low` preference the image is deferred behind a "click to load" placeholder until the user opts in.
- **The box before hydration**: on a page that does not render the components on the server, `hydrated.css` gives the host its box before the component hydrates, as a native `<img>` reserves its room before the picture loads: as wide as its container, and with `width` and `height`, as tall as their ratio says. The page below it does not move when the component hydrates. The ratio needs typed `attr()`, in Chromium since 133; in a browser without it the host stays 0 px tall until it hydrates, and a page that needs the room there too gives the host the ratio itself (`aspect-ratio`).
- **Localized placeholder**: The deferred-load placeholder label is resolved through the shared locale system (el/en/es/it).

#### Properties & Visual Configurations

Most props are pass-throughs to the native image attributes of the same name (`src`, `srcset`, `sizes`, `width`, `height`, `loading`, `decoding`, `crossorigin`, `referrerpolicy`) and behave as in HTML; pick values as you would for a plain `<img>`. `crossorigin`, `referrerpolicy` and `decoding` have no default: unset, the image is fetched without CORS and with the referrer policy of the page, and the browser chooses how to decode it. `loading` defaults to `'lazy'`, so opt into `'eager'` only for above-the-fold imagery.

#### Other behavioral props

- **`srcsetConsumption`** is a comma-separated map of sources tagged `low` / `medium` / `high` (e.g. `"img-bw.jpg low, img.jpg medium, img@2x.jpg high"`). Unlike native `srcset`, the bucket is chosen by the stored consumption preference rather than viewport, letting low-bandwidth users save data by deferring or downgrading the image.
- **`alt`** supply meaningful text for content images, since it doubles as both the accessible name and the visible label in the error and deferred-load states.

## Read next

- [`examples.md`](examples.md) - correct usage, from the most common case
- [`mistakes.md`](mistakes.md) - the wrong forms, each with the right one
- [Storybook](https://magma-design-system.github.io/magma/storybook/?path=/docs/ui-image--docs) - the live examples, in the published Storybook of `main`
- [`conventions.md`](../../../../agents/conventions.md), [`variants.md`](../../../../agents/variants.md), [`mistakes.md`](../../../../agents/mistakes.md) - the rules shared by every component

## Props

| Prop | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `alt` | `alt` | `string` | - | Specifies an alternate text for an image. An empty `alt` marks the image as decorative, left out by assistive technologies; without `alt` the image is named after its file |
| `crossorigin` | `crossorigin` | `"anonymous" \| "use-credentials" \| undefined` | - | Fetches the image with CORS, as the `crossorigin` attribute of `<img>`: a third-party image can then be used with canvas, if its site allows it. Unset, the image is fetched without CORS |
| `decoding` | `decoding` | `"async" \| "auto" \| "sync" \| undefined` | - | How the browser decodes the picture, as the `decoding` attribute of `<img>`: `async` apart from the rest of the page, which then shows without waiting for it, as a large picture lower on the page can; `sync` with it; `auto` as the browser chooses. Unset, the browser chooses |
| `height` | `height` | `string \| undefined` | - | The height of the picture, in pixels, as the `height` attribute of `<img>`. With `width`, its ratio gives the host its box before the component hydrates, as a native `<img>` reserves its room before the picture loads (hydrated.css, where the browser supports typed `attr()`) |
| `loading` | `loading` | `"eager" \| "lazy" \| undefined` | `'lazy'` | Specifies whether a browser should load an image immediately or to defer loading of images until some conditions are met. |
| `referrerpolicy` | `referrerpolicy` | `"no-referrer" \| "no-referrer-when-downgrade" \| "origin" \| "origin-when-cross-origin" \| "unsafe-url" \| undefined` | - | Specifies which referrer information to use when fetching an image, as the `referrerpolicy` attribute of `<img>`. Unset, the policy of the page applies |
| `sizes` | `sizes` | `string \| undefined` | - | One or more strings separated by commas, indicating a set of source sizes. https://medium.com/@MRWwebDesign/responsive-images-the-sizes-attribute-and-unexpected-image-sizes-882a2eadb6db |
| `src` | `src` | `string \| undefined` | - | Specifies the path to the image |
| `srcset` | `srcset` | `string \| undefined` | - | Specifies a list of image files to use in different situations. Defines multiple sizes of the same image, allowing the browser to select the appropriate image source. |
| `srcsetConsumption` | `srcset-consumption` | `string \| undefined` | - | Specifies a list of image files to use in different situations: comma-separated, each file followed by the consumption level it is for (`low`, `medium`, `high`), so the component picks the image source that matches the consumption configuration. |
| `width` | `width` | `string \| undefined` | - | The width of the picture, in pixels, as the `width` attribute of `<img>`. With `height`, its ratio gives the host its box before the component hydrates |

## Events

| Event | Detail | Description |
| --- | --- | --- |
| `mdsImgLoadError` | `MdsImgEventDetail` | Emits when the image is not loaded |
| `mdsImgLoadSuccess` | `MdsImgEventDetail` | Emits when the image is successfully loaded |

## Parts

| Part | Description |
| --- | --- |
| `media` | The native `<img>` that shows the picture |

## CSS custom properties

| Property | Description |
| --- | --- |
| `--mds-img-background` | The background color of the image container. |
| `--mds-img-color` | The foreground/text color used inside the image component. |

Only these are public. Its other 4 are internal: they may change in any release, do not set them.

## Dependencies

Uses: `mds-icon`, `mds-text`, `mds-button`.
Used by: `mds-avatar`, `mds-file-preview`, `mds-push-notification-item`.
