|
| 1 | +--- |
| 2 | +pcx_content_type: tutorial |
| 3 | +title: Optimize mobile viewing |
| 4 | +description: Lazy loading is an easy way to optimize the images on your webpages for mobile devices, with faster page load times and lower costs. |
| 5 | +sidebar: |
| 6 | + order: 1 |
| 7 | +--- |
| 8 | + |
| 9 | +You can use lazy loading to optimize the images on your webpages for mobile viewing. This helps address common challenges of mobile viewing, like slow network connections or weak processing capabilities. |
| 10 | + |
| 11 | +Lazy loading has two main advantages: |
| 12 | +* **Faster page load times** — Images are loaded as the user scrolls down the page, instead of all at once when the page is opened. |
| 13 | +* **Lower costs for image delivery** — When using Cloudflare Images, you only pay to load images that the user actually sees. With lazy loading, images that are not scrolled into view do not count toward your billable Images requests. |
| 14 | + |
| 15 | +Lazy loading is natively supported on all Chromium-based browsers like Chrome, Safari, Firefox, Opera, and Edge. |
| 16 | + |
| 17 | +:::note |
| 18 | +If you use older methods, involving custom JavaScript or a JavaScript library, lazy loading may increase the initial load time of the page since the browser needs to download, parse, and execute JavaScript. |
| 19 | +::: |
| 20 | + |
| 21 | +## Modify your loading attribute |
| 22 | + |
| 23 | +Without modifying your loading attribute, most browsers will fetch all images on a page, prioritizing the images that are closest to the viewport by default. You can override this by modifying your `loading` attribute. |
| 24 | + |
| 25 | +There are two possible `loading` attributes for your `<img>` tags: `lazy` and `eager`. |
| 26 | + |
| 27 | +### Lazy loading |
| 28 | + |
| 29 | +Lazy loading is recommended for most images. With Lazy loading, resources like images are deferred until they reach a certain distance from the viewport. If an image does not reach the threshold, then it does not get loaded. |
| 30 | + |
| 31 | +Example of modifying the `loading` attribute of your `<img>` tags to be `"lazy"`: |
| 32 | +````HTML |
| 33 | +<img src="example.com/cdn-cgi/width=300/image.png" loading="lazy"> |
| 34 | +```` |
| 35 | + |
| 36 | +### Eager loading |
| 37 | + |
| 38 | +If you have images that are in the viewport, eager loading, instead of lazy loading, is recommended. Eager loading loads the asset at the initial page load, regardless of its location on the page. |
| 39 | + |
| 40 | +Example of modifying the `loading` attribute of your `<img>` tags to be `"eager"`: |
| 41 | +````HTML |
| 42 | +<img src="example.com/cdn-cgi/width=300/image.png" loading="eager"> |
| 43 | +```` |
0 commit comments