Wordpress native galleries are not responsive as we need. This is Jquery solution to quickly overide the wp galleries markup from the frontend. No php involved, which makes it very simple and fast for a front end developer to achieve.
To fix this issue I have replaced the markup of the native wordpress gallery
for a ul with li items that inside contain 2 span elements:
one with the image, and the other with the caption.
This code sets you free from Wordpress native gallery CSS , by renaming classes and replacing elements. You will have to add custom CSS to specify how many elements you would like to
display per row, also make custom breaking points with media queries will be necessary.
Hope this code helps people to overcome the issue of the lack of a Wordpress responsive gallery.
// replace dl with li
jQuery(jQuery('.gallery').find('dl').get().reverse()).each(function(){
jQuery(this).replaceWith(jQuery('<li>'+jQuery(this).html()+'</li>'))
})
// replace dd with spans
jQuery(jQuery('.gallery').find('dd').get().reverse()).each(function(){
jQuery(this).replaceWith(jQuery('<span class="image_container">'+jQuery(this).html()+'</span>'))
})
// replace dt with spans
jQuery(jQuery('.gallery').find('dt').get().reverse()).each(function(){
jQuery(this).replaceWith(jQuery('<span class="caption_container">'+jQuery(this).html()+'</span>'))
})
// replace wordpress gallery container div with ul
jQuery(jQuery('.services_image_block').find('div.gallery').get().reverse()).each(function(){
jQuery(this).replaceWith(jQuery('<ul class="wp_gallery_responsive">'+jQuery(this).html()+'</ul>'))
})
// remove break of line
jQuery(function() {
jQuery( ".wp_gallery_responsive br" ).remove();
});
This code can be added in the page template of single page to avoid changing all the galleries across the CMS.
Another option is to add in the footer , then it will override all the wp gallery markup.
Hope this helps!