How to Make WordPress Images Responsive

January 22nd, 2013 - Tutorials | 74 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.

  5. If you would like to take much from this piece of writing then you have to apply these methods
    to your won webpage.

  6. This site was… how do I say it? Relevant!! Finally I’ve found something which helped
    me. Kudos!

  7. Thank you for any other wonderful article. The place else may anybody get that kind of info
    in such an ideal method of writing? I’ve a presentation next
    week, and I’m on the look for such information.

  8. Write more, thats all I have to say. Literally, it seems as though
    you relied on the video to make your point. You obviously know what youre talking about, why throw
    away your intelligence on just posting videos to your weblog
    when you could be giving us something enlightening to read?

  9. I’ve been exploring for a bit for any high-quality articles or blog posts on this kind
    of space . Exploring in Yahoo I eventually stumbled upon
    this website. Reading this info So i am satisfied to express that I’ve a very good uncanny feeling I came upon exactly what I needed.

    I such a lot surely will make sure to do not overlook this site and provides it a look regularly.

  10. Magnificent beat ! I would like to apprentice at the same time as you amend
    your website, how can i subscribe for a weblog site?
    The account helped me a appropriate deal. I had been tiny bit familiar of this your broadcast offered
    shiny transparent idea

  11. I’m gone to say to my little brother, that he should also pay a quick visit this weblog
    on regular basis to get updated from newest reports.

  12. Hi there would you mind sharing which blog
    platform you’re using? I’m going to start my own
    blog soon but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and I’m looking for something unique.
    P.S Apologies for being off-topic but I had to ask!

  13. I was curious if you ever considered changing the structure of
    your website? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.

    Youve got an awful lot of text for only having 1 or two images.

    Maybe you could space it out better?

  14. Hmm is anyone else having problems with the images on this blog loading?
    I’m trying to determine if its a problem on my end or
    if it’s the blog. Any feed-back would be greatly appreciated.

  15. Interestingly, most individuals right now are not even mindful of small carb meals which can give them healthy and balanced way of living if they abide by a ordinary routine of consumption
    on a weekly basis. Ways to attain the best advantages from your
    healthy and balanced smoothie recipes is always to look at
    the proper mix of fruit and veggies. cup frozen strawberries, and
    one scoop vanilla protein powder in a blender and blend until smooth
    and frosty.

  16. I’m trying to determine if its a problem on my end or
    if it’s the blog. Any feed-back would be greatly appreciated.

  17. The widely used the page using the links, the harder inevitable a thief will edit it,
    removing the links and any probability of gaining.
    Larger organizations are apt to have stricter, expressly written rules on etiquette.
    To my thoughts the thought of woman president is something speculative, going after
    dark generally accepted rules possibly at the very best subject to the
    theoretical and philosophical comprehension.

  18. This is the forthcoming Fallout four Beta Keys.

    With this Fallout 4 Generator you will be in a position to generate
    beta keys to use on your Xbox 360|Ps3 |or even Computer.

    Giving you possibility to enter the planet of
    the new redesigned Fallout four : Forgotten Order in a beta
    section. We are a massive gaming community with lots of keys on each platform.
    Our keys are coming from numerous developers these kinds of as Obsidian
    Enjoyment who themselves need to have beta testers for this sport just before launch
    day. Each and every beta keys coming from our Fallout four
    Beta Keys Generator will be created from our databases and uncover the unused valid keys and offer
    them to you in a solitary click on. Every keys. As we can continuously get beta keys and feed our database our
    purposes are constantly up-to day.

  19. My spouse and I stumbled over here from a different web page and thought I might check things out.
    I like what I see so now i’m following you.
    Look forward to looking over your web page for a second
    time.

  20. Aren’t you tired of playing the game countless hours in order to generate the items in the game.
    What outcome you get by playing it like 12 hours a day. Small amount of
    items get increase and you’re happy with that.
    What If I tell you that you can generate unlimited gems, resources and other things in the game.
    YES!! Unlimited Items and you will not have to play the countless hours in order
    to beat your friends or family members. If you are looking to the way
    to get a ton of Gems, Elixir & Gold without spending your money, then this Clash of Clans Hack website is for you.

  21. Aren’t you tired of playing the game countless hours in order
    to generate the items in the game. What outcome you get by playing it like 12 hours a day.
    Small amount of items get increase and you’re happy with that.
    What If I tell you that you can generate unlimited
    gems, resources and other things in the game. YES!!
    Unlimited Items and you will not have to play the countless hours
    in order to beat your friends or family members. If you are looking to the way
    to get a ton of Gems, Elixir & Gold without spending your money, then
    this Clash of Clans Hack website is for you.

  22. This design is spectacular! You obviously know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start my own blog (well,
    almost…HaHa!) Fantastic job. I really enjoyed what you had to say, and more than that, how you presented it.
    Too cool!

    Also visit my weblog: TuneUp Utilities 2014 Code activateur

  23. Fine way of describing, and pleasant article to get information
    concerning my presentation subject, wgich i am going to deliver iin school.

  24. I’m impressed, I must say. Seldom do I encounter a blog that’s both educative and engaging, and without a doubt,
    you have hit the nail on the head. The problem is
    an issue that too few folks are speaking
    intelligently about. I’m very happy I came across this during my hunt for something regarding this.

  25. It’s hard to find well-informed people on this topic, however, you sound like you know what
    you’re talking about! Thanks

  26. I love what you guys are up too. Such clever work and coverage!
    Keep up the fantastic works guys I’ve added you guys to my blogroll.

  27. Hi there, You’ve done a fantastic job. I’ll certainly digg it
    and personally recommend to my friends. I’m sure they
    will be benefited from this website.

  28. Thaank you, I’ve jyst beenn searching for information about this topic for a
    while and yours iss the greatest I’ve found out till now. However, what in regards to the conclusion?
    Aree you sure about the source?

  29. This is really interesting, You’re a very skilled blogger.
    I have joined your feed and look forward to seeking more of
    your great post. Also, I have shared your site
    in my social networks!

  30. So, we would take it that this coffee brewer produces good brew.
    3 cubic feet of interior cooking space and
    features convection heating on the top and bottom.
    This is an exciting take on salsa that uses apples instead of
    tomatoes and flavors it with the root of the Mexican jicama vine.

    Also visit my web page; site

  31. Hello, I enjoy reading all of your article. I wanted to write a little comment to support
    you.

  32. I don’t know if it’s just me or if everybody else encountering problems with your website.
    It appears as though some of the written text in your content
    are running off the screen. Can someone else please
    provide feedback and let me know if this is happening to them too?
    This might be a issue with my browser because I’ve had this happen
    previously. Appreciate it

  33. I like the valuable info you provide in your articles.
    I’ll bookmark your blog and check again here regularly.
    I am quite certain I will learn plenty of
    new stuff right here! Best of luck for the next!

  34. I will immediately clutch your rss feed as I can not in finding your email subscription link oor e-newsletter service.
    Do you’ve any? Please lett me know so that I maay just subscribe.
    Thanks.

  35. Hello to every single one, it’s really a good for me to visit this
    web page, it consists of useful Information.

  36. I’m not certain where you’re getting your info,
    but great topic. I must spend some time studying much more or working out more.
    Thank you for magnificent information I was looking for this info for my mission.

    Also visit my blog post … homefront telecharger le film

  37. It’s appropriate time to make some plans for the longer
    term annd it is time to be happy. I’ve learn this submit and if
    I may I wih to suggest you few attention-grabbing issues or tips.
    Perhaps you could write next articles refrring to this article.
    I want to read even more issues approximately it!

  38. naturally like your web site but you have to take a look at the spelling on several of your
    posts. Several of them are rife with spelling issues and I in finding it very troublesome
    to tell the truth on the other hand I will definitely come back
    again.

  39. Appreciate the recommendation. Will try it out.

    Visit my web-site Khumba Télécharger

  40. Hey there, You have done a great job. I’ll definitely digg it and personally recommend to my friends.

    I’m confident they’ll be benefited from
    this site.

  41. There are few better feelings in thee world.

    We got the box. Just on Monday Piee topped with ice cream for dessert.
    So even if the scalrs are not altering, it doesn’t mean that
    a person needs to consume fewer calories. You have to legalize all foods and eliminate feelings of deprivation by dealing with the little
    ones. However, you do have to be torture. Making an attempt to discriminate against healthful eaters by declaring them to 980d slimming legging
    bee perfect.

    Haave a lokok at my web-site :: how to lose Belly fat fast (webmailalt.selfnet.at)

  42. A person necessarily help to make severely articles I might state.
    This is the first time I frequented your website page and up to now?
    I amazed with the research you made to make this actual put up extraordinary.

    Excellent process!

  43. DKNY: In DKNY stores and online, take 30% off sale items.

    Ruche: “New Arrivals” category sale, 30% off everything under that category.
    Streamline your holiday shopping by checking
    out the deals at these sites to get your holiday gift list
    focused and organized.

  44. When I originally commented I seem to have clicked the -Notify me when new comments are added-
    checkbox and from now on whenever a comment is added
    I recieve four eemails with thee exact same comment.
    Perhaps there iss an easy method you aare able to remove me from that
    service? Appreciate it!

    Check out mmy web-site :: como agrandar ell pene naturalmente y gratis (Edmund)

  45. Hi it’s me, I am also visiting this web site daily, this web site is really good and the people are genuinely sharing pleasant thoughts.

  46. This is very interesting, You’re a very skilled blogger. I have joined
    your feed and look forward to seeking more of your excellent post.
    Also, I’ve shared your site in my social networks!

  47. I was curious if you ever thought of changing the layout of your website?
    Its very well written; I love what youve got to
    say. But maybe you could a little more in the way of content so people could connect with it better.

    Youve got an awful lot of text for only having one or
    2 images. Maybe you could space it out better?

  48. Hi! I know this is kinda off topic but I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa?
    My blog goes over a lot of the same subjects as yours and I believe we could
    greatly benefit from each other. If you’re interested feel free to shoot me an e-mail.
    I look forward to hearing from you! Superb blog by the way!

    my site :: work from home

  49. Fascinating blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple tweeks would really make my blog stand out.
    Please let me know where you got your theme. Kudos

    Stop by my page – brave frontier Hack [http://youtube.com/watch?v=MN_psN0GQVI]

  50. each time i used to read smaller content which as well clear their motive, and that is also happening with this post which
    I am reading here.

    My webpage … World Of Warplanes Hack

  51. Write more, thats all I have to say. Literally, it seems as
    though you relied on the video to make your point. You definitely know
    what youre talking about, why throw away your intelligence on just posting videos
    to your blog when you could be giving us
    something enlightening to read?

  52. President Obama and AG Holder have admitted as
    much in their comments after the Decision was handed down.
    Ladle in the soup, then top with the remaining crabmeat.
    Whatever the case may be, the key to a healthy weight and lifestyle is eating nutritious meals
    and exercise.

  53. What i do not realize is if truth be told how you’re no longer actually much more neatly-liked than you
    may be right now. You’re so intelligent. You recognize therefore significantly on the subject of this subject,
    produced me in my view believe it from so many numerous angles.
    Its like women and men don’t seem to be involved unless it is something to do with Lady gaga!
    Your individual stuffs outstanding. All the time maintain
    it up!

  54. I truly love your blog.. Very nice colors & theme.

    Did you make this website yourself? Please reply back as I’m trying to create
    my very own website and would love to learn where you got this from or what the theme is called.

    Thank you!

  55. magnificent issues altogether, you simply received a brand new reader.
    What might you suggest in regards to your post that
    you simply made a few days in the past? Any positive?

    chaussure de luxe femme louboutin

  56. Woah! I’m really loving the template/theme
    of this blog. It’s simple, yet effective. A lot
    of times it’s difficult to get that “perfect balance” between superb
    usability and appearance. I must say you’ve done a
    amazing job with this. Also, the blog loads very fast for me on Internet explorer.
    Excellent Blog!

  57. hey there and thank you for your information – I have definitely piked up something nnew from
    right here. I did however expertise some technical issues using this web site,
    as I experienced to reload the web site a lot oof times previous to I could get
    it to load correctly. I had been wondering if your web
    hoswt is OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and
    could damage your high-quality score if advertising and mrketing
    with Adwords. Well I’m adding this RSS tto myy
    email and could look out for much more of yyour respective intriguing content.
    Make sure you update this aggain soon.

  58. Great site. Plenty of useful info here. I am sending
    it to some buddies ans additionally sharing in delicious. And naturally,
    thank you in your sweat!

  59. That kind of educational freedom is empowering’ for both teachers and students.
    However, very few people manage to reap the rewards they seek
    from their blog. If you give quality with reliability then people will purchase your product or
    service.

  60. I think this is among the most important info for me.

    And i am glad reading your article. But want
    to remark on few general things, The website style is
    perfect, the articles is really nice : D. Good job, cheers

    Check out my site frozen full movie online free

  61. Hi! I’ve been following your website for some time now and finally got the bravery to go ahead and give you a shout out
    from Kingwood Tx! Just wanted to say keep up the fantastic
    job!

    Feel free to surf to my blog – Clayton Kershaw Jersey

  62. Sweet blog! I fouynd it while searching on Yahoo News.
    Do you have aany suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Thanks

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
  5. How to Make WordPress Images Responsive.
  6. Responsive web design | iqpress
  7. Best WordPress snippets, hacks and tips from 2013 | JTB Productions

Leave a Comment