WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Inline width and height attributes were never a huge problem with images in WordPress, since these were easily overwritten with CSS.

The problem I'm having is that any images with captions are being wrapped in a ID 'attachment_('attachmentnumber') and a class of 'wp-caption' AND they're given inline CSS width and height properties. This is a major pain in the butt, so I'd like to remove the inline styles of this div if at all possible.

share|improve this question

You can override inline styles with "!important" like this:

width: 100px !important;

If you want a PHP fix take a look at this: http://troychaplin.ca/2012/06/updated-function-fix-inline-style-that-added-image-caption-wordpress-3-4/

add_shortcode('wp_caption', 'fixed_img_caption_shortcode');
add_shortcode('caption', 'fixed_img_caption_shortcode');
function fixed_img_caption_shortcode($attr, $content = null) {
    if ( ! isset( $attr['caption'] ) ) {
        if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
        $content = $matches[1];
        $attr['caption'] = trim( $matches[2] );

    $output = apply_filters('img_caption_shortcode', '', $attr, $content);
    if ( $output != '' )
    return $output;

        'id' => '',
        'align' => 'alignnone',
        'width' => '',
        'caption' => ''
    ), $attr));

    if ( 1 > (int) $width || empty($caption) )
    return $content;

    if ( $id ) $id = 'id="' . esc_attr($id) . '" ';

    return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . $width . 'px">' . do_shortcode( $content ) . '<p>' . $caption . '</p></div>';

or javascript/JQuery:

share|improve this answer
!important has always been supported, that part can be edited out. If you could also take the PHP solution and put it in your answer? At the moment if troychaplin.ca goes down this answer becomes useless – Tom J Nowell Mar 4 '13 at 0:38
Ok thx. Should be fixed now. – Kim Mar 4 '13 at 1:51
.wp-caption img,
.wp-caption {
    width:auto !important;
    max-width:100% !important;
    height:auto !important;
share|improve this answer
Please add an explanation to your answer: why could that solve the problem? – toscho Jul 18 '15 at 12:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.