ponyfoo.com

Make All Images on Your Website Responsive in 3 Easy Steps

Fix
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.

Size-Fitting

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:

<script
  src="https://cdnjs.cloudflare.com/ajax/libs/cloudinary-core/2.3.0/cloudinary-core-shrinkwrap.min.js">
</script>

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:

<img
  data-src="
    http://res.cloudinary.com/christekh/image/upload/w_auto,c_scale/v1501761946/pexels-photo-457044_etqwsd.jpg
  "
  alt=""
  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' })
cl.responsive()

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 (5)

Ben Abbott wrote

Hi

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