question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set caption width to be the exact same width as each rendered image?

See original GitHub issue

Hi,

I’d like to be able to align the captions that display under each image to the bottom left edge of each image when you are in fullscreen photoSwipe mode. The gallery i’m working on has varying heights and widths of images. So when the caption is centered in the viewport the caption alignment can sometimes look wrong, especially for landscape images .

I thought of a workaround, which would be to update the width of the caption block each time an image changes or an arrow is clicked within photoSwipe to match the width of the image currently being shown. If this was set to the same width of the image itself the text inside the caption could be left aligned as usual and the caption block would sit flush to the left of each image.

I’m having problems getting this to work though.

I’m trying to get the width of the current image being shown by using this code:

$('.pswp__img').innerWidth();  

I’m putting this within either the listen(‘imageLoadComplete’) function or the AfterChanged function. both seem to work for some images, but not all.

I’m targeting this element to set the width inline using jQuery

 $('.pswp__caption__center').css("max-width", nextImgWidth);

Does anyone know where i’m going wrong here?

photoswipeInstance.listen('imageLoadComplete', function(index, item) {
              var currentImageWidth = $('.pswp__img').innerWidth();   // the current width of the image in the lightbox
              $('.pswp__caption__center').css("max-width", currentImageWidth);  // set the caption block to the same width as the current image being shown
           });

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dimsemenovcommented, Oct 9, 2015

Try option addCaptionHTMLFn:

addCaptionHTMLFn: function(item, captionEl /*, isFake */) {
    if(!item.title) {
        captionEl.children[0].innerHTML = '';
        return false;
    }
    captionEl.children[0].innerHTML = item.title;
    captionEl.children[0].style.width = (item.w * item.fitRatio) + 'px';
    return true;
}

And also don’t forget to override default max-width:

.pswp__caption__center {
    max-width: 100%;
}

http://photoswipe.com/documentation/options.html

You’ll also need to set size on PhotoSwipe resize event (if you want it to change during page resize).

0reactions
Madeon08commented, Apr 5, 2020

Hi guys, thanks a lot for the tips!

I personally just added a SetTimeout function because it was working randomly without it.

window.onresize = function(event) {
    setTimeout(function(){
        var imgwidth = $(".pswp__img:not(.pswp__img--placeholder)").width();
        $(".pswp__caption__center").css('width',imgwidth,'important');
    },25);
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Image caption width to same as image - html - Stack Overflow
So the problem is that you don't know how wide the img will be, and the caption for the img may exceed the...
Read more >
How to set width of caption to width of figure? - TeX
You need to store the image in a savebox and then wrap the caption into a minipage with the width of the box....
Read more >
CSS: figures & captions - W3C
If you know the width of all images in the document, you can add a minimum width to figure, like this: figure {...
Read more >
figure / img / figcaption width - Getting Started - ProcessWire
When I insert a img with figcaption in the body text (through the images field), the caption doesn't fit the image width. Meaning:...
Read more >
Make wp-caption the same (responsive) width as the Image
Take a look at where we first set $caption . This is where we set the max-width of the div to match the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found