

By Mikhail Kokarev image optimization 7 min read Alternatively, you can change specific image paths on the site to start with /cdn-cgi/image/format=auto/.This will convert all images on the site. You can enable the automatic WebP conversion in Polish.

If you want to use WebP images, but do not need resizing, you have two options: Cloudflare can serve WebP images automatically whenever you use /cdn-cgi/image/format=auto URLs in src or srcset. HTML also supports the element External link icon Open external link that can optionally request an image in the WebP format, but you do not need it. It is unnecessary for vector-based SVG images. Srcset is useful for pixel-based formats such as PNG, JPEG, and WebP. 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.
#Image responsive resize full
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. In the src attribute the browser will pick any size that is a good fallback for older browsers that do not understand srcset. With you can offer the browser several possible sizes and let it choose the most appropriate size automatically.īy default, the browser assumes the image will be stretched to the full width of the screen, and will pick a size that is closest to 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. 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. 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. That would only increase file sizes without improving visual quality. Note that it does not make sense to scale images up for use in srcset. It will be displayed at the same size as a 960 pixel image, but with double the number of pixels which will make it look twice as sharp on high-DPI displays. In this case, specifying width=1920 (two times 960 pixels) and adding 2x at the end, informs the browser that this is a double-density image. The browser will automatically select between the images in the src and srcset. The srcset attribute adds another, high-DPI image. assets/product.jpg is a URL to the source image on the server. This is followed by width=960 which resizes the image to have a width of 960 pixels. cdn-cgi/image/ is a special path for resizing images. In the URL path used in this example, the src attribute is for images with the usual “1x” density.
#Image responsive resize code
The difference between 2x and 3x is visually insignificant, but 3x files are two times larger than 2x files.Īssuming you have an image product.jpg in the assets folder and you want to display it at a size of 960px, the code is as follows: However, while the jump from 1x to 2x is a clear improvement, there are diminishing returns from increasing the resolution further.

Some mobile phones have very high-DPI displays and could use even a 3x resolution. 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. srcset for high-DPI displaysįor high-DPI display you need two versions of every image. This is best for hero images and pages with fluid layouts, including pages using media queries to adapt to various screen sizes. Responsive images that stretch to fill a certain percentage of the screen (usually full width).This is appropriate for icons, thumbnails, and most images on pages with fixed-width layouts. These images take the same amount of space on the page regardless of screen size, but are sharper on high-resolution displays. Images with a fixed size in terms of CSS pixels, but adapting to high-DPI screens (also known as Retina displays).There are two different scenarios where it is useful to use srcset: Srcset requires providing multiple resized versions of every image, and with Cloudflare’s Image Resizing this is an easy task to accomplish. The srcset feature of HTML External link icon Open external link allows browsers to automatically choose an image that is best suited for user’s screen resolution.
