|

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

  • WordCamp US, or Why I Love the WordPress Community

    Last week I attended the inaugural WordCamp US, where I spoke as well as attended the Community Summit. The Community Summit “is a smaller, work-focused event. Community leaders gather to discuss and work on issues that the WordPress community faces.” It was a great time, and between that, the actual WordCamp, and Contributor Day, I…

  • |

    Announcing Responsive Design with WordPress

    If you’ve been milling around the site lately, or follow me on Facebook, Twitter, Google+, LinkedIn, or Dribbble, you’ll know I’ve been working on a book. Last week I officially launched the site for @NEPABlogCon and I’m happy today to officially announce my second book, Responsive Design with WordPress. Responsive Design with WordPress shows readers Responsive…

  • | | |

    Web Development Resources, Summer 2013

    I have a ton of bookmark folders to help keep myself organized, though to be honest I usually bookmark something and then forget about it. So, while looking through some of my folders, I can across a good amount of web development resources I’ve been keeping. I’ve decided to share them.

  • |

    My One Month Learning Plan

    I have a confession to make. Lately I’ve felt like my skills have been slipping, or at least stagnating. Part of it is because I’ve been so busy, but part of it is because of feeble excuses like, “I’ve been so busy.” This year I’ve resolved to do a whole bunch of things, including learn more….

  • |

    Some Truths About Web Design

    I’ve been thinking a lot about the changing landscape of web design and development, and I believe there’s already a fast-moving shift in how customers are approaching getting online. I may elaborate more later, but here are the overall thoughts.

  • |

    Do we REALLY Need to Own Our Platform?

    Last week’s Facebook outage brought an onslaught of platitudes from people talking about how terrible Facebook is, and how you should own your own platform. But even as a developer myself, my thoughts are a little different.

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.