Everyday city bicycle
A step-through frame, a front basket, and room for an afternoon’s errands.
Web component / Examples
Enlarge an image without changing its link. Read the README for installation and API details.
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>
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>
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.
Link the image to a larger file. The Enlarge button uses that file, while clicking the thumbnail still follows the link.
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>
<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>
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>
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>
See the README’s image-source guide for source priorities and less common configurations.
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>
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.
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>
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>
A gallery lets you browse several images in one viewer. Use
one wrapper for a set of images, or connect separate
wrappers with a shared name.
Put several images inside one
<img-enlarge> to create a distinct
gallery.
In gallery mode, arrow buttons are added and the keyboard’s Left and Right arrow keys can be used to browse. Open either photo to start there; use Close or Escape to return to its Enlarge button.
<img-enlarge force open-label="Enlarge ride photo: {name}">
<figure>
<img src="./bicycle.svg" width="192" height="108"
alt="A red bicycle with a basket beside a pale stone path.">
<figcaption>The bicycle, ready for the ride.</figcaption>
</figure>
<figure>
<picture>
<source media="(min-width: 40rem)" srcset="./mountains-large.svg">
<img src="./mountains.svg" width="192" height="96"
alt="A trail between two green mountains beneath a golden sun.">
</picture>
<figcaption>The trail at the end of the afternoon.</figcaption>
</figure>
</img-enlarge>
Give separate <img-enlarge> elements
the same name to connect their images.
These bicycle photos share
name="city-bike". Open a detail photo to
browse all three, even if the larger bicycle image has
no Enlarge button. A wrapper without a
name stays in its own gallery.
<div class="product-gallery">
<img-enlarge name="city-bike"
caption="The city bicycle, ready for an afternoon ride."
open-label="Enlarge the city bicycle">
<a href="./bicycle.svg">
<img src="./bicycle.svg" alt="A red city bicycle with a basket.">
</a>
</img-enlarge>
<img-enlarge name="city-bike"
caption="The front basket has room for a small bag."
open-label="Enlarge the front basket">
<a href="./bicycle-basket.svg">
<img src="./bicycle-basket.svg" alt="A woven basket above the front wheel.">
</a>
</img-enlarge>
<img-enlarge name="city-bike"
caption="The rear wheel and red frame."
open-label="Enlarge the rear wheel">
<a href="./bicycle-wheel.svg">
<img src="./bicycle-wheel.svg" alt="The bicycle’s rear wheel and frame.">
</a>
</img-enlarge>
</div>
Set controls="buttons" to use HTML
navigation buttons in every browser. Otherwise, the
component uses native CSS scroll buttons where supported
and HTML buttons elsewhere.
<img-enlarge name="details" controls="buttons">
<img src="./bicycle.svg" alt="A red bicycle with a basket.">
</img-enlarge>
<img-enlarge name="details" controls="buttons">
<img src="./bicycle-basket.svg" alt="A woven basket above the front wheel.">
</img-enlarge>
Use a class on <img-enlarge> to alter
that viewer’s look and feel. The
README lists the styling
hooks.
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>
This gallery fades the overlay in and out and animates the change between images. The controls sit plainly against the canvas, with native keyboard focus indicators. Motion is disabled when you prefer reduced motion.
<img-enlarge class="animated-gallery gallery-thumbnails" force>
<figure>
<img src="./bicycle.svg" alt="A red bicycle with a basket.">
<figcaption>The bicycle, ready for a ride.</figcaption>
</figure>
<figure>
<img src="./bicycle-basket.svg" alt="A woven basket above the front wheel.">
<figcaption>The basket has room for a small bag.</figcaption>
</figure>
</img-enlarge>
<style>
.gallery-thumbnails {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 1rem;
}
.animated-gallery .img-enlarge-dialog {
border: 0;
opacity: 0;
transform: translateY(0.75rem);
transition: opacity 220ms ease, transform 220ms ease,
display 220ms allow-discrete, overlay 220ms allow-discrete;
}
.animated-gallery .img-enlarge-dialog[open] {
opacity: 1;
transform: translateY(0);
}
.animated-gallery .img-enlarge-dialog::backdrop {
opacity: 0;
transition: opacity 220ms ease, display 220ms allow-discrete,
overlay 220ms allow-discrete;
}
.animated-gallery .img-enlarge-dialog[open]::backdrop {
opacity: 1;
}
@supports not (overlay: auto) {
.animated-gallery .img-enlarge-dialog {
display: grid;
z-index: 1;
box-shadow: 0 0 0 100vmax rgb(0 0 0 / 75%);
}
.animated-gallery .img-enlarge-dialog::backdrop {
background: transparent;
}
}
@starting-style {
.animated-gallery .img-enlarge-dialog[open] {
opacity: 0;
transform: translateY(0.75rem);
}
.animated-gallery .img-enlarge-dialog[open]::backdrop {
opacity: 0;
}
}
.animated-gallery .img-enlarge-track {
scroll-behavior: smooth;
}
.animated-gallery .img-enlarge-dialog button {
color: CanvasText;
background: transparent;
border: 0;
}
.animated-gallery .img-enlarge-dialog button[aria-disabled='true'] {
color: GrayText;
}
@supports selector(::scroll-button(inline-start)) {
.animated-gallery .img-enlarge-track::scroll-button(*) {
color: CanvasText;
background: transparent;
border: 0;
}
.animated-gallery .img-enlarge-track::scroll-button(*):disabled {
color: GrayText;
}
}
.animated-gallery .img-enlarge-slide[aria-hidden='false'] .img-enlarge-image {
animation: gallery-image-in 220ms ease both;
}
@keyframes gallery-image-in {
from { opacity: 0; }
to { opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
.animated-gallery .img-enlarge-dialog,
.animated-gallery .img-enlarge-dialog::backdrop {
transition: none;
}
.animated-gallery .img-enlarge-track {
scroll-behavior: auto;
}
.animated-gallery .img-enlarge-slide[aria-hidden='false'] .img-enlarge-image {
animation: none;
}
}
</style>
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>