This zone is sleeping right now! ZzzZZzzz

Better not disturb it! ~You go to the petrol station and buy a slushy instead, everything is cool!~

This area will open again in 000.beats!

(Learn about Swatch Time)

Some other zones are awake :^]


Artifacts Gallery Guilds Search Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining?
a Summer day - @463.92
Activity rating: Three Stars Posts & Arts: 36/1k.beats Random | Recent Posts | Guild Recents
News: :skull: Websites are like whispers in the night  :skull: Guild Events: Summer Projects

+  MelonLand Forum
|-+  Life & The Web
| |-+  ✁ ∙ Web Crafting
| | |-+  Image lightboxes with 'show me the alt text' buttons... in pure CSS


« previous next »
Pages: [1] Print Embed
Author Topic: Image lightboxes with 'show me the alt text' buttons... in pure CSS  (Read 728 times)
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!Visited on Melon's 10th Anniversary!
« on: a Winter day » Embed

I had an idea recently and wanted to share it: a way of making:

  • "Lightboxes" (images where when you click them, you see a bigger/higher-def version as a "popover"/"overlay"),
  • with a "show alt text" button like Mastodon has (because alt text is for everybody!),
  • in pure CSS and semantic HTML (no JS = fast performance, less to break!)

Here's what I came up with! The short of it is that you have some HTML code like this:

Code: html
<figure>
  <div class="img-with-alt-text">
    <details class="lightbox">
      <summary>
        <img src="owls-thumbnail.jpg" width="640" height="400" alt="A pair of barred owls rest in the shade of a leafy deciduous tree in the afternoon light.">
      </summary>
      <span class="lightbox-full-image">
        <img src="owls-full.jpg" width="6016" height="4016" loading="lazy" alt="Large image of a pair of barred owls rest in the shade of a leafy deciduous tree in the afternoon light.">
      </span>
    </details>
    <details class="alt-text">
      <summary><span>Alt</span></summary>
      <p>A pair of barred owls rest in the shade of a leafy deciduous tree in the afternoon light.</p>
    </details>
  </div>
  <figcaption>
    Photo courtesy Tom Nebel, used under the Pexels license.
  </figcaption>
</figure>

This works by itself because of the semantic HTML, but it's not very pretty, so we enhance it with this CSS:

Code: css
body:has(.lightbox[open]) {
  overflow: hidden;
}

figure {
  width: fit-content;
}

.img-with-alt-text {
  position: relative;
}

.lightbox {
  display: inline-block;
  width: fit-content;
  margin: 0 auto;

  summary {
    list-style: none;
    cursor: zoom-in;
  }

  &[open] {
    summary {
      cursor: zoom-out;

      &::after {
        content: "";
        position: fixed;
        z-index: 1;
        background-color: rgba(0, 0, 0, 0.6);
        backdrop-filter: blur(2px);
        inset: 0;
      }
    }

    .lightbox-full-image {
      position: fixed;
      inset: 0;
      z-index: 2;
      pointer-events: none;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 1.5ch;

      img {
        object-fit: contain;
        z-index: 3;
        height: 100%;
        width: 100%;
      }
    }
  }
}

.alt-text {
  font-size: 90%;

  summary, p {
    background: rgba(0, 0, 0, 0.65);
    color: var(--color-secondary);
    border-radius: 4px;
    padding: 1px 3px;
    margin: 0;
  }

  summary {
    position: absolute;
    bottom: 10px;
    left: 5px;
    list-style: none;
    cursor: pointer;
    text-transform: uppercase;
  }

  p {
    right: 5px;
  }

  &[open] {
    position: absolute;
    bottom: 10px;
    left: 5px;
    right: 5px;

    summary {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: transparent;

      span {
        display: none;
      }

      p {
        right: 5px;
        margin: 0;
        padding: 0 2px;
      }
    }
  }
}

I don't like the repetition in the HTML, but if you're using a CMS or a Static Site Generator then it's unlikely to be an issue.

Anyway, have a play with what I came up with if it's the kind of thing that interests you. Could be great for a blog or a gallery page.

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif

Artifact Swap: PolyamorousI met Dan Q on Melonland!Doctor RedactedJoined 2025!
Pages: [1] Print Embed 
« previous next »
 

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021 | Privacy Notice | Send Feedback | Supporters ♥ Forum Guide | Rules | RSS | WAP | Mobile


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi
@000 MelonLand Zap! Want to Login or Join ? Forum Art Hub Chat Webring Editor Recent Edits Tenement Arcade Web Guides Graphics Catalogue Wiki Newsletters Image Stream
Melon's Sites TamaNotchi Textures PixelSea GifyPet MoMG Ozwomp Online Loom Videos Leaky Webring Melonking
Tools Melon Software ArtHub Embed Maker ML Passports
Outlinks Webrings Internet Phone Book HTML Energy Declarations Hackers & Designers Frutiger Aero Forum m15o's Web Services 32Bit Cafe iMood
Minecraft: Online
Join: craft.melonking.net