make WordPress image captions responsive

This webpage contains images inserted by WordPress. The code used to insert the first image is:


    <a href="http://steven.doig.com.au/files/2013/06/Forest_Legacy_m.jpg">
        <img class="size-large wp-image-887" alt="a Forest Legacy group" src="http://steven.doig.com.au/files/2013/06/Forest_Legacy_m-1024x681.jpg" width="1024" height="681" />
    </a> a Forest Legacy group

This image is controlled by CSS:

Read More
#content .wp-caption a img {
    width: 614px;
    height: auto;
}

I want to make this image responsive. I’ve inserted the CSS:

@media (max-width:988px) {
    #content .wp-caption a img {
        width: 99.03225806%; /* 614/620 */
        height: auto;
    }
}

However, the DIV.wp-caption remains at 604px, as specified inside the WordPress post. I’ve tried specifying this as a percentage (97.41935483%) but WordPress reinterpreted it as 104px.

The inline CSS is overriding the CSS I insert into the stylesheet.

<div id="attachment_887" class="wp-caption alignnone" style="width: 614px">

Any ideas on how I can make the .wp-caption responsive?

Related posts

4 comments

  1. You’re going to want to use:

    @media (max-width: 988px){
      .wp-caption {
        /* Force the box to be 100% */
        width: 100% !important;
      }
      #content .wp-caption a img {
        /* Scale down if too big */
        max-width: 99.03225806%; /* 614/620 */
        height: auto;
      }
    }
    
  2. Another possibility is to change the shortcode output so that the width is no longer hard-coded. Modifying the Codex example to have no width:

    add_filter('img_caption_shortcode', 'my_img_caption_shortcode_filter',10,3);
    
    /**
     * Filter to replace the [caption] shortcode text with HTML5 compliant code
     *
     * @return text HTML content describing embedded figure
     **/
    function my_img_caption_shortcode_filter($val, $attr, $content = null)
    {
        extract(shortcode_atts(array(
            'id'    => '',
            'align' => '',
            'width' => '',
            'caption' => ''
        ), $attr));
    
        if ( 1 > (int) $width || empty($caption) )
            return $val;
    
        $capid = '';
        if ( $id ) {
            $id = esc_attr($id);
            $capid = 'id="figcaption_'. $id . '" ';
            $id = 'id="' . $id . '" aria-labelledby="figcaption_' . $id . '" ';
        }
    
        return '<figure ' . $id . 'class="wp-caption ' . esc_attr($align) . '" >'
        . do_shortcode( $content ) . '<figcaption ' . $capid 
        . 'class="wp-caption-text">' . $caption . '</figcaption></figure>';
    }
    

    http://codex.wordpress.org/Function_Reference/add_filter#Example

  3. Here’s a much simpler and cleaner solution:

    function my_img_caption_shortcode_width($width, $atts, $content)
    {
        return 0;
    }
    
    add_filter('img_caption_shortcode_width', 'my_img_caption_shortcode_width', 10, 3);
    
  4. works 😉 thanks

    .wp-caption {
    /* Force the box to be 100% */
    width: 100% !important;
    }
    
    #content .wp-caption a img {
    /* Scale down if too big */
    max-width: 99.03225806%; /* 614/620 */
    height: auto;
    }
    

Comments are closed.