---
url: "https://web.dev/articles/baseline-in-action-image-gallery"
titulek: "How to implement an image gallery using Baseline features | Articles | web.dev"
h1: "How to implement an image gallery using Baseline features Stay organized with collections Save and categorize content based on your preferences."
popis: "Image galleries are a common user interface pattern on the web. Learn how to create one using Baseline features."
jazyk: "en"
kanonicka: "https://web.dev/articles/baseline-in-action-image-gallery"
drobecky: ["Articles", "How to implement an image gallery using Baseline features"]
zdroj: "live"
wayback_snapshot: ""
stazeno: "2026-09-13 22:27:03"
znaku: 8556
slov: 1413
kodovani: "utf-8"
sha256: "8415706edbc9174fbf29c2b4682adb81ed3cd063946d829306a588d1e1164fbc"
---
How to implement an image gallery using Baseline features
Stay organized with collections
Save and categorize content based on your preferences.
Stacy Kvernmo
Published: October 29, 2025
From image sharing sites to online stores, image galleries are a common pattern on the web. Images can be very data heavy, and loading images can make the page take a long time to load. In addition, users have high expectations around the usability of galleries, so it's common to add additional libraries that further increase page size. The web platform has added support for many of the underlying pieces of an image gallery. So, what does it look like to code an image gallery with Baseline features? This demo showcases a variety of techniques that you can use to do just that. How to deal with large image sizes Images can be some of the largest assets you ask your users to download, and image galleries by their nature often have many images. This demo uses a couple of Baseline features to help reduce the performance impact on users. Lazy loading Loading all of the images when the page loads can make the other content slower to load overall. It's a useful pattern to defer loading of images that aren't on the screen until they are on the screen. But it's a balance—you don't want users to have to wait for images to load as they scroll. You want them to already be loaded when they scroll into view. JavaScript libraries have made this possible in the past, and you could also roll your own with an IntersectionObserver. However, both options mean shipping more code, and leave more room for bugs. Browsers have made this possible with an HTML-based progressive enhancement: By adding `loading="lazy"` to `
` elements, the browser will defer loading those images until they are needed. If you have a sense of how many images will be seen on screens when the page is first loaded, you can set `loading="eager"` on the first few images, to make sure that those are loaded immediately. Because `loading="eager"` is the default, you can also just omit the attribute for those images. If you view the Network panel in your browser's developer tools as you load the demo, only the first six will load immediately. Depending on your screen size and how many are visible, several others may be loaded. As you scroll, the browser will load the remaining images. Lazy loading images works with more than just scrolling. If an image is initially hidden—for example, in a dialog—the image won't load until the dialog is opened. This would be useful in an image gallery if you are loading a larger image size when the user clicks on an image. When you add lazy loading to your images, make sure that you are providing dimensions for the image. This prevents a layout shift if the image doesn't get loaded in time. In this demo, the inline size is determined by the grid definition, and the `aspect-ratio` CSS property on the image gallery grid items is set to `1 / 1`, so the block size matches that. You can also [set dimension attributes](/articles/browser-level-image-lazy-loading#dimension-attributes) directly. AVIF While you want to show your users high resolution images, you likely don't want to show them the full resolution image, especially in the gallery of thumbnails. You'll want to either compress and resize your images before uploading to your server, or configure your server to resize images for you. The AVIF file format is a relatively new format that can be an alternative to legacy formats such as JPEG and PNG, and is worth investigating for potential loading performance benefits. Along with supporting animated images and transparency, AVIF can have lower file sizes than other image formats. The images in this demo were converted to AVIF from JPG, and are much smaller while retaining suitable image quality. AVIF also supports HDR images. Online tools like [Squoosh](https://squoosh.app) and [Cloudinary](https://cloudinary.com/tools/image-to-avif) are useful for one-off conversions to AVIF if you're looking to experiment with the format. Design tools such as Photoshop (version 26+) and GIMP also export to AVIF. You can also incorporate AVIF encoding in your image
processing pipelines using [ImageMagick](https://imagemagick.org/script/formats.php/#supported). Lightbox effect In an image gallery, users have the expectation that they can click an image to view a larger version. In the past, this often required `z-index` hacks, but now you can use the `