Skip to main content

HTML Guide

Bad value X for attribute “sizes” on element “img”: Bad CSS number token: Expected a minus sign or a digit but saw “a” instead at “auto,”.

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.

Learn more:

Related W3C validator issues