Skip to content

Hover and keyboard image zoom 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.

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>Zoom sur la photo au survol et au clavier</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <main class="demo">
      <section class="gzs" data-gzs aria-labelledby="gzs-titre">
        <div class="gzs-media">
script.js
document.querySelectorAll("[data-gzs]").forEach((root) => {
  const on = (el, type, fn) => el.addEventListener(type, fn);
  const stage = root.querySelector("[data-gzs-stage]");
  const img = root.querySelector("[data-gzs-img]");
  let x = 50;
  let y = 50;
  const pressed = () => stage.getAttribute("aria-pressed") === "true";
  const place = () => {
    img.style.transformOrigin = `${x}% ${y}%`;
  };
  const zoom = (show) => root.toggleAttribute("data-gzs-zoom", show);
  on(stage, "pointermove", (event) => {
style.css
:root {
  --gzs-card: #ffffff;
  --gzs-text: #1c1917;
  --gzs-muted: #57534e;
  --gzs-accent: #0f766e;
  --demo-bg: #efece6;
  --demo-text: #1c1917;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --gzs-card: #1a1917;

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

Galleries and carousels components in HTML and CSS
  • 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
  • Projects sorted by type of work, arranged in tabs that work with the keyboard and screen readers.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component