Skip to content
  • Recent
  • Tags
  • Popular
  • Users
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

Lay Theme Forum

  1. Home
  2. General Discussion
  3. Dynamic caption for any media using alt-tags

Dynamic caption for any media using alt-tags

Scheduled Pinned Locked Moved General Discussion
2 Posts 2 Posters 25 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ErEs
    wrote on last edited by ErEs
    #1

    I did some custom code to generate captions on all kind of images dynamically from their alt-tag on a website:

    I also use a counter for galleries. Now i need to implement the same caption also for videos.
    Since videos are treated differently and do not have an alt-tag, i used laythemes own "edit caption" option for the video in my gallery.

    So far so good. I will share the code down below.
    The only thing im struggling with is the position of the video caption.

    What i want to achieve: Video caption on the left. Counter on the right.

    But unfortunately both video caption and counter are controlled by the same class since they are both children of it. This makes it difficult to have different positions. Of course i tried to deal with flexbox and a space-between layout. This does indeed work if i additionally put width: 100% on my

    .lay-carousel-sink-parent
    

    but then this also results in captions being ultra-wide if there is no counter (like for single images).

    Any ideas on how to solve this quickly?

    CSS:

    .lay-carousel-sink-parent{
        position: absolute;
        bottom: 0;
        z-index:99;
        display: inline-block;
        margin-bottom: 0.5vw;
        right:.8rem;    
    }
    
    .lay-carousel-sink.numbers-right.captions-left.sink-with-numbers-and-captions.show-sink {
      display: flex !important;
      justify-content: space-between;
      align-items: flex-end;
    }
    
    
    .numbers{
        border-radius: .3125rem;
      padding-left: .4em;
      padding-right: .4em;
      padding-block-start: 0.35em;
      padding-block-end: 0.35em;
      min-height: 1.4375rem;
      -webkit-backdrop-filter: blur(.875rem);
      backdrop-filter: blur(.875rem);
      background-color: rgba(0, 0, 0, 0.09);
    }
    
    /*Reset*/
    .captions-and-numbers-not-same-position.sink-below .sink-with-numbers-and-captions.lay-carousel-sink .numbers{
        position: unset !important;
    }
    
    

    Javascript (HTML at bottom):

    <script>
    document.addEventListener("DOMContentLoaded", function () {
      console.log("DOM fully loaded and parsed");
    
      const images = document.querySelectorAll("img");
      console.log(`Found ${images.length} image(s) on the page`);
    
      images.forEach((img, index) => {
        console.log(`\nImage ${index + 1}:`);
        console.log("Source:", img.src);
        const altText = img.getAttribute("alt");
        console.log("ALT attribute:", altText);
    
        // Check if alt exists and prevent duplicate captions
        if (altText && !img.nextElementSibling?.classList?.contains('alt-caption')) {
          const caption = document.createElement("div");
          caption.className = "alt-caption _BU_Button";
          caption.textContent = altText;
          img.insertAdjacentElement("afterend", caption);
          console.log("Caption inserted");
        } else {
          if (!altText) {
            console.warn("No ALT text found — skipping");
          } else {
            console.warn("Caption already exists — skipping");
          }
        }
      });
    
      console.log("All done inserting captions");
    });
    </script>
    

    Bildschirmfoto 2025-07-23 um 10.09.13.png

    1 Reply Last reply
    0
    • arminunruhA Offline
      arminunruhA Offline
      arminunruh
      Global Moderator
      wrote on last edited by
      #2

      .lay-carousel-sink-parent

      needs to be 100% width.
      maybe u made it not 100% width by using custom css for .lay-carousel-sink-parent ?

      u could use this css instead of yours:

      .lay-carousel-sink-parent {
          position: absolute;
          bottom: 0;
          z-index: 99;
          display: inline-block;
          margin-bottom: 0.5vw;
          padding-right: .8rem;
          padding-left: .8rem;
          box-sizing: border-box;
          width: 100%;
      }
      

      when a sink of carousel has captions and numbers it has the class:
      sink-with-numbers-and-captions

      if it has only numbers it has the class:
      sink-only-numbers

      Screenshot 2025-07-23 at 11.26.21.png

      i can see the issue you have is because you use a background color for your numbers / captions.

      this css will make a sink with only a number not span the background over the whole carousel:

      .sink-only-numbers.lay-carousel-sink .numbers{
          width: auto!important;
          display: inline-block!!important;
          width: auto!important;
          position: absolute!important;
          right: 0!important;
          bottom: 0!important;
          top: auto!important;
      }
      
      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      I also code custom websites or custom Lay features.
      šŸ’æ Email me here: šŸ’æ
      info@laytheme.com

      Before you post:
      1. When using a WordPress Cache plugin, disable it or clear your cache.
      2. Update Lay Theme and all Lay Theme Addons
      3. Disable all Plugins
      4. Go to Lay Options → Custom CSS & HTML, click "Turn Off All Custom Code", click "Save Changes"

      This often solves issues you might run into

      When you post:
      1. Post a link to where the problem is
      2. Does the problem happen on Chrome, Firefox, Safari or iPhone or Android?
      3. If the problem is difficult to explain, post screenshots / link to a video to explain it
      Online Users
      dmncnD
      dmncn
      Forgot your key, lost your files, need a previous Lay Theme or Addon version? Go to www.laykeymanager.com
      laytheme.com
      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Recent
      • Tags
      • Popular
      • Users
      • Search