<!--
  GENERATED FILE - do not edit by hand.
  Source of truth: src/components/mds-img/usage/ in the magma monorepo.
-->

# mds-img - examples

> The API of this component: [`AGENTS.md`](AGENTS.md).

Correct and idiomatic ways to use the `<mds-img>` component, ordered from most common to most specialized. The examples assume a working knowledge of the shared component rules in [`agents/conventions.md`](../../../../agents/conventions.md).

#### Basic Image with Alt Text

The canonical form. Always supply a meaningful `alt` value for content images - it doubles as the accessible name and as the visible label in both the error state and the deferred-load placeholder.

```html
<mds-img src="/assets/img/copertina-corso.jpg" alt="Copertina del corso di formazione"></mds-img>
```

#### Responsive Image with `srcset` and `sizes`

Pass the standard `srcset` and `sizes` attributes exactly as you would on a native `<img>`. The browser picks the best source based on viewport and device pixel ratio.

```html
<mds-img
  src="/assets/img/hero-800.jpg"
  srcset="/assets/img/hero-400.jpg 400w, /assets/img/hero-800.jpg 800w, /assets/img/hero-1600.jpg 1600w"
  sizes="(max-width: 600px) 400px, (max-width: 1200px) 800px, 1600px"
  alt="Vista panoramica del centro storico"
></mds-img>
```

#### Eager Loading for Above-the-Fold Images

`loading` defaults to `"lazy"`. Set it to `"eager"` only for images that are immediately visible on page load, such as hero images or content at the top of the first screen.

```html
<mds-img
  src="/assets/img/hero-principale.jpg"
  alt="Hero della pagina principale"
  loading="eager"
></mds-img>
```

#### A Large Picture Lower on the Page

`decoding="async"` lets the browser decode the picture apart from the rest of the page, which then shows without waiting for it. With `width` and `height` the host keeps the room of the picture before the component hydrates and before the picture loads.

```html
<mds-img
  src="/assets/img/studio.webp"
  alt="Magma Studio"
  width="1440"
  height="900"
  decoding="async"
></mds-img>
```

#### Consumption-Aware Sources with `srcset-consumption`

Use `srcset-consumption` to serve a different image source depending on the user's bandwidth preference stored via `mds-pref-consumption`. Tag each source with `low`, `medium`, or `high`. Under the `low` preference the image is deferred behind a "Carica immagine" placeholder until the user clicks.

```html
<mds-img
  src="/assets/img/copertina-libro.jpg"
  srcset-consumption="/assets/img/copertina-bw-1x.jpg low, /assets/img/copertina-1x.jpg medium, /assets/img/copertina-2x.jpg high"
  alt="Il nuovo codice dei contratti pubblici"
></mds-img>
```

#### Consumption-Aware with Fallback `src`

Provide `src` alongside `srcset-consumption` as the fallback used when a bucket is missing from the consumption map. Here `src` is the high-resolution file; only `low` and `medium` buckets are overridden.

```html
<mds-img
  src="/assets/img/copertina-2x.jpg"
  srcset-consumption="/assets/img/copertina-bw-1x.jpg low, /assets/img/copertina-1x.jpg medium"
  alt="Manuale pratico degli appalti pubblici"
></mds-img>
```

#### Reacting to Load Events

Listen to `mdsImgLoadSuccess` and `mdsImgLoadError` to react to image load outcomes. Both events carry the underlying `HTMLImageElement` in `event.detail.image`, allowing you to read natural dimensions or show fallback UI.

```html
<mds-img
  id="hero"
  src="/assets/img/hero-principale.jpg"
  alt="Immagine principale"
></mds-img>

<script>
  document.getElementById('hero').addEventListener('mdsImgLoadSuccess', (event) => {
    const img = event.detail.image;
    console.log('Caricata:', img.naturalWidth, 'x', img.naturalHeight);
  });

  document.getElementById('hero').addEventListener('mdsImgLoadError', (event) => {
    console.warn('Immagine non disponibile:', event.detail.image.src);
  });
</script>
```

#### Fixed Dimensions

Set `width` and `height` (as string pixel values) to reserve layout space and prevent content reflow during load: they give the inner image its aspect ratio, and the host the same box before the component hydrates. They do not fix the rendered size - the inner image follows the host, which is `display: flex; width: 100%` by default - so size the host with CSS when a fixed pixel size is intended.

```html
<mds-img
  src="/assets/img/thumbnail.jpg"
  alt="Anteprima documento"
  width="320"
  height="240"
></mds-img>
```

#### Styling Customization

Style the component through its public `--mds-img-*` CSS custom properties. Use the semantic color roles wrapped in `rgb(var(--magma-<role>))` so dark mode and high-contrast modes keep working.

```css
.card-copertina mds-img {
  --mds-img-background: rgb(var(--magma-accent-emphasis-hover));
  --mds-img-color: rgb(var(--magma-accent-surface));
}
```

#### Styling the Inner `<img>` via `::part(media)`

When a CSS property must be applied directly to the inner `<img>` element (for example `object-fit` or `border-radius`) use the documented `media` shadow part. This is the only supported way to pierce the shadow boundary.

```css
.copertina-libro mds-img::part(media) {
  border-radius: var(--magma-radius-md);
  object-fit: cover;
}
```
