|

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

  • Clients Always Mess Things Up

    Imagine you’re buying a new phone. You walk into the store and you overhear a conversation with 2 of the sales clerks. One says to the other, “Ugh. One of my customers put an ugly case on the beautiful iPhone I gave them. Customers are so stupid. They always mess things up.” You probably wouldn’t…

  • |

    Do We Need ANOTHER WordPress Hosting Company?

    WordPress hosting companies are a dime a dozen these days, and if you’re going to be one, you need to differentiate yourself. This could be by offering managed WooCommerce hosting, adding on a theme shop for all customers, or creating a fantastic local development environment that integrates perfectly. When it comes to WordPress host WPX, there are 3 features that differentiate them from competitors.

  • Pivoting WP in One Month

    Do you know the story of Pixar? It’s a really interesting one that I strongly recommend you read all about in Creativity Inc. Here’s the gist: Pixar started off as a piece of hardware inside LucasFilms, and the team was tasked with pushing the technological envelope for live-action movies. When Lucas had to sell off…

  • A Stellar(WP) Sale – 40% Off Everything

    My favorite all-around WordPress company, StellarWP (parent company: LiquidWeb), is celebrating their official launch with a 40% off sale, now through August 4th with the code STELLARSALE. This is a great opportunity to pick up some tools I strongly recommend.

  • |

    CaboPress: The Business Conference I Needed

    When you tell people that you’re going to Cabo San Lucas for a business conference, you get some funny looks. Well, you get one specific look: “Oh sure. I bet you’ll do a ton of work there.” It’s easy to think. I mean, we are in paradise. Our sessions were in pools. We had afternoons…

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.