Web component / Examples

<img-enlarge>

Enlarge an image without changing its link. Read the README for installation and API details.

One-off image enlargement

Wrap an image in <img-enlarge>. An Enlarge button appears, but only when there’s room to show the image larger. Use the button to open it; press Escape or the Close button to return focus to the Enlarge button. Without JavaScript the image still renders normally.

Images inside picture, figures, and links work too. The examples below show how to choose a different enlargement, add captions, and connect images in a gallery.

<script type="module" src="../define.js"></script>

<img-enlarge>
  <img src="./mountains.svg"
    alt="A trail between two green mountains beneath a golden sun.">
</img-enlarge>
A trail between two green mountains beneath a golden sun.

Force enlargement

Add force to show the Enlarge button even when the image can’t appear larger in the viewer.

<img-enlarge force>
  <img src="./mountains.svg"
    alt="A trail between two green mountains beneath a golden sun.">
</img-enlarge>
A trail between two green mountains beneath a golden sun.

Alternate enlarged versions

By default, the enlargement uses the original image. You can link to a larger file, let the browser choose from responsive images, or supply a separate enlargement.

Use an existing image link

Link the image to a larger file. The Enlarge button uses that file, while clicking the thumbnail still follows the link.

<img-enlarge>
  <a href="./mountains-large.svg">
    <img src="./mountains.svg" alt="A trail between green mountains.">
  </a>
</img-enlarge>
A trail between green mountains.

Let the browser choose the larger image

Keep srcset and sizes on the image and let the browser choose a larger candidate for the viewer.

<img-enlarge class="responsive-thumbnail">
  <img src="./mountains.svg?responsive=small"
    srcset="./mountains.svg?responsive=small 800w,
      ./mountains-large.svg?responsive=large 1200w"
    sizes="192px"
    width="800" height="400"
    alt="A trail between green mountains.">
</img-enlarge>

<style>
.responsive-thumbnail > img {
  inline-size: min(100%, 12rem);
}
</style>
A trail between green mountains.

Use the <picture> element

A picture can use different images at different window widths. Resize the window to try this example. The enlargement uses the responsive source chosen for that image.

<img-enlarge class="responsive-thumbnail">
  <picture>
    <source media="(min-width: 40rem)"
      srcset="./mountains.svg?picture=small 800w,
        ./mountains-large.svg?picture=large 1200w" sizes="192px">
    <img src="./mountains.svg" width="800" height="400"
      alt="A trail between green mountains.">
  </picture>
</img-enlarge>

<style>
.responsive-thumbnail > picture > img {
  inline-size: min(100%, 12rem);
}
</style>
A trail between green mountains.

Enlarge a specific image

Set src on the component when you want a different enlarged image without adding a link. It takes priority over the image’s link and responsive sources.

<img-enlarge src="./mountains-large.svg">
  <img src="./mountains.svg" alt="A trail between green mountains.">
</img-enlarge>
A trail between green mountains.

Declare several enlargement options

Use srcset on the component to give the viewer its own choice of image sizes. The thumbnail keeps its separate srcset and sizes.

<img-enlarge class="responsive-thumbnail" src="./mountains-large.svg"
  srcset="./mountains.svg?viewer=small 800w,
    ./mountains-large.svg?viewer=large 1200w">
  <img src="./mountains.svg?thumbnail=small"
    srcset="./mountains.svg?thumbnail=small 800w"
    sizes="192px" width="800" height="400"
    alt="A trail between green mountains.">
</img-enlarge>

<style>
.responsive-thumbnail > img {
  inline-size: min(100%, 12rem);
}
</style>
A trail between green mountains.

See the README’s image-source guide for source priorities and less common configurations.

Captions

Put the image in a figure with a figcaption and the enlargement will use that caption. Set caption on the component to use different text in the viewer. The original caption stays in place; an empty caption="" leaves the viewer caption out.

<img-enlarge caption="An afternoon on the mountain trail.">
  <figure>
    <img src="./mountains.svg" alt="A trail between green mountains.">
    <figcaption>A view from the trail.</figcaption>
  </figure>
</img-enlarge>
A trail between green mountains.
A view from the trail.

Rewrite or translate the labels

The default labels may not work for your application. You can rewrite the button text and messages using the associated *-label attribute on the component for a one-off change, or use a JSON configuration for shared labels.

The default Enlarge button label includes the image’s accessible name. Include {name} in your open-label template to keep that name in a rewritten or translated label. Leave it out for a fixed label, as in the first example.

Rewrite a label

Set open-label and close-label to use your application’s wording for the two buttons.

<img-enlarge open-label="View the trail photo"
  close-label="Back to the page">
  <img src="./mountains.svg" alt="A trail between green mountains.">
</img-enlarge>
A trail between green mountains.

Translate the labels

You can also translate every control and status message using shared JSON. Here, data-img-enlarge-config="french" applies the labels only to the gallery named french. The Canadian French fr-CA entry changes the Enlarge label; fr supplies the other labels. The first image overrides that shared Enlarge label with open-label="Agrandir le paysage".

For a page in one language, review the single-language example.

<script type="application/json" data-img-enlarge-config="french">
{
  "fr": {
    "open-label": "Agrandir l’image : {name}",
    "dialog-label": "Images agrandies",
    "close-label": "Fermer",
    "previous-label": "Image précédente",
    "next-label": "Image suivante",
    "loading-label": "Chargement de l’image",
    "error-label": "Impossible de charger l’image.",
    "retry-label": "Réessayer",
    "position-label": "{current} sur {total}"
  },
  "fr-CA": { "open-label": "Agrandir la photo : {name}" }
}
</script>

<div lang="fr-CA">
  <img-enlarge name="french" open-label="Agrandir le paysage"
    caption="Un sentier entre les montagnes.">
    <img src="./mountains.svg" alt="Un sentier entre deux montagnes vertes.">
  </img-enlarge>
  <img-enlarge name="french" caption="Un vélo pour la promenade.">
    <img src="./bicycle.svg" alt="Un vélo rouge avec un panier.">
  </img-enlarge>
</div>
Un sentier entre deux montagnes vertes. Un vélo rouge avec un panier.

Styling

Use a class on <img-enlarge> to alter that viewer’s look and feel. The README lists the styling hooks.

Basic styling

Use a class on the component to style one viewer. Here, the caption sits beside the image on wide screens and below it on narrow screens.

<img-enlarge class="editorial" force
  caption="An afternoon on the mountain trail.">
  <img src="./mountains.svg" alt="A trail through green mountains.">
</img-enlarge>

<style>
.editorial {
  --img-enlarge-dialog-padding: 4rem;
  --img-enlarge-caption-gap: 1.5rem;
}
.editorial .img-enlarge-slide {
  display: grid;
  grid-template-columns: minmax(0, 3fr) minmax(0, 2fr);
  gap: 1.5rem;
  align-items: center;
  text-align: start;
}
@media (max-width: 40rem) {
  .editorial { --img-enlarge-dialog-padding: 3.5rem; }
  .editorial .img-enlarge-slide { grid-template-columns: minmax(0, 1fr); }
}
</style>
A trail through green mountains.

Hide disabled navigation arrows

Set --disabled-button-visibility: hidden to hide the unavailable arrow at either end of the collection.

<img-enlarge class="hide-disabled" name="quiet-arrows">
  <img src="./mountains.svg" alt="A trail between green mountains.">
</img-enlarge>
<img-enlarge class="hide-disabled" name="quiet-arrows">
  <img src="./bicycle.svg" alt="A red bicycle with a basket.">
</img-enlarge>

<style>
.hide-disabled {
  --disabled-button-visibility: hidden;
}
</style>
A trail between green mountains. A red bicycle with a basket.