Make All Images on Your Website Responsive in 3 Easy Steps

A relevant ad will be displayed here soon. These ads help pay for my hosting.
Please consider disabling your ad blocker on Pony Foo. These ads help pay for my hosting.
You can support Pony Foo directly through Patreon or via PayPal.

Images are crucial to website performance, but most still don’t implement responsive images. It’s not just about fitting an image on the screen, but also making the the image size relatively rational to the device. The srcset and sizes options, which are your best hit are hard to implement. Cloudinary provides an easier way, which we will discuss in this article.

In this blog, I’ll share a valuable shortcut that can help you turn all images responsive. But first, let’s talk a bit about what responsive images are: an image is considered responsive if it retains its quality on varying device sizes without having an unpleasant impact on performance.

To better understand this concept, let’s take a look at some device dimensions:

Possible Full-Width Image Dimensions by Screen Sizes

  • Large/X-Large commercial screens: 2000+ pixels
  • Websites: 760 - 1200 pixels
  • Mobile Phones: < 760 pixels

Let’s assume you have a mobile-first strategy for building responsive apps. You may decide to use 760px images throughout, without considering larger screens. If these images are meant to take the full width of the devices on which they are rendered, then your content will look distorted and unprofessional on websites or commercial screens.

Your next attempt would be to use the largest possible image (2000px) and scale it down based on the screen sizes on which it is displayed.

Down-Scaling Images

Using CSS or JavaScript to down-scale images only makes them dimensionally responsive. The following image illustrates this better:

Down-scaling illustration on mobile
Down-scaling illustration on mobile
Down-scaling illustration on desktop
Down-scaling illustration on desktop

Both on web and mobile devices, the size is still 8.9MB. Bearing in mind that your mobile phones have less resources than your PC, we still have more work to do.

Using only a down-scaling approach is not ideal because it does not account for the size of the image; just its dimensions.

We have already seen that up-scaling wouldn’t work either, hence we need something that handles both dimensions and size.


Our best option is to generate images based on the screen size and render them. This process can be extremely complex, but there’s a shortcut that can help you automate much of the process. You can make all images responsive in three easy steps with Cloudinary:

1. Include Cloudinary in Your Project

Add the Cloudinary SDK to your project simply by including it in your index.html using script tags:


2. Add Images with data-src

You don’t want the images rendered immediately, until JavaScript runs. Hence, include the image using the data-src attribute instead of src:

  class="cld-responsive" />

Using this approach, Cloudinary analyzes your browser screen first, resizes the image saved in Cloudinary storage as provided in data-src, and then renders the image in the appropriate size and dimension using JavaScript.

Two things to note from the tag:

  • w_auto,c_scale transformation parameters tell Cloudinary to dynamically generate an image URL scaled to the correct width value, based on the detected width actually available for the image in the containing element.
  • The class cld-responsive tells Cloudinary which images to apply this feature too.

3. JavaScript Call

Finally, initialize a Cloudinary instance in your JavaScript files and call the responsive method on this instance:

const cl = cloudinary.Cloudinary.new({ cloud_name: 'YOUR_CLOUD_NAME' })

Remember to create a free Cloudinary account so you can receive a cloud name for configuring this instance.

This piece of code will walk through the DOM, find all image tags with the class cld-responsive to apply size and dimension fitting images on them.

Final Words

Always keep in mind that when you use CSS like the following code below to make images responsive, it does not guarantee a good user experience:

img {
  width: 100%;
  height: auto;

The sizes of these images remain the same. Large images on mobile devices eat up resources (like allocated memory and running processes) causing slow downloads or unexpected behavior on the user’s device. Responsive images ensure that users save lots of data bandwidth & have great experiences when using your image-rich website or app.

Lastly, it’s good to keep in mind that the suggested approach relies on JavaScript. Therefore, the preloading capabilities provided by srcset and sizes are sacrificed and your browser must have JavaScript enabled for this feature to work.

Liked the article? Subscribe below to get an email when new articles come out! Also, follow @ponyfoo on Twitter and @ponyfoo on Facebook.
One-click unsubscribe, anytime. Learn more.

Comments (8)

Ben Abbott wrote


Does it work with embedded video such as youtube?

Michael J. Ryan wrote

Youtube already has controls in place, and user configurations for size/data rates.

John Doe wrote

How is this supposed to be easier than declaring srcset and sizes?

Arthur wrote

Well, for example, it requires the user to have JavaScript enabled and then forces you to link your images against a third-party service.

More seriously, I guess this means you don’t need to worry about manually resizing the image in Photoshop or ImageMagick, which might be useful if you don’t already have a build process on your site. And it should work for browsers that don’t support srcset, although it’s not like srcset doesn’t include a fallback.

And I guess the syntax is easier? 🤷

Lukas wrote

“Sponsored” might be a keyword here

Christian Kaindl wrote

What about the HTML picture element? Besides eventual compatibility issues (because it is relatively new), are there any disadvantages from using picture?

Liam Quin wrote

The challenge is to be responsive and still be accessible. With this approach, if the JavaScript is blocked or the CSS isn’t loaded, the user doesn’t get the image at all. Why not use the src attribute to supply for whatever is likely to be the most common case and then change it if needed?

In addition, the lack of height and width attributes on the image mean that either you have to use blocking CSS and javaScript or the user will see a page reflow when the image loads, which can considerably degrade the user experience.

Francois Devatine wrote

Shouldn’t we mention CSS @media as a good way to have a responsive image? Or is it already outdated?

@media only screen and (max-width: 899px) { .responsiveImage { background-image: url("./image_for_smartphone.png"); } }

@media only screen and (min-width: 900px) { .responsiveImage { background-image: url("./image_for_desktop.png"); } }