Image
Image with lazy loading, a loading placeholder, an error fallback, click-to-load for untrusted URLs and a full-screen preview.
Attributes
| Name | Type | Default | Description |
|---|---|---|---|
src | string | — | Image URL. |
srcset | string | undefined | — | Candidate sources, passed to the inner image. |
sizes | string | undefined | — | Source sizes for srcset, passed to the inner image. |
alt | string | — | Alternative text. Also the label of the placeholder and the fallback. |
width | number | undefined | — | Intrinsic width in pixels: reserves the space and the ratio before the image loads. |
height | number | undefined | — | Intrinsic height in pixels: reserves the ratio before the image loads. |
loading | ImageLoading | lazy | lazy (default) fetches near the viewport, eager at once, click only after the reader activates the placeholder. |
placeholder-src | string | undefined | — | Tiny preview shown blurred while loading. Under loading="click" only a data: URI is used, since it makes no request. |
referrerpolicy | ReferrerPolicy | undefined | — | Referrer policy of the request, passed to the inner image. |
crossorigin | string | undefined | — | CORS mode of the request, passed to the inner image. |
preview | boolean | false | Clicking the loaded image opens it full screen. |
Slots
| Name | Description |
|---|---|
placeholder | Replaces the loading shimmer. |
fallback | Replaces the error state (icon and alternative text). |
load-label | Replaces the "Load image" label of the click-to-load placeholder. |
Events
| Name | Type | Description |
|---|---|---|
load | Event | The image has loaded (re-dispatched from the inner image). |
error | Event | The image failed to load (re-dispatched); the fallback shows. |
load-request | CustomEvent<{ src: string }> | Cancelable. The reader activated a click-to-load placeholder; cancel it to keep the image blocked. |
preview-open | CustomEvent<{ src: string }> | Cancelable. The reader opened the preview; cancel it to show your own viewer. |
CSS parts
Shadow DOM styling hooks used with ::part(name). Prefer CSS custom properties when they cover the change.
| Name | Description |
|---|---|
image | The native image element once it is requested. |
placeholder | Loading surface region shown until the image has loaded, containing the placeholder slot or its shimmer fallback. |
fallback | Error surface containing the fallback slot or the default icon and alternative text. |
load-button | Click-to-load placeholder button containing the load-label slot, the alternative text and the host. |
CSS custom properties
Grouped by semantic target and state from --c2-<component>__<target>__<state>--<property>. Click a group to collapse it; click a name to copy it.
| Name | Type | Default | Description |
|---|---|---|---|
--c2-image--width | width | auto | Width of the image box. Defaults to the width attribute, capped at the container. |
--c2-image--aspect-ratio | aspect-ratio | auto | Overrides the ratio taken from width and height. |
--c2-image--object-fit | object-fit | cover | |
--c2-image--border-radius | border-radius | 0 | |
--c2-image__enter--duration | time | 200ms | Fade from the placeholder to the loaded image. |
--c2-image__placeholder--background | background | #fafafa | |
--c2-image__placeholder--color | color | #71717a | |
--c2-image__placeholder--font-size | pixel | 12px | |
--c2-image__placeholder--min-height | pixel | 120px | Height of the placeholder, fallback and click-to-load surfaces when no ratio is known. |
--c2-image__placeholder--min-width | pixel | 200px | |
--c2-image__shimmer--color | color | rgba(255, 255, 255, 0.6) | |
--c2-image__shimmer--duration | time | 1.2s | |
--c2-image__blur--radius | pixel | 16px | Blur applied to placeholder-src. |
--c2-image__icon--size | pixel | 20px | |
--c2-image__fallback--background | background | #fafafa | |
--c2-image__fallback--color | color | #71717a | |
--c2-image__load-button--border | border | 1px dashed #bcbcc6 | |
--c2-image__load-button--color | color | #18181b | |
--c2-image__load-button__hover--background | background | #e4e4e7 | |
--c2-image__load-button__focus--outline | outline | 2px solid rgba(2, 101, 220, 0.4) | |
--c2-image__preview--width | pixel | min(1080px, calc(100vw - 32px)) | Width of the preview dialog. |
--c2-image__preview__focus--outline | outline | 2px solid rgba(2, 101, 220, 0.4) | |