Skip to content

Commit 17aee88

Browse files
committed
Added info about width parameter
1 parent 9dd0ada commit 17aee88

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

src/content/docs/images/transform-images/make-responsive-images.mdx

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ sidebar:
66

77
---
88

9+
You can serve responsive images in two different ways:
10+
- Use the HTML `srcset` feature to allow browsers to choose the most optimal image. This is the most reliable solution to serve responsive images.
11+
- Use the `width=auto` option to serve the most optimal image based on the available browser and device information. This is a server-side solution that is supported only by Chromium-based browsers.
12+
13+
## Transform with HTML `srcset`
14+
915
The `srcset` [feature of HTML](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images) allows browsers to automatically choose an image that is best suited for user’s screen resolution.
1016

1117
`srcset` requires providing multiple resized versions of every image, and with Cloudflare’s image transformations this is an easy task to accomplish.
@@ -15,7 +21,7 @@ There are two different scenarios where it is useful to use `srcset`:
1521
* Images with a fixed size in terms of CSS pixels, but adapting to high-DPI screens (also known as Retina displays). These images take the same amount of space on the page regardless of screen size, but are sharper on high-resolution displays. This is appropriate for icons, thumbnails, and most images on pages with fixed-width layouts.
1622
* Responsive images that stretch to fill a certain percentage of the screen (usually full width). This is best for hero images and pages with fluid layouts, including pages using media queries to adapt to various screen sizes.
1723

18-
## `srcset` for high-DPI displays
24+
### `srcset` for high-DPI displays
1925

2026
For high-DPI display you need two versions of every image. One for `1x` density, suitable for typical desktop displays (such as HD/1080p monitors or low-end laptops), and one for `2x` high-density displays used by almost all mobile phones, high-end laptops, and 4K desktop displays. Some mobile phones have very high-DPI displays and could use even a `3x` resolution. However, while the jump from `1x` to `2x` is a clear improvement, there are diminishing returns from increasing the resolution further. The difference between `2x` and `3x` is visually insignificant, but `3x` files are two times larger than `2x` files.
2127

@@ -34,7 +40,7 @@ The `srcset` attribute adds another, high-DPI image. The browser will automatica
3440

3541
Note that it does not make sense to scale images up for use in `srcset`. That would only increase file sizes without improving visual quality. The source images you should use with `srcset` must be high resolution, so that they are only scaled down for `1x` displays, and displayed as-is or also scaled down for `2x` displays.
3642

37-
## `srcset` for responsive images
43+
### `srcset` for responsive images
3844

3945
When you want to display an image that takes a certain percentage of the window or screen width, the image should have dimensions that are appropriate for a visitor’s screen size. Screen sizes vary a lot, typically from 320 pixels to 3840 pixels, so there is not a single image size that fits all cases. With `<img srcset>` you can offer the browser several possible sizes and let it choose the most appropriate size automatically.
4046

@@ -61,7 +67,7 @@ If the image is not displayed at full width of the screen (or browser window), y
6167
* If the image is displayed at full width of a fixed-width column, use the first technique that uses one specific image size.
6268
* If it takes a specific percentage of the screen, or stretches to full width only sometimes (using CSS media queries), then add the `sizes` attribute as described below.
6369

64-
### The `sizes` attribute
70+
#### The `sizes` attribute
6571

6672
If the image takes 50% of the screen (or window) width:
6773

@@ -86,7 +92,7 @@ The `vw` unit is a percentage of the viewport (screen or window) width. If the i
8692

8793
In this example, `sizes` says that for screens smaller than 640 pixels the image is displayed at full viewport width; on all larger screens the image stays at 640px. Note that one of the options in `srcset` is 1280 pixels, because an image displayed at 640 CSS pixels may need twice as many image pixels on a high-dpi (`2x`) display.
8894

89-
## What about other formats?
95+
## WebP images
9096

9197
`srcset` is useful for pixel-based formats such as PNG, JPEG, and WebP. It is unnecessary for vector-based SVG images.
9298

@@ -96,3 +102,42 @@ If you want to use WebP images, but do not need resizing, you have two options:
96102

97103
* You can enable the automatic [WebP conversion in Polish](/images/polish/activate-polish/). This will convert all images on the site.
98104
* Alternatively, you can change specific image paths on the site to start with `/cdn-cgi/image/format=auto/`. For example, change `https://example.com/assets/hero.jpg` to `https://example.com/cdn-cgi/image/format=auto/assets/hero.jpg`.
105+
106+
## Transform with `width` parameter
107+
108+
When setting up a [transformation URL](/images/transform-images/transform-via-url/#width), you can apply the width=auto option to serve the most optimal image based on the available information about the user's browser and device.
109+
110+
This method can serve multiple sizes from a single URL. Currently, images will be served in one of four sizes:
111+
112+
- 1200 (large desktop/monitor)
113+
- 960 (desktop)
114+
- 768 (tablet)
115+
- 320 (mobile)
116+
117+
Each width is counted as a separate transformation. For example, if you use `width=auto` and the image is delivered with a width of 320px to one user and 960px to another user, then this counts as two unique transformations.
118+
119+
By default, this feature uses information from the user agent, which detects the platform type (e.g. iOS, Android) and browser.
120+
121+
### Client hints
122+
123+
For more accurate results, you can use client hints to send the user's browser information as request headers.
124+
125+
This method currently works only on Chromium-based browsers such as Chrome, Edge, and Opera.
126+
127+
You can enable client hints via HTML by adding the following tag in the `<head>` tag of your page before any other elements:
128+
129+
```txt
130+
<meta http-equiv="Delegate-CH" content="sec-ch-dpr https://example.com; sec-ch-viewport-width https://example.com"/>
131+
```
132+
133+
Replace `https://example.com` with your Cloudflare zone where transformations are enabled.
134+
135+
Alternatively, you can enable client hints via HTTP by adding the following headers to your HTML page's response:
136+
137+
``txt
138+
critical-ch: sec-ch-viewport-width, sec-ch-dpr
139+
140+
permissions-policy: ch-dpr=("https://example.com"), ch-viewport-width=("https://example.com")
141+
```
142+
143+
Replace `https://example.com` with your Cloudflare zone where transformations are enabled.

src/content/partials/images/width.mdx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
---
44
import { Tabs, TabItem } from "~/components"
55

6-
Specifies maximum width of the image in pixels. Exact behavior depends on the `fit` mode (described below).
6+
Specifies maximum width of the image. Exact behavior depends on the `fit` mode; use the `fit=scale-down` option to ensure that the image will not be enlarged unnecessarily.
7+
8+
Available options are a specified width in pixels or `auto`.
79

810
<Tabs>
911
<TabItem label="URL format">
@@ -21,4 +23,12 @@ Specifies maximum width of the image in pixels. Exact behavior depends on the `f
2123
cf: {image: {width: 250}}
2224
```
2325
</TabItem>
24-
</Tabs>
26+
</Tabs>
27+
28+
Ideally, image sizes should match exactly the size they are displayed on the page. If the page contains thumbnails with markup such as `<img width="200">`, then you can resize the image by applying `width=200`.
29+
30+
[To serve responsive images](/images/transform-images/make-responsive-images/#transform-with-html-srcset), you can use the HTML srcset element and apply width parameters.
31+
32+
`auto`
33+
34+
Automatically serves the image in the most optimal width based on available information about the browser and device. This method is supported only by Chromium browsers. For more information about this works, refer to [Transform width parameter](/images/transform-images/make-responsive-images/#transform-with-width-parameter).

0 commit comments

Comments
 (0)