Skip to content

Instantly share code, notes, and snippets.

@gabelloyd
Created April 13, 2017 15:48
Show Gist options
  • Save gabelloyd/56df05ba0c2da2940a113ec9670fc46f to your computer and use it in GitHub Desktop.
Save gabelloyd/56df05ba0c2da2940a113ec9670fc46f to your computer and use it in GitHub Desktop.
Make WP Captions Responsive to image widths.
function caption_responsive(){
//// Make captions responsive
// we want to change the width of the figcaptions within figures
$('.wp-caption').each(function(){
var caption = $(this),
figcaption = caption.find('figcaption').first(),
img = caption.find('img[class*="wp-image-"]').first();
if (img.length) {
// wait for the image to load or it might have no dimensions
img.load(function() {
// Clear the default WP Core styling
caption.removeAttr('style');
figcaption.css("width", img.width())
});
}
});
}
jQuery(document).ready(function($) {
caption_responsive();
$(window).resize(function() {
caption_responsive();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment