Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 8, 2021 09:49 pm GMT

3 attributes your images must have!

Contents

  1. Intro
  2. Images
  3. Background Images
  4. Summary

Intro

They say a picture tells a thousand words. It certainly costs a thousand words when it comes to performance!
Images tend to be quite large files, so take a while to load. They also block rendering so they have to complete before content further down the page can load.

But there is hope! We can make images lazy and get out the way of the rest of the page with some simple attributes!

First thing first, make sure your images are as small as possible in the first case. I'd highly recommend Squoosh by the Chrome team which you can use to optimise the size of your images.

Convert them to more modern formats like webp which can save up to 26% of file size just by changing the file type!! Webp has fairly good browsers support now too.

Images

Here's a current image on a website:

<img src="some_image_file.png" />

We can improve the loading of this image with a few attributes, like so:

<img src="some_image_file.png" loading="lazy" decoding="async" alt="A Description of the image" />

Easy!!

Lets dig into those attributes.

Loading

It has wide browser support now, and if the browser doesn't support it, it will just be ignored, so no excuse not to add it!

It can have the values of eager or lazy.
Eager is the default value, meaning it will load the image immediately regardless of if the user can currently see the image.
Lazy allows the browser to decide when to load the image, which is will do when the user is nearer the content. Meaning we don't have to load a full page of images straight away, only the ones we can see immediately.

Decoding

This is supported across all browsers.
This gives the browser a hint on how you want your image to be decoded, either synchronously or asynchronously. Either getting in the way of the rest of your content or not.

Alt

This doesn't impact loading performance. But please use it!
It takes very little effort to add and helps make your site more accessible!
The best way to come up with the value is to imagine you are describing the image to your friend over the phone, how would you do it.
This is exactly how the alt attribute functions to screen readers so please use it!

Background Images

Background images are a bit more tricky to be lazy loading.

Firstly I would really try not to use background images as they aren't great for accessibility, so if at all possible use a normal image element.

If you absolutely can't use a normal image. Then you'll have to resort to using JavaScript to only display the image when its near to viewport.

This can be achieved with the Intersection Observer, there is a great example of that on the web.dev site which I would recommend if you absolutely can't use the image element.

Summary

In summary, these 3 attributes improve the performance of your website with minimal effort!
There is no browser support issues or any reason not to use them, so add them to your images now!

These attributes also work is you are using a picture element, just set them on the image element as normal.
Here's an example of using a picture element for switching images based on dark mode or not.

<picture>    <source srcset="/images/linkedin-white.webp" media="(prefers-color-scheme: dark)" />    <img class="linkedin" src="/images/linkedin.webp" alt="LinkedIn Profile" loading="lazy" decoding="async" /></picture>

They also work is you are using a image element, with multiple sized images via srcset attribute.

Add these 3 simple attributes to your images right now!

Happy Building!


Original Link: https://dev.to/jordanfinners/3-attributes-your-images-must-have-3fhj

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To