HTML Guide
The sizes
attribute is used to complement the srcset
attribute on an <img>
tag for responsive images, therefore it’s not valid if srcset
is missing.
Learn more:
Related W3C validator issues
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.
-
Adding a srcset Attribute: If you need responsive images, make sure to include the srcset attribute in your <img> element.
-
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.
Check the HTMLImageElement.srcset guide to learn about the correct usage of the srcset and sizes attributes.
The sizes attribute specifies the size of the image when it is displayed on different devices.
The error message is saying that the value auto is not a valid value for the sizes attribute.
To fix this issue, you need to replace the value auto with a valid size. You can use a width descriptor or a media query to specify the size for different device widths.
Here’s an example of using a width descriptor:
<img src="example.jpg" sizes="(max-width: 600px) 100vw, 50vw" />
This example sets the size of the image to 100% of the viewport width when the device width is less than or equal to 600px, and 50% of the viewport width for larger device widths.
Alternatively, you can remove the sizes attribute altogether and let the browser decide the best size for the image based on the viewport size.
<img src="example.jpg" />
If you do this, the browser will use the default sizes value of 100vw and will scale the image accordingly.
The sizes attribute is used to complement the srcset attribute on an <img> tag for responsive images. When this attribute is present, all image candidates must specify its width.
The sizes attribute for an img element requires valid CSS syntax, and auto is not an acceptable value within that attribute.
The sizes attribute allows you to specify a list of media conditions and corresponding sizes for the images. Each condition determines which size of the image should be displayed at different viewport widths, ensuring responsive image delivery. The syntax for sizes should be a comma-separated list of media queries followed by a value denoting the corresponding width of the image. This width value may be in pixels (px) or as a percentage (vw, vh), but auto is not valid in this context.
Here is a breakdown of a correct sizes attribute usage:
- 50vw: This denotes that the image should take up 50% of the viewport’s width.
- (max-width: 600px) 100vw, 50vw: When the viewport is at most 600 pixels wide, the image should occupy the full width (100vw). Otherwise, it should take 50 percent of the viewport width.
Remove “auto” from your sizes value and provide a valid, contextually correct CSS value.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Responsive Images Example</title>
</head>
<body>
<img
src="image.jpg"
sizes="(max-width: 472px) 100vw, 472px"
srcset="image-small.jpg 300w, image-medium.jpg 600w, image-large.jpg 1000w"
alt="A description of the image"
>
</body>
</html>
In this example, if the viewport width is less than or equal to 472 pixels, the image will take up the entire width (100vw). For larger widths, the srcset specifies different image files for varying resolutions.
The srcset property on img elements, when used, requires at least one value. This property is a string which identifies one or more image candidate strings, separated using commas (,) each specifying image resources to use under given circumstances.
Each image candidate string contains an image URL and an optional width or pixel density descriptor that indicates the conditions under which that candidate should be used instead of the image specified by the src property.
Example:
<img
src="/img/cat-200px.png"
alt="Cat"
srcset="
/img/cat-200px.png 1x,
/img/cat-400px.png 2x
">
Ensure all descriptors in the srcset have positive widths greater than zero. In the srcset attribute, each source candidate consists of a URL followed by a width descriptor, which should be a positive integer followed by w.
The issue arises from using 0w, which is not valid as width descriptors must be positive numbers. The srcset attribute allows browsers to choose the appropriate image from different candidates based on device resolution and screen size, so specifying a meaningful width is important.
Here’s how you can fix it:
- Verify that each candidate in the srcset includes a valid URL and a width descriptor greater than zero.
- Decide the relevant width for your images and update the descriptors accordingly.
Here’s a corrected example of how to use the srcset attribute:
<picture>
<source
srcset="/images/icon_small_1x.png 300w,
/images/icon_small_2x.png 600w"
media="(max-width: 600px)">
<img src="/images/icon_fallback.png"
alt="App Logo">
</picture>
In this example, each image in srcset has a positive width descriptor: 300w and 600w. This ensures that the browser can make an appropriate choice based on the current viewport and resolution, adhering to the W3C HTML standards.
The srcset property on source elements, when used, requires at least one value.
The <source> HTML element specifies multiple media resources for the <picture>, the <audio> element, or the <video> element. It is a void element, meaning that it has no content and does not have a closing tag. It is commonly used to offer the same media content in multiple file formats in order to provide compatibility with a broad range of browsers given their differing support for image file formats and media file formats.
The srcset attribute is required if the source element’s parent is a <picture> element, but not allowed if the source element’s parent is an <audio> or <video> element.
The sizes attribute is used to complement the srcset attribute on an <img> tag for responsive images. When this attribute is present, all image candidates must specify its width.