---
title: "Using PhotoSwipe in Astro to Build an Image Gallery"
description: "How to Build an image gallery in an Astro application using PhotoSwipe?"
source: "https://www.launchfa.st/blog/photoswipe-astro"
author: "Rishi Raj Jain"
created_at: 2024-04-24T00:00:00.000Z
updated_at: 2026-07-02T00:00:00.000Z
---

> **Site index for agents**
> Fetch https://www.launchfa.st/llms.txt to discover every page on this site.
> Every page is also available as Markdown by appending `.md` to its URL.

<img width="1600" height="900" alt="Using PhotoSwipe in Astro to Build an Image Gallery" decoding="async" loading="eager" class="mt-4 border rounded-sm bg-cover bg-center bg-no-repeat transform will-change-auto" src="https://ik.imagekit.io/vjeqenuhn/launchfast-website/photoswipe-astro" />

In this guide, you will learn how to use PhotoSwipe in an Astro application. You will go through the process of setting up a new Astro project, installing the PhotoSwipe module, and turning your image collection into an image gallery.

## Prerequisites

You'll need the following:

- [Node.js 20](https://nodejs.org/en/blog/announcements/v20-release-announce) or later

## Table Of Contents

- [Create a new Astro application](#create-a-new-astro-application)
- [Integrate PhotoSwipe in your Astro project](#integrate-photoswipe-in-your-astro-project)
  - [Install PhotoSwipe SDK](#install-photoswipe-sdk)
  - [Create an Image Collection](#create-an-image-collection)
  - [Turn your Image Collection into an Image Gallery](#turn-your-image-collection-into-an-image-gallery)
- [Build and Test your Astro application locally](#build-and-test-your-astro-application-locally)
- [Source Code](#source-code)
- [Conclusion](#conclusion)

## Create a new Astro application

Let’s get started by creating a new Astro project. Execute the following command:

```bash
npm create astro@latest my-app
```

`npm create astro` is the recommended way to scaffold an Astro project quickly.

When prompted, choose:

- `Empty` when prompted on how to start the new project.
- `Yes` when prompted if you plan to write TypeScript.
- `Strict` when prompted how strict TypeScript should be.
- `Yes` when prompted to install dependencies.
- `Yes` when prompted to initialize a git repository.

Once that’s done, you can move into the project directory and start the app:

```bash
cd my-app
npm run dev
```

The app should be running on [localhost:4321](http://localhost:4321/).

Now, let's move on to integrate PhotoSwipe in your Astro application.

## Integrate PhotoSwipe in your Astro project

### Install PhotoSwipe SDK

Execute the command below to install the necessary package for using PhotoSwipe:

```bash
npm install photoswipe
npm install -D @types/photoswipe
```

The command installs the following library:

- `photoswipe`: A framework-agnostic JavaScript Image Gallery library.
- `@types/photoswipe`: Type definitions for `photoswipe` library.

### Create an Image Collection

Using PhotoSwipe (with the default configuration) requires two things:

- A parent HTML element (most commonly, a `<div>`) which contains all the images.
- Each image to be enclosed in an `<a>` tag containing the `height` and `width` attributes of the image.

<aside class="border-l-4 pl-4 border-[#3d50f5] bg-[#cfd4fc] py-2 rounded-sm" aria-label="Note"> <div class="flex flex-row items-center gap-x-1" aria-hidden="true"> <svg aria-hidden="true" width="20" height="20" viewBox="0 0 24 24" fill="#0f1c8a"><path d="M12 11a1 1 0 0 0-1 1v4a1 1 0 0 0 2 0v-4a1 1 0 0 0-1-1Zm.38-3.92a1 1 0 0 0-.76 0 1 1 0 0 0-.33.21 1.15 1.15 0 0 0-.21.33 1 1 0 0 0 .21 1.09c.097.088.209.16.33.21A1 1 0 0 0 13 8a1.05 1.05 0 0 0-.29-.71 1 1 0 0 0-.33-.21ZM12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20Zm0 18a8 8 0 1 1 0-16.001A8 8 0 0 1 12 20Z"></path></svg> <span class="text-[#0f1c8a]">Note</span> </div> <section> You can use packages like <a href="https://www.npmjs.com/package/image-size" target="_blank">image-size</a> to compute dimensions of remote images easily. </section> </aside>

To make sure that the `<a>` element does not take over the styling of your `<img>` elements, you can use the following CSS to only render its children elements: `display: contents;`. In the example below, you are going to use the `contents` class by Tailwind CSS.

```astro {3,13,16-37}
---
// File: src/pages/index.astro // [!code focus]
import { ViewTransitions } from "astro:transitions";
---
// [!code focus]
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
    <meta name="viewport" content="width=device-width" />
    <meta name="generator" content={Astro.generator} />
    <title>Astro</title>
    <ViewTransitions />
  </head>
  <body>
    <div id="my-gallery" class="grid grid-cols-1 py-10 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4">
      <a href="https://picsum.photos/900/1600" data-pswp-width="900" data-pswp-height="1600" class="contents">
        <img
          width="900"
          height="1600"
          loading="lazy"
          decoding="async"
          src="https://picsum.photos/900/1600"
          class="absolute h-[250px] w-[300px] object-cover shadow-sm"
        />
      </a>
      <a href="https://picsum.photos/900/1600" data-pswp-width="900" data-pswp-height="1600" class="contents">
        <img
          width="900"
          height="1600"
          loading="lazy"
          decoding="async"
          src="https://picsum.photos/900/1600"
          class="absolute h-[250px] w-[300px] object-cover shadow-sm"
        />
      </a>
    </div>
  </body>
</html>
```

### Turn your Image Collection into an Image Gallery

In minimal lines of code with PhotoSwipe, you can turn your collection of images into an interactive image gallery. You can easily do that in 3 steps:

- Import the `PhotoSwipe` css.
- Import the `PhotoSwipe` module.
- Create a lightbox using `PhotoSwipe`.

Refer to the script below to do the above steps in this example:

```astro {38-54}
---
// File: src/pages/index.astro // [!code focus]
import { ViewTransitions } from "astro:transitions";
---
// [!code focus]
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
    <meta name="viewport" content="width=device-width" />
    <meta name="generator" content={Astro.generator} />
    <title>Astro</title>
    <ViewTransitions />
  </head>
  <body>
    <div id="my-gallery" class="grid grid-cols-1 py-10 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4">
      <a href="https://picsum.photos/900/1600" data-pswp-width="900" data-pswp-height="1600" class="contents">
        <img
          width="900"
          height="1600"
          loading="lazy"
          decoding="async"
          src="https://picsum.photos/900/1600"
          class="absolute h-[250px] w-[300px] object-cover shadow-sm"
        />
      </a>
      <a href="https://picsum.photos/900/1600" data-pswp-width="900" data-pswp-height="1600" class="contents">
        <img
          width="900"
          height="1600"
          loading="lazy"
          decoding="async"
          src="https://picsum.photos/900/1600"
          class="absolute h-[250px] w-[300px] object-cover shadow-sm"
        />
      </a>
    </div>
    <script>
      import 'photoswipe/style.css'
      import pswpModule from 'photoswipe'
      import PhotoSwipeLightbox from 'photoswipe/lightbox'

      const lightbox = new PhotoSwipeLightbox({
        pswpModule,
        children: 'a',
        gallery: '#my-gallery',
      })

      document.addEventListener(
        'astro:page-load',
        () => { if (lightbox) lightbox.init() },
        { once: false },
      )
    </script>
  </body>
</html>
```

In the code above you have done the following:

- Imported the `PhotoSwipe` CSS.
- Created a lightbox that listens to clicks on all the `<a>` elements, present hierarchically in the HTML element that has `my-gallery` as its id attribute.
- Attaches an Astro page load listener and brings the lightbox in effect. **This way of instantiating the lightbox is helpful as this script can be used with View Transitions enabled in your Astro application as well.**

## Build and Test your Astro application locally

To test the application, prepare a build and run the preview server using the command below:

```bash
npm run build && npm run preview
```

## Source Code

To get everything pre-built as in this guide, please visit https://github.com/rishi-raj-jain/astro-photoswipe-starter.

## Conclusion

In this guide, you learned how to use PhotoSwipe in an Astro application to build an Image Gallery.

If you have any questions or comments, feel free to reach out to me on [Twitter](https://twitter.com/direct_messages/create/rishi_raj_jain_).
