How to Make WordPress Images Responsive

January 22nd, 2013 - Tutorials | 13 Comments

One of the most exciting trends in web design, including WordPress development is “responsiveness”. You`re probably hearing all over the place about “responsive WordPress themes” and wonder what does it mean. Well, it means that a website reshapes itself to fit and look beautiful on any screen, especially on mobile phones and tablets. The process is not very complicated and it uses CSS media queries to transform a layout and optimize it for tablets and smartphones. Basically, using CSS, we rewrite the rules used when building the website interface for a normal screen.

As an example, if we defined the width of the regular website wrapper to 980px, for a smartphone with a screen dimension of 480x800px, we redefine the width of the wrapper to 480px and so on. But as hard as we try to make a WordPress theme 100% responsive, there are still situations when users have to pay attention at what type of content they use in posts or pages, without breaking the layout.

A special statute have images. Because they vary in dimensions, is difficult to create a rule for all of them. Let`s take an example: You write a blog post and use 2 images: one of 200x200px and the other of 640x480px. Most of responsive rules for images say that if a screen is smaller than 768px, the images will receive a width of 100%. Well, assigning a width of 100% to the big image is ok, but in the small image case it isn`t, because it will get resized from 200x200px to 768x768px and it will look really awful. The solution is to apply responsive rules to images in a selective manner, and this is where user comes in. He must know what images should scale or not.

The easiest way to give some WordPress power to a user is through shortcodes. I`m going to show you how to create a WordPress shortcode, meant to assign the responsive behavior to an image.

Usually, shortcodes and other custom functions are written inside functions.php file and I`m suggesting you do the same. The body of the shortcode is simple and what it does is to wrap in a div all the content found in it.


function responsive_images($atts, $content = null) {
     return '<div class="image-resized">' . $content .'</div>';
}

add_shortcode('responsive', 'responsive_images');

Here`s an example of how to use it:

[responsive]<img src="image_url" alt="alt" title="title" />[/responsive] 

If you inspect the image, you`ll see that now is wrapped by a div class called “image-resized”.

The last step is to assign the CSS rules to images between the shortcode by using @media-queries. Add this to your stylesheet:

@media only screen and (max-width:767px) {
    .image-resized img {
        width:100%;
        height:auto;
    }
}

In most cases, the code above works just fine but if you want to go further and assign different image dimensions for different screens, you can extend the media queries for certain dimensions:

/* 240px screens */
@media only screen and (max-width: 319px) {
    .image-resized img {
        width:240px;
        height:auto;
    }
}

/* 320px screens */
@media only screen and (min-width: 320px) and (max-width: 479px) {
    .image-resized img {
        width:320px;
        height:auto;
    }
}

/* 480px screens */
@media only screen and (min-width: 480px) and (max-width: 767px) {
    .image-resized img {
        width:480px;
        height:auto;
    }
}

Using a shortcode to implement a responsive behavior to images extends the standard CSS rules and offers to users the chance to actually involve in the process of web design. Although media queries exists since CSS version 2, the responsive phenomenon took dimension only recently and responsiveness based on CSS media queries is just a step forward to what it means mobile friendly websites. Nowadays, when most websites are jQuery enhanced with sliders and all kind of interactive stuff, the question raised is how would you adapt these features for mobile screens? But this is another discussion we`ll certainly approach in the future.

Written by

Comments

  1. I suppose this is a solution. Altho the WordPress twentytwelve theme already have an all CSS solution for responsive images inside content. It also does not require any use of shortcode when writing content

    /* FIX IMAGE SIZES */
    .entry img,
    .comment-content img,
    .widget img, .wp-caption {
    max-width: 100%; /* Fluid images for posts, comments, and widgets */
    }
    img[class*="align"],
    img[class*="wp-image-"] {
    height: auto; /* Make sure images with WordPress-added height and width attributes are scaled correctly */
    }
    img.size-full {
    max-width: 100%;
    width: auto; /* Prevent stretching of full-size images with height and width attributes in IE8 */
    }

    • Hi Jonathan,
      Thanks for pointing out the TwentyTwelve solution to responsive images. Indeed, their approach works great for images wrapped in “.hentry” class. The tutorial gives an alternative and explains how you can actually control any image responsiveness behavior by using shortcodes, no matter where they`re used into the website.

  2. Hello, thank you for this tutorial.I have a wordpres site in which I am using easingslider.The site is responsive but the images from the easinslider are no.I am trying to use your tutorial to make the images responsive but when I add the code above to my functions.php file it returns an error that says undefined function add_shortcode().How do I work around this

    • If you receive errors when you add the code in functions.php, try to put the code in another file(let`s say shortcodes.php) and call this file from functions.php with “require_once( “shortcodes.php” );”. I`m not sure if the shortcode will make your slider images responsive because the slider styles might overwrite it, but give it a go and let me know if is working!

  3. Nice post, thanks for sharing this with us! But I was wondering, shouldn’t it be the following function? Where is the class ‘image-resized’ coming from? I haven’t tested it yet though.

    function responsive_images($atts, $content = null) {
    return ” . $content .”;
    }

    • Damn, it doesn’t show my edited function fully. I mean:

      return (div class=image-resized)content(/div)

      • The class “image-resized” is the class which handles the CSS properties for image responsiveness. Without it, the images wrapped in the shortcode are simple images. So, what I tried to do is to wrap the images within a class and style them for mobile screens using that class.

  4. Do we need to use shortcode when we can achieve this using 100% CSS?
    Using shortcode seems impractical to me.
    But I like the concept though.

    • Well, you could use only CSS if you want and if your skills are good enough, this tutorial is learning how to make responsive only images that you want to be responsive.

Trackbacks for this post

  1. How to Make WordPress Images Responsive - Rockable Themes - ちゅどん道中記
  2. IT Digest: Web Design Trends for 2013, Mega and Its 1M of Users at First Day, Opera Ice and Much More | Zfort Group Blog
  3. How to Make WordPress Images Responsive | Design News
  4. How to easily make WordPress images responsive - H22 Solutions Ltd

Leave a Comment