Skip to main content

HTML Guide

The “sizes” attribute must only be specified if the “srcset” attribute is also specified.

The W3C validator error you are seeing indicates that you’ve used the sizes attribute on an <img> element without providing a srcset attribute. According to HTML specifications, the sizes attribute is only meaningful when used alongside the srcset attribute.

The srcset attribute allows you to specify a list of possible image files for the browser to choose from based on conditions like the screen size or resolution. The sizes attribute helps the browser understand what size the image should be under specific conditions defined in srcset.

How to Fix the Issue

There are two options, adding or removing attributes.

  1. Adding a srcset Attribute: If you need responsive images, make sure to include the srcset attribute in your <img> element.

  2. Removing the sizes Attribute (if not necessary): If responsive images are not required and you’re not using srcset, simply remove the sizes attribute.

Example

Incorrect Markup:

<img src="image.jpg" sizes="(max-width: 600px) 480px, 800px" alt="Example Image">

Correct Markup with srcset:

<img src="image.jpg" 
     srcset="image-480w.jpg 480w, image-800w.jpg 800w" 
     sizes="(max-width: 600px) 480px, 800px" 
     alt="Example Image">

In this correct example, the srcset attribute provides two different images (image-480w.jpg and image-800w.jpg) and their corresponding widths (480w and 800w). The sizes attribute tells the browser to use the 480 pixels wide image if the viewport is 600 pixels or less, otherwise, it will use the full 800 pixels.

Correct Markup without sizes and srcset:

<img src="image.jpg" alt="Example Image">

If you don’t need responsive images, simply remove the sizes attribute to fix the issue.

Summary

Ensure that whenever you use the sizes attribute, you also include a srcset attribute. This makes sure that the browser can effectively make decisions about which image size to load in different scenarios.

Learn more:

Related W3C validator issues