Skip to content

Horizontal scrolling gallery in HTML and CSS

A strip of cards that scrolls horizontally with snapping, by touch, wheel or with previous / next buttons.

Here is an excerpt of this component's code in HTML and CSS. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in HTML and CSS

index.html
<!doctype html>
<html lang="fr">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Galerie à défilement horizontal</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <main class="demo">
      <section class="gdh" data-gdh aria-labelledby="gdh-titre">
        <div class="gdh-head">
script.js
document.querySelectorAll("[data-gdh]").forEach((root) => {
  const on = (el, type, fn) => el.addEventListener(type, fn);
  const strip = root.querySelector("[data-gdh-strip]");
  const prev = root.querySelector("[data-gdh-prev]");
  const next = root.querySelector("[data-gdh-next]");

  const update = () => {
    const end = strip.scrollWidth - strip.clientWidth - 2;
    prev.setAttribute("aria-disabled", String(strip.scrollLeft <= 2));
    next.setAttribute("aria-disabled", String(strip.scrollLeft >= end));
  };
  // aria-disabled plutôt que disabled : le bouton garde le focus quand on arrive au bout
style.css
:root {
  --gdh-card: #ffffff;
  --gdh-text: #2a1f24;
  --gdh-muted: #64545b;
  --gdh-line: #e7d9de;
  --gdh-accent: #9d174d;
  --gdh-on-accent: #ffffff;
  --demo-bg: #f4ecee;
  --demo-text: #2a1f24;
}

@media (prefers-color-scheme: dark) {

Free component: copy it freely, including for commercial projects.

Galleries and carousels components in HTML and CSS
  • A product photo that zooms in under the pointer, and from the keyboard with Enter and the arrow keys, to inspect the details.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Building site photos sorted by dated step on a vertical timeline, with the current step highlighted.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • YouTube videos shown as thumbnails: the player only loads on click, to respect privacy and keep the page light.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A slideshow that fills the whole screen, browsed with the keyboard or a finger swipe, and closed with Escape.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component