Wrap one image, optionally inside a picture or link. The component adds a “View alt text” button only when the image has nonempty alt text. The non-modal dialog stays over the image; the rest of the page remains interactive and Tab can leave the panel. Click the opener again, or press Escape while focus is inside the wrapper, to dismiss it. The separate ✕ Close button appears in the dialog’s corner when it receives keyboard focus, without changing the panel’s size.
Start with an image
The button opens a command-driven dialog. The component never replaces or clones the image.
<img-alt-overlay>
<img src="bicycle.svg"
width="640" height="360"
alt="A red bicycle beside a blue wall, with a basket on its handlebars.">
</img-alt-overlay>
Translate the controls
button-label, dialog-label, and
close-label replace all generated wording.
They’re plain text; lang and
dir inherit normally.
<img-alt-overlay lang="fr"
button-label="Voir le texte alternatif"
dialog-label="Description de l’image"
close-label="Fermer">
<img src="bicycle.svg" width="640" height="360"
alt="Un vélo rouge devant un mur bleu, avec un panier sur le guidon.">
</img-alt-overlay>
<img-alt-overlay lang="ar" dir="rtl"
button-label="عرض النص البديل"
dialog-label="وصف الصورة"
close-label="إغلاق">
<img src="bicycle.svg" width="640" height="360"
alt="دراجة حمراء أمام جدار أزرق، وسلة على المقود.">
</img-alt-overlay>
Keep responsive images and links
Put an image link inside the wrapper, not around it. The generated button remains outside the link. Keep captions outside the image wrapper.
<figure>
<img-alt-overlay>
<a href="bicycle.svg">
<picture>
<source media="(min-width: 40em)"
srcset="bicycle.svg">
<img src="bicycle.svg" width="640"
height="360" alt="View the red bicycle illustration.">
</picture>
</a>
</img-alt-overlay>
<figcaption>A neighborhood bicycle.</figcaption>
</figure>
Theme the overlay
The optional stylesheet centers the description and gives
the panel a minimum block size of 30% of its available
image-overlay area, with a 4rem floor for the Close button
and its focus ring when space permits. Override the
percentage with
--img-alt-dialog-min-block-size; this example
uses 45%. Without the stylesheet, the same native dialog
works with browser-default presentation.
img-alt-overlay.bright {
--img-alt-dialog-min-block-size: 45%;
--img-alt-button-background: #fff;
--img-alt-button-color: #132d46;
--img-alt-dialog-background: #f0f7ff;
--img-alt-dialog-color: #132d46;
}
Set all four logical insets
Use --img-alt-button-inset-* and
--img-alt-overlay-inset-* to position the
opener and overlay area independently. Each supports
block-start, block-end,
inline-start, and inline-end.
Inline sides follow the writing direction. Set the opposing
side to auto when moving a button to another
edge.
Defaults place the opener 0.5rem from block start and inline end. The overlay area starts 3.75rem from block start, leaving room for the opener, with 0.5rem on its other edges. The panel is centered inside that area. This example puts the opener at block end/inline start and reserves space for it below the panel.
img-alt-overlay.repositioned {
--img-alt-button-inset-block-start: auto;
--img-alt-button-inset-block-end: 0.75rem;
--img-alt-button-inset-inline-start: 0.75rem;
--img-alt-button-inset-inline-end: auto;
--img-alt-overlay-inset-block-start: 0.75rem;
--img-alt-overlay-inset-block-end: 4rem;
--img-alt-overlay-inset-inline-start: 0.75rem;
--img-alt-overlay-inset-inline-end: 0.75rem;
}
Match the website overlay
Add class="site-style" to the wrapper for a
full-image-width panel with white text on black, no border,
and square corners, like the
overlay on Aaron’s website. Setting both inline insets to zero lets the panel reach
both image edges; the opener keeps its own inset and remains
clickable.
img-alt-overlay.site-style {
--img-alt-dialog-background: #000;
--img-alt-dialog-color: #fff;
--img-alt-overlay-inset-block-end: 0;
--img-alt-overlay-inset-inline-start: 0;
--img-alt-overlay-inset-inline-end: 0;
}
img-alt-overlay.site-style [data-alt-dialog] {
border: 0;
border-radius: 0;
}
Slide in from block end with CSS
This version uses class="site-style slide-up".
It anchors the panel to block end and both inline edges,
then slides and fades it in and out over 300ms. The clipping
layer keeps the motion inside the image. A 4rem minimum
block size leaves room for the Close button and its focus
ring, even with a short description.
@starting-style defines the entry state, and
allow-discrete keeps the dialog rendered until
the exit finishes in supporting browsers; otherwise closing
is immediate. Reduced-motion preferences skip the
transitions. No JavaScript animation code is involved.
img-alt-overlay.slide-up {
--img-alt-overlay-inset-block-start: auto;
--img-alt-overlay-inset-block-end: 0;
--img-alt-overlay-inset-inline-start: 0;
--img-alt-overlay-inset-inline-end: 0;
}
img-alt-overlay.slide-up [data-alt-ui] {
max-block-size: calc(100% - 3.75rem);
overflow: clip;
}
img-alt-overlay.slide-up [data-alt-dialog] {
min-block-size: 4rem;
margin: 0;
}
@media (prefers-reduced-motion: no-preference) {
img-alt-overlay.slide-up [data-alt-dialog] {
opacity: 0;
transform: translateY(100%);
transition:
display 300ms allow-discrete,
opacity 300ms ease,
transform 300ms ease;
}
img-alt-overlay.slide-up [data-alt-dialog][open] {
opacity: 1;
transform: translateY(0);
}
@starting-style {
img-alt-overlay.slide-up [data-alt-dialog][open] {
opacity: 0;
transform: translateY(100%);
}
}
}
Leave decorative images alone
Empty alt text is intentional for decorative images, so no button appears. Missing alt text also produces no button, but authors still need to supply an appropriate alternative or mark the image decorative.
<img-alt-overlay>
<img src="bicycle.svg" width="640"
height="360" alt="">
</img-alt-overlay>
Loading, failure, and long descriptions
Nothing hides the image while JavaScript loads. Disable JavaScript or block the module request to check the native fallback. The component doesn’t fetch images, change loading hints, or depend on image load. Long descriptions wrap and scroll inside the dialog.
<img-alt-overlay>
<img src="missing-image.svg" width="640"
height="360"
alt="A red bicycle with a basket. The front wheel points slightly to the left; a blue wall fills the background.">
</img-alt-overlay>
Invalid configuration emits
img-alt-overlay:error and leaves authored
markup alone. See the
versioned CDN example
for class-only imports; it needs the package to be published
first.