|

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

  • |

    Sass, Sublime Text, Github, and Changing my Development Workflow

    When I left the full time freelancing world 2.5ish years ago, I had a very specific way I did things and I enjoyed it; however, admittedly, it wasn’t the best. I was using Coda, not making local copies, not using any form of version control, and my frameworks were becoming stale. I’ve been going to…

  • |

    Add Attachments to WordPress Search Results

    I feel like this has to have been done a lot, and there are great plugins out there for it, but if you’re just looking to add a quick function to your theme (or a really simple plugin) yourself, here’s how to modify WordPress’ search query to include attachments (like images). function attachment_search( $query ) {…

  • | |

    Freelancers: Generalize Your Code

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!One thing that was really driven home during my first year of grad school was the importance of reusable code. Not…

  • New Facebook Fun

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website! For all the people out there with Facebook, an online social site for college and now high school students all…

  • 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…

  • |

    Quick Tip: How to Un-shrink a Sweater

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!So this isn’t really tech related, but I’m a guy and not a very bright one at that. Last week a…

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.