|

Make wp-caption the same (responsive) width as the Image

I was working on a problem last week in WordPress where the caption for an image was extending the whole width of the container, not staying the width of the image. This make sense – the image is inside the .wp-caption container. So how to we fix it? Let me tell you!

The first thing I tried was some nice, simple CSS:

.wp-caption {
   display: table;
}

.wp-caption-text {
   caption-side: bottom;
   display: table-caption;
}

WordPress places a paragraph around the actual caption text called .wp-caption-text, so what this CSS is saying is, “Treat .wp-caption like a table, and .wp-caption-text like a caption for the table. Place it on the bottom of the table.”

This actually works out great. The div is the width of its content (in this case, an image) but it’s not responsive. It doesn’t pay attention to max-width either. So back to the drawing board.

What I did, thanks to a push in the right direction from this post, is less elegant but actually works responsively. It involves using PHP and filtering the caption output to add a max-width style inline that is the same as the image’s width. Here’s the code below. It’s actually pretty similar to the code above, with one important difference:

function responsive_wp_caption($x = NULL, $attr, $content) {
    extract( shortcode_atts( 
        array(
         'id' => '',
         'align' => 'alignnone',
         'width' => '',
         'caption' => '',
        ), 
        $attr 
      )
    );
 
    if ( intval( $width ) < 1 || empty( $caption ) ) {
        return $content;
    }
 
    $id = $id ? ('id="' . $id . '" ') : '';
 
    $caption = '
';     $caption .= do_shortcode( $content );     $caption .= '

' . $caption . '

';     $caption .= '
';     $caption .= '
';       return $ret; }   add_filter( 'img_caption_shortcode', 'responsive_wp_caption', 10, 3 );

Take a look at where we first set $caption. This is where we set the max-width of the div to match the width of the image. We’re also adding in a width of 100% to make sure the div doesn’t get too small or too big depending on it’s child image.

Similar Posts

  • | |

    2015: Resolving to Do More, and Less

    Every year I publish my list of resolutions as well as a scorecard from the previous year. For 2015, I’ve redesign the site (well, I used HTML5 Boilerplate) and broke my resolutions down into 2 categories: “Do More” and “Do Less.”

  • | |

    Creating a Responsive Gutenberg Price Table

    Last week I worked on an upcoming tutorial for a popular online publication on how to style the Gutenberg Columns block (I’ll be sure to send that along when it comes out). As as result, I decided to experiment to see what you could reasonable do, and came up with this Gutenberg Price Table: https://codepen.io/jcasabona/pen/RYvEYd. In…

  • |

    Adding the Media Uploader in WordPress

    …without including the Editor. In the Admin. That was a really long title, so I hope you don’t feel mislead! I was recently working on a project that required a Custom Post Type without the editor, but needed the Media Uploader. Note: This is not a full-blown tutorial. The purpose of this post is to…

  • Fixing the Fatal ‘add_rewrite_tag()’ Error

    I’m working on a WordPress little side project where I am registering a new Custom Post Type and Taxonomy and came across this: Fatal error: Call to a member function add_rewrite_tag() on a non-object… After doing a Google Search to try to figure out what the issue is, all I came up with was “disable…

One Comment

  1. Hi Joe, thanks so much for this snippet, it’s the clearest example I could find to solve this issue.
    Just needed to fix a typo to make this work.. the $caption variables (except the original $caption being concatenated) should be named $ret. Cheers!

Comments are closed.