Responsive images

Responsive website design assumes using images that can adapt to different user devices and browsers. Uploadcare allows you to get the most appropriate image size and resolution to improve users’ accessibility and enhance your website’s performance.

Uploadcare’s on-the-fly CDN provides a comprehensive suite of image processing operations for crafting an optimal responsive image experience on your website. It automatically identifies the maximum compression level that maintains visual quality without noticeable artefacts. Additionally, it offers a robust set of image processing tools to resize, crop, and scale images to suit your requirements.

You can improve the display of responsive versions of an image using art direction.

When saving images to the Uploadcare CDN, you can access automatic intelligent conversion and compression through URL operations.

Auto format is automatically applied to this image.

1https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/

For retina screens add -/quality/lightest/:

1https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/quality/lightest/

Adaptive quality is also automatically applied to this image if you enable in the Dashboard.

Automatically

You can use the adaptive image component tool to adjust responsive images automatically.

You can implement a script to deliver optimized images automatically to users. Image optimization occurs in real-time during user requests, with results cached on our CDN.

  1. Connect the Adaptive Delivery JS script (ensure you use your own public API key) instead of YOUR_PUBLIC_KEY):
1<script src="https://cdn.jsdelivr.net/npm/@uploadcare/file-uploader@v1/web/uc-img.min.js" type="module"></script>
2
3<style>
4 uc-img {
5 --uc-img-pubkey: 'YOUR_PUBLIC_KEY';
6 }
7</style>
  1. If your images are hosted outside Uploadcare CDN, whitelist the image sources. Go to “Settings” → “Delivery” → “Allowed domains for Proxy” in the Dashboard, click “Add domain” and add the domains you intend to use.
  2. Replace your img tags with uc-img tag:
1<uc-img
2src="https://somestorage.example.com/somepicture.png"
3alt=""
4></uc-img>
5
6<!-- you can use file UUID for images stored on Uploadcare CDN -->
7
8<uc-img
9uuid="36f90908-afb9-4e0a-b254-1b769faf8f3a"
10alt=""
11></uc-img>

Note: uc-img is not self closing tag, so you need to close it with </uc-img>.

You can further enhance your responsive, optimized, and lazy-loaded images by adding various filters and effects and selecting different delivery options. Check out adaptive image component for more details.

Next.js

You can optimize images by using the Next.js image loader and component.

Nuxt

You can optimize images by using the Nuxt.

Manually

You can configure responsiveness manually.

Make a single image responsive

To make a single image responsive without using JS:

  • Resize the image to fit the widest container, ideally preserving its aspect ratio.
  • Set width: 100%; declaration to the image block so that the image adapts to the container width automatically.
  • For instance, if your original image resolution is 4928x3280px with a file size of 1.85 MB:
    https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/

If the container width is limited to 1200 px on the widest screens, use the resize operator in the image URL to reduce its resolution and file size:

https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/1200x/

Insert the updated image URL into the src attribute of the <img> tag:

1<!-- replace 36f90908-afb9-4e0a-b254-1b769faf8f3a with your image UUID -->
2
3<img
4 src="https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/1200x/"
5 alt=""
6>
7<style>
8 img {
9 width: 100%;
10 height: auto;
11 }
12</style>

The original image width has been reduced to 1200px, and the file size has been reduced to just 125 KB.

However, this 2400px wide image is hardly necessary for an iPhone version of the website, which needs only 320px width by a 2x pixel ratio, which gives us 640px. If you want to smartly adapt your images to various screen widths, continue to the next section.

Optimize the image for high pixel ratios

Set an additional version of the image for HiDPI screens with the <img> element and the x descriptor.

Retina screens have a pixel ratio of 2. To optimize for them:

  • Multiply the resize value by 2.
  • Reduce the image quality using the quality operation.
  • Add the alternative image link to the srcset attribute followed by the 2x descriptor that ties the image to devices with a 2x pixel ratio.
1<!-- replace 36f90908-afb9-4e0a-b254-1b769faf8f3a with your image UUID -->
2
3<img
4 src="https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/1200x/1.jpg"
5 srcset="https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/1200x/1.jpg 1x,
6 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/2400x/-/quality/lightest/2.jpg 2x"
7 alt="">

Example with the same image on Uploadcare CDN to produce an alternative retina version of it:

Optimize an image for different screen widths and breakpoints

Use URL API to:

  • Create various resized versions of the image for different screen widths.
  • Add them to the srcset attribute followed by a w descriptor with screen width in pixels
  • Add the sizes attribute to the <img> element to set primitive media conditions. It should contain information on how much of the viewport will be occupied by the image

Example from the previous section that now contains various versions of an image optimized for many different widths in w descriptors. Replace 36f90908-afb9-4e0a-b254-1b769faf8f3a with your image UUID:

1<img src="https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/1200x/fallback.jpg"
2 srcset="https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/320x/320.jpg 320w,
3 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/450x/450.jpg 450w,
4 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/640x/640.jpg 640w,
5 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/750x/750.jpg 750w,
6 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/800x/800.jpg 800w,
7 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/900x/900.jpg 900w,
8 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/1000x/-/quality/lighter/1000.jpg 1000w,
9 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/1200x/-/quality/lighter/1200.jpg 1200w,
10 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/1500x/-/quality/lighter/1500.jpg 1500w,
11 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/1600x/-/quality/lighter/1600.jpg 1600w,
12 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/2000x/-/quality/lightest/2000.jpg 2000w"
13 sizes="(min-width: 1200px) 50vw, 90vw"
14 alt="">`
15/>

In this example, this value of sizes tells the browser to set the image width to 50vw if the viewport width is 1000px or more; otherwise, occupy most of the screen width: sizes="(min-width: 1200px) 50vw, 90vw"

Take a look at this to see our pal Greenpaws adapt to various screen sizes, and make sure to try some of your own pictures in it.

Art direction

The art direction issue involves wanting to change the image displayed to suit different image display sizes. For example, in a broader sense, apply filters, control colors, and so on, or a web page includes a large landscape shot with an object in the middle when viewed in a desktop browser. When viewed on a mobile browser, that same image is shrunk down, making the object in the image tiny and hard to see. It would probably be better to show an image on mobile, which zooms in on the object.

Scale instead of resizing example

Usually, your image looks the same in portrait versions of devices, on desktops, and in landscape versions. That may be fine for square images, but the subjects of the wide images will probably end up looking too small on mobiles:

Landscape-oriented image resized on a smartphone

Portrait-oriented resize works better for the smartphone screen

Use automatic cropping of an image to an object using the scale_crop operation instead of resizing images and combining them with media queries.

To use the same image for different layouts — for example, a landscape image showing the entire scene for a desktop layout and a portrait image showing an enlarged main subject for a mobile layout — use the <picture> element.

Use srcset/sizes to create a resolution switcher example, serving the same-size image at different resolutions depending on the device resolution or different image sizes depending on the viewport widths.

1<!-- replace 36f90908-afb9-4e0a-b254-1b769faf8f3a with your image UUID -->
2
3<picture>
4 <source
5 srcset="https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/scale_crop/320x569/center/320-569.jpg 320w,
6 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/scale_crop/640x1138/center/-/quality/lightest/640-1138.jpg 640w,
7 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/scale_crop/450x800/center/450-800.jpg 450w,
8 https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/scale_crop/900x1600/center/-/quality/lightest/900-1600.jpg 900w"
9 sizes="90vw"
10 media="(max-width: 450px) and (orientation: portrait)">
11
12 <img src="https://ucarecdn.com/36f90908-afb9-4e0a-b254-1b769faf8f3a/-/resize/1200x/fallback.jpg"
13 alt="Greenpaws the Chameleon">
14</picture>

Note: We recommend using the media attribute only in art direction scenarios. If you use media, don’t also offer media conditions within the sizes attribute.

Enable art direction

Art direction is the focus of the main subject of the image. It is essential for smaller screens. On the large screen, we can show the entire image, but on a smaller screen, we want to show just the main part of the image.

We want to show only the chameleon’s head on smartphones:

Horizontal image resized on a smartphone screen

Use the crop operation with the resize operation to get a custom part of an image. Use the <picture> element to implement an art direction picture switcher.

1<picture>
2 <source
3 srcset="https://ucarecdn.com/:uuid/-/crop/2048x1365/1345,670/-/resize/320x/320.jpg 320w,
4 https://ucarecdn.com/:uuid/-/crop/2048x1365/1345,670/-/resize/640x/640.jpg 640w,
5 https://ucarecdn.com/:uuid/-/crop/2048x1365/1345,670/-/resize/450x/450.jpg 450w,
6 https://ucarecdn.com/:uuid/-/crop/2048x1365/1345,670/-/resize/900x/900.jpg 900w"
7 sizes="90vw"
8 media="(max-width: 450px) and (orientation: portrait)">
9
10 <img src="https://ucarecdn.com/:uuid/-/resize/1000x/fallback.jpg"
11 srcset="https://ucarecdn.com/:uuid/-/resize/320x/-/quality/lighter/320.jpg 320w,
12 https://ucarecdn.com/:uuid/-/resize/450x/-/quality/lighter/450.jpg 450w,
13 https://ucarecdn.com/:uuid/-/resize/640x/-/quality/lighter/640.jpg 640w,
14 https://ucarecdn.com/:uuid/-/resize/750x/-/quality/lighter/750.jpg 750w,
15 https://ucarecdn.com/:uuid/-/resize/800x/-/quality/lighter/800.jpg 800w,
16 https://ucarecdn.com/:uuid/-/resize/900x/-/quality/lighter/900.jpg 900w,
17 https://ucarecdn.com/:uuid/-/resize/1000x/-/quality/lighter/1000.jpg 1000w,
18 https://ucarecdn.com/:uuid/-/resize/1200x/-/quality/lighter/1200.jpg 1200w,
19 https://ucarecdn.com/:uuid/-/resize/1500x/-/quality/lighter/1500.jpg 1500w,
20 https://ucarecdn.com/:uuid/-/resize/1600x/-/quality/lighter/1600.jpg 16000w,
21 https://ucarecdn.com/:uuid/-/resize/2000x/-/quality/lightest/2000.jpg 2000w"
22 sizes="(min-width: 1000px) 50vw, 90vw">
23</picture>

Use the crop operation to get the main part of an image and use the resize operation after that.

1https://ucarecdn.com/:uuid/-/crop/1800x1200/1090,1220/-/resize/900x/

If your image contains a face, it’s easy to use the crop by object operation without additional resizing.