Join the Webstudio community

Updated 3 weeks ago

Webstudio incorrectly rendering lightgallery.js library

At a glance

A community member was using the lightgallery.js library to add a lightbox/gallery to their site, but the CSS was not working correctly, with the top toolbar and prev & next buttons rendered out of place or incorrectly. They tried troubleshooting but couldn't find a solution. However, when they copied the HTML/CSS from a Webstudio page and pasted it into a Replit, the lightgallery.js implementation worked fine. The community members discussed the issue, and one of them suggested that the root cause was the white-space-collapse: preserve setting, which was different from the browser default of collapse.

Useful resources
I've been using the lightgallery.js library to add a lightbox/gallery to my site. Using the HTML block, I embedded the lightgallery.js style sheet and script. The js and gallery initiates and works, however, the css is a bit wonky. Top toolbar and prev & next buttons are rendered out of place or incorrectly.

After troubleshooting with no luck, I copied the html/css of the webstudio page and paste it into a replit, added the lightgallery script and style sheet, and it worked there!

Webstudio builder view link -
https://p-c4b0aacc-ba6e-4cbb-998d-5dde2361f039.apps.webstudio.is/?authToken=71b4fac9-f738-4203-9f92-b37620395464&mode=preview

Replit code -
https://replit.com/@ashish102/GlumTerribleStructure
Marked as solution
the reason was white-space-collapse: preserve we define on the root
View full solution
O
a
H
21 comments
I can see it working on your project on small previews which are not wrapped in a link
so you managed to turn a div into a link (Anchor) using a binding for the tag property
and your library is not designed to work with links or you are using something wrong there, setting there a div back to div makes everything functional
also tip:

add defer attribute to your script

<script defer src="
https://cdn.jsdelivr.net/npm/[email protected]/lightgallery.min.js
"></script>

This will avoid blocking rendering while downloading the script
I've added a loom to clarify the issue. The gallery + lightbox views are working. But within the lightbox view, the "toolbar" header and icons with webstudio have odd css issues. -

Webstudio Loom
https://www.loom.com/share/ee964eaecfaa49339d98fbe8c6b61065?sid=ac7fa0a1-1901-4a9e-92fa-694fad77ea2a

Replit Loom -
https://www.loom.com/share/3fd40cf165db4f4caec75b300d907ccd?sid=0d2dd32a-a38d-4b19-8584-07f489182995

Here is the replit live page (the page renders funny, but click on one of the empty images and the lightbox opens fine)
https://aaa1f12b-ea33-4c86-be98-3fb8a182e75a-00-2un9z11ob8ck7.spock.replit.dev/
Ended up using https://github.com/nextapps-de/spotlight over https://github.com/sachinchoolur/lightgallery.js Minimal changes to classes required and everything works well.
if you still have lightgallary example, try removing display grid from the html element, just as an experiment
The HTML element uses the following display option. I've duplicated the project with the lightgallery.js implementation with build permissions if you want to navigate the canvas itself -https://p-ffa01bca-c354-40ed-9eac-f93278d718f8.apps.webstudio.is/?authToken=344308da-8800-4eaf-adec-c372e80f7653&mode=design
the reason was white-space-collapse: preserve we define on the root
where browser default seems to be collapse
you can set it here if you still need it, in the meantime I will find out why we decided to set the default to preserve
technically the library is also relying on these global things is not very robustly made, but that's another story
Thanks for helping troubleshoot this issue! Would have never guessed that was the root of the problem.
me neither, I just went looking into devtools what is the styles that we define and turned off one after another
I'm interested in Spotlight too, as it has nice features – but wonder how you solved linking to larger image versions from the thumbnail (so that Cloudflare serves separate images). Could you share a minimal sample of your setup?
I use directus as a cms which dynamically populates the gallery with links to images hosted on a seperate CDN. My thumbnails on the page are dynamically optimized through webstudio's cloudflare image service, however the larger image served by spotlight displays the cdn link provided by directus.

I'm using the following setup from spotlights documentation to achieve this -

Non-Anchor Elements
Alternatively you can use non-anchor elements also:

<div class="spotlight" data-src="cdn image link">
<!-- image or any other elements -->
<img> webstudioized image (from cdn image link) </img>
</a>

You should also be able to use this with the optimized image provided by webstudio by appending queries to the end of the image url? (?width=2048&quality=80&format=auto)
Considering Oleg was able to troubleshoot the issue with lightgalleryjs, I would recommend moving over to it considering it's setup pretty much the same way and it's actively maintained.
Thank you very much for this elaborate answer! I don't plan using a CMS – but the first part of your answer may help others nonetheless.

You should also be able to use this with the optimized image provided by webstudio by appending queries to the end of the image url? (?width=2048&quality=80&format=auto)

I'm afraid, I have to further expose my noob status here …
Β I understand how these URL-parameters work – but how do I get them? Grab from Devtools for each image (I have quite a few...)? And will the Cloudflare- URL change when I publish my finished project with its proper domain?

Put differently – would you see a straightforward way to assign the same [image large] treatment to any image with some class assigned, so that one can skip looking up URLs?
Got it - my recommendation is to run a script before the spotlight script is run which grabs all the parent elements with the .spotlight class, grabs the child image href, modifies the query parameters and appends it to the parent element as a data-src attribute...
The following script should do the job. I've also linked my dummy site if you want to look at the build itself.
Plain Text
<script>
  document.addEventListener('DOMContentLoaded', () => {
  const spotlights = document.querySelectorAll('.spotlight');

  spotlights.forEach((spotlight) => {
    // Find the child <img> (adjust if your actual structure differs).
    const img = spotlight.querySelector('img');

    // Safety check: make sure an <img> exists and has a src
    if (img && img.src) {
      // Parse the URL using the built-in URL API
      const url = new URL(img.src);

      // Modify the query parameters as needed
      // For example: set width = 3000, quality = 80, format = 'auto'
      url.searchParams.set('width', '3000');
      url.searchParams.set('quality', '80');
      url.searchParams.set('format', 'auto');

      // Finally, set the parent div's data-src to the updated URL
      spotlight.setAttribute('data-src', url.toString());
    }
  });
});


</script>

Dummy Site - Navigate to the page "Spotlight-test"

https://p-ffa01bca-c354-40ed-9eac-f93278d718f8.apps.webstudio.is/?authToken=344308da-8800-4eaf-adec-c372e80f7653&mode=design
@Holger While I do not believe the clouldflare imagee domain changes, this script makes that worry moot as it grabs the actual img src everytime.
Awesome - thanks a lot for sharing! I'm currently away from the computer but will try this out later.
Add a reply
Sign up and join the conversation on Discord
This will avoid blocking rendering while downloading the script","upvoteCount":0,"dateCreated":"2025-01-15T10:31:16.903Z","datePublished":"2025-01-15T10:31:16.903Z","dateModified":"2025-01-15T10:31:16.903Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#98f4ef77-9f09-49c4-937b-2a91bc4a3b3a","author":{"@type":"Person","url":"https://help.webstudio.is/members/cdd3ed87-53a0-414a-885a-56b7939e412a","name":"Oleg Isonen","identifier":"cdd3ed87-53a0-414a-885a-56b7939e412a","image":"https://cdn.discordapp.com/avatars/469405813048606720/8b66a5882214c63ee6148fcce3ef8e93.webp?size=256"}},{"@type":"Answer","text":"I've added a loom to clarify the issue. The gallery + lightbox views are working. But within the lightbox view, the \"toolbar\" header and icons with webstudio have odd css issues. - Webstudio Loomhttps://www.loom.com/share/ee964eaecfaa49339d98fbe8c6b61065?sid=ac7fa0a1-1901-4a9e-92fa-694fad77ea2aReplit Loom - https://www.loom.com/share/3fd40cf165db4f4caec75b300d907ccd?sid=0d2dd32a-a38d-4b19-8584-07f489182995Here is the replit live page (the page renders funny, but click on one of the empty images and the lightbox opens fine)https://aaa1f12b-ea33-4c86-be98-3fb8a182e75a-00-2un9z11ob8ck7.spock.replit.dev/","upvoteCount":0,"dateCreated":"2025-01-15T16:28:54.434Z","datePublished":"2025-01-15T16:28:54.434Z","dateModified":"2025-01-15T16:43:31.062Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#0479fd8d-1d7c-4d78-918a-20d9183921de","author":{"@type":"Person","url":"https://help.webstudio.is/members/16723a2e-6cbd-494c-8aa7-dc048772d793","name":"ashishdhngr","identifier":"16723a2e-6cbd-494c-8aa7-dc048772d793","image":"https://cdn.discordapp.com/embed/avatars/2.png"}},{"@type":"Answer","text":"Ended up using https://github.com/nextapps-de/spotlight over https://github.com/sachinchoolur/lightgallery.js Minimal changes to classes required and everything works well.","upvoteCount":0,"dateCreated":"2025-01-15T20:23:59.983Z","datePublished":"2025-01-15T20:23:59.983Z","dateModified":"2025-01-15T20:24:01.658Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#6ac02ea2-5014-467a-97de-8bdd31ef4110","author":{"@type":"Person","url":"https://help.webstudio.is/members/16723a2e-6cbd-494c-8aa7-dc048772d793","name":"ashishdhngr","identifier":"16723a2e-6cbd-494c-8aa7-dc048772d793","image":"https://cdn.discordapp.com/embed/avatars/2.png"}},{"@type":"Answer","text":"if you still have lightgallary example, try removing display grid from the html element, just as an experiment","upvoteCount":0,"dateCreated":"2025-01-15T20:29:52.108Z","datePublished":"2025-01-15T20:29:52.108Z","dateModified":"2025-01-15T20:29:52.108Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#e243d10c-364b-4c57-afe8-43dd4f199181","author":{"@type":"Person","url":"https://help.webstudio.is/members/cdd3ed87-53a0-414a-885a-56b7939e412a","name":"Oleg Isonen","identifier":"cdd3ed87-53a0-414a-885a-56b7939e412a","image":"https://cdn.discordapp.com/avatars/469405813048606720/8b66a5882214c63ee6148fcce3ef8e93.webp?size=256"}},{"@type":"Answer","text":"The HTML element uses the following display option. I've duplicated the project with the lightgallery.js implementation with build permissions if you want to navigate the canvas itself -https://p-ffa01bca-c354-40ed-9eac-f93278d718f8.apps.webstudio.is/?authToken=344308da-8800-4eaf-adec-c372e80f7653&mode=design","upvoteCount":0,"dateCreated":"2025-01-16T02:16:28.290Z","datePublished":"2025-01-16T02:16:28.290Z","dateModified":"2025-01-16T02:16:28.766Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#be6af4bb-af31-49f2-b730-58e0ed1f9e2f","author":{"@type":"Person","url":"https://help.webstudio.is/members/16723a2e-6cbd-494c-8aa7-dc048772d793","name":"ashishdhngr","identifier":"16723a2e-6cbd-494c-8aa7-dc048772d793","image":"https://cdn.discordapp.com/embed/avatars/2.png"}},{"@type":"Answer","text":"where browser default seems to be collapse","upvoteCount":0,"dateCreated":"2025-01-16T09:14:20.876Z","datePublished":"2025-01-16T09:14:20.876Z","dateModified":"2025-01-16T09:14:20.876Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#21b840cd-ebcd-43f5-8348-379fba7fa3d3","author":{"@type":"Person","url":"https://help.webstudio.is/members/cdd3ed87-53a0-414a-885a-56b7939e412a","name":"Oleg Isonen","identifier":"cdd3ed87-53a0-414a-885a-56b7939e412a","image":"https://cdn.discordapp.com/avatars/469405813048606720/8b66a5882214c63ee6148fcce3ef8e93.webp?size=256"}},{"@type":"Answer","text":"you can set it here if you still need it, in the meantime I will find out why we decided to set the default to preserve","upvoteCount":0,"dateCreated":"2025-01-16T09:17:15.680Z","datePublished":"2025-01-16T09:17:15.680Z","dateModified":"2025-01-16T09:17:16.069Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#815a4d57-ce79-4cd5-a4d0-2678d376b231","author":{"@type":"Person","url":"https://help.webstudio.is/members/cdd3ed87-53a0-414a-885a-56b7939e412a","name":"Oleg Isonen","identifier":"cdd3ed87-53a0-414a-885a-56b7939e412a","image":"https://cdn.discordapp.com/avatars/469405813048606720/8b66a5882214c63ee6148fcce3ef8e93.webp?size=256"}},{"@type":"Answer","text":"technically the library is also relying on these global things is not very robustly made, but that's another story","upvoteCount":0,"dateCreated":"2025-01-16T09:17:34.682Z","datePublished":"2025-01-16T09:17:34.682Z","dateModified":"2025-01-16T09:17:42.603Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#43f1c1b2-e082-4746-8cee-167f966cec69","author":{"@type":"Person","url":"https://help.webstudio.is/members/cdd3ed87-53a0-414a-885a-56b7939e412a","name":"Oleg Isonen","identifier":"cdd3ed87-53a0-414a-885a-56b7939e412a","image":"https://cdn.discordapp.com/avatars/469405813048606720/8b66a5882214c63ee6148fcce3ef8e93.webp?size=256"}},{"@type":"Answer","text":"Thanks for helping troubleshoot this issue! Would have never guessed that was the root of the problem.","upvoteCount":0,"dateCreated":"2025-01-16T19:40:16.343Z","datePublished":"2025-01-16T19:40:16.343Z","dateModified":"2025-01-16T19:40:16.343Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#f1eab5ea-1a93-4b26-b0c3-13bce0c70ba7","author":{"@type":"Person","url":"https://help.webstudio.is/members/16723a2e-6cbd-494c-8aa7-dc048772d793","name":"ashishdhngr","identifier":"16723a2e-6cbd-494c-8aa7-dc048772d793","image":"https://cdn.discordapp.com/embed/avatars/2.png"}},{"@type":"Answer","text":"me neither, I just went looking into devtools what is the styles that we define and turned off one after another","upvoteCount":0,"dateCreated":"2025-01-16T20:24:00.700Z","datePublished":"2025-01-16T20:24:00.700Z","dateModified":"2025-01-16T20:24:00.700Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#98e22c8b-9437-4ecd-b826-996f791529a8","author":{"@type":"Person","url":"https://help.webstudio.is/members/cdd3ed87-53a0-414a-885a-56b7939e412a","name":"Oleg Isonen","identifier":"cdd3ed87-53a0-414a-885a-56b7939e412a","image":"https://cdn.discordapp.com/avatars/469405813048606720/8b66a5882214c63ee6148fcce3ef8e93.webp?size=256"}},{"@type":"Answer","text":"I'm interested in Spotlight too, as it has nice features – but wonder how you solved linking to larger image versions from the thumbnail (so that Cloudflare serves separate images). Could you share a minimal sample of your setup?","upvoteCount":0,"dateCreated":"2025-01-16T21:53:09.744Z","datePublished":"2025-01-16T21:53:09.744Z","dateModified":"2025-01-16T21:53:09.744Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#34c6e816-b4e5-4cfd-855c-83e6e1586b6c","author":{"@type":"Person","url":"https://help.webstudio.is/members/dfdcdd26-d7c0-453a-8508-b59d90e68303","name":"Holger","identifier":"dfdcdd26-d7c0-453a-8508-b59d90e68303","image":"https://cdn.discordapp.com/avatars/959891796467527693/d84c8c1c4a82099f604fbb5510f32a49.webp?size=256"}},{"@type":"Answer","text":"I use directus as a cms which dynamically populates the gallery with links to images hosted on a seperate CDN. My thumbnails on the page are dynamically optimized through webstudio's cloudflare image service, however the larger image served by spotlight displays the cdn link provided by directus. I'm using the following setup from spotlights documentation to achieve this - Non-Anchor ElementsAlternatively you can use non-anchor elements also:
webstudioized image (from cdn image link) You should also be able to use this with the optimized image provided by webstudio by appending queries to the end of the image url? (?width=2048&quality=80&format=auto)","upvoteCount":0,"dateCreated":"2025-01-16T22:14:00.754Z","datePublished":"2025-01-16T22:14:00.754Z","dateModified":"2025-01-16T22:14:00.754Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#df985257-a0cb-4aa7-8392-2d37f05be3d3","author":{"@type":"Person","url":"https://help.webstudio.is/members/16723a2e-6cbd-494c-8aa7-dc048772d793","name":"ashishdhngr","identifier":"16723a2e-6cbd-494c-8aa7-dc048772d793","image":"https://cdn.discordapp.com/embed/avatars/2.png"}},{"@type":"Answer","text":"Considering Oleg was able to troubleshoot the issue with lightgalleryjs, I would recommend moving over to it considering it's setup pretty much the same way and it's actively maintained.","upvoteCount":0,"dateCreated":"2025-01-16T22:17:32.757Z","datePublished":"2025-01-16T22:17:32.757Z","dateModified":"2025-01-16T22:17:32.757Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#9dfd262a-201f-40a1-b69e-cc5ce16f98f8","author":{"@type":"Person","url":"https://help.webstudio.is/members/16723a2e-6cbd-494c-8aa7-dc048772d793","name":"ashishdhngr","identifier":"16723a2e-6cbd-494c-8aa7-dc048772d793","image":"https://cdn.discordapp.com/embed/avatars/2.png"}},{"@type":"Answer","text":"Thank you very much for this elaborate answer! I don't plan using a CMS – but the first part of your answer may help others nonetheless.You should also be able to use this with the optimized image provided by webstudio by appending queries to the end of the image url? (?width=2048&quality=80&format=auto) I'm afraid, I have to further expose my noob status here … I understand how these URL-parameters work – but how do I get them? Grab from Devtools for each image (I have quite a few...)? And will the Cloudflare- URL change when I publish my finished project with its proper domain?Put differently – would you see a straightforward way to assign the same [image large] treatment to any image with some class assigned, so that one can skip looking up URLs?","upvoteCount":0,"dateCreated":"2025-01-16T22:38:33.960Z","datePublished":"2025-01-16T22:38:33.960Z","dateModified":"2025-01-16T22:38:33.960Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#2fe7f0ab-a267-4fc9-acd7-0c9cbda47839","author":{"@type":"Person","url":"https://help.webstudio.is/members/dfdcdd26-d7c0-453a-8508-b59d90e68303","name":"Holger","identifier":"dfdcdd26-d7c0-453a-8508-b59d90e68303","image":"https://cdn.discordapp.com/avatars/959891796467527693/d84c8c1c4a82099f604fbb5510f32a49.webp?size=256"}},{"@type":"Answer","text":"Got it - my recommendation is to run a script before the spotlight script is run which grabs all the parent elements with the .spotlight class, grabs the child image href, modifies the query parameters and appends it to the parent element as a data-src attribute...The following script should do the job. I've also linked my dummy site if you want to look at the build itself.Dummy Site - Navigate to the page \"Spotlight-test\"https://p-ffa01bca-c354-40ed-9eac-f93278d718f8.apps.webstudio.is/?authToken=344308da-8800-4eaf-adec-c372e80f7653&mode=design","upvoteCount":0,"dateCreated":"2025-01-17T04:12:11.963Z","datePublished":"2025-01-17T04:12:11.963Z","dateModified":"2025-01-17T04:21:06.497Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#b7e2a9e4-c127-43b3-8dbf-c77eea844594","author":{"@type":"Person","url":"https://help.webstudio.is/members/16723a2e-6cbd-494c-8aa7-dc048772d793","name":"ashishdhngr","identifier":"16723a2e-6cbd-494c-8aa7-dc048772d793","image":"https://cdn.discordapp.com/embed/avatars/2.png"}},{"@type":"Answer","text":"@Holger While I do not believe the clouldflare imagee domain changes, this script makes that worry moot as it grabs the actual img src everytime.","upvoteCount":0,"dateCreated":"2025-01-17T04:22:50.365Z","datePublished":"2025-01-17T04:22:50.365Z","dateModified":"2025-01-17T04:23:49.857Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#62f6d82c-84ed-4478-91c3-3b7144b92ba7","author":{"@type":"Person","url":"https://help.webstudio.is/members/16723a2e-6cbd-494c-8aa7-dc048772d793","name":"ashishdhngr","identifier":"16723a2e-6cbd-494c-8aa7-dc048772d793","image":"https://cdn.discordapp.com/embed/avatars/2.png"}},{"@type":"Answer","text":"Awesome - thanks a lot for sharing! I'm currently away from the computer but will try this out later.","upvoteCount":0,"dateCreated":"2025-01-17T09:00:45.659Z","datePublished":"2025-01-17T09:00:45.659Z","dateModified":"2025-01-17T09:01:59.679Z","url":"https://help.webstudio.is/webstudio-incorrectly-rendering-lightgalleryjs-library-rJyJ0bVhvBby#bfa327df-a41c-42ed-be0b-fe8d4cef5f4b","author":{"@type":"Person","url":"https://help.webstudio.is/members/dfdcdd26-d7c0-453a-8508-b59d90e68303","name":"Holger","identifier":"dfdcdd26-d7c0-453a-8508-b59d90e68303","image":"https://cdn.discordapp.com/avatars/959891796467527693/d84c8c1c4a82099f604fbb5510f32a49.webp?size=256"}}]}}