It’s been a source of some irritation up to now that WordPress caption containers have been somewhat unwieldy when it comes to styling. I really wanted them to behave differently depending on what size image they contained, and whilst the images have identifying hooks (like size-large & size-medium etc) the caption containers just displayed wp-caption and wp-caption-text (other than the dynamically generated individual image identifier).
What I had to do in the end was ‘hack the core’ and make a very slight adjustment to the includes/media.php file as follows:
I changed this line (approximately line 745):
return ‘<div ‘ . $id . ‘class=”wp-caption ‘ . esc_attr($align) . ‘” style=”width: ‘ . (10 + (int) $width) . ‘px”>’
return ‘<div ‘ . $id . ‘class=”wp-caption ‘ . ‘imagesize’.$width . ‘ ‘ . esc_attr($align) . ‘” style=”width: auto”>’
This achieved two things. First it removed the automated additional 10px width added to the box (taking the image width and adding 10 pixels). Secondly it added a CSS class after the wp-caption entry. This becomes imagesizexxx. The xxx becomes whatever the image width is. That way you can specify exactly how you want the caption box to behave depending on what width image it contains.