skip to Main Content

Is auto a valid value for the HTML attributes height and width ?

Please provide a reputable source. I was unable to find a reputable source saying yes or no (i.e. specifically settling this issue one way or the other).

(I know that style="width:auto" is valid CSS.
But my question is about the HTML <img> tag.)

2

Answers


  1. Chosen as BEST ANSWER

    For reference, the W3 validator says that auto is an invalid value:

    input:

    <img src="image.png" alt="" width="500" height="auto">
    

    output:

    "Error: Bad value auto for attribute height on element img"

    screenshot


  2. The HTML living standard specification attributes index says that for each, the value must be a Valid non-negative integer.

    This is defined as

    A string is a valid non-negative integer if it consists of one or more ASCII digits.

    Note however, that the attributes index is non-normative. For the normative requirement, the definition of the attribute on each element must be checked.

    For example on the <img> element the text says:

    The width and height attributes on img, iframe, embed, object, video, source when the parent is a picture element and, when their type attribute is in the Image Button state, input elements may be specified to give the dimensions of the visual content of the element (the width and height respectively, relative to the nominal direction of the output medium), in CSS pixels. The attributes, if specified, must have values that are valid non-negative integers.

    So since "auto" does not consist of one or more ASCII digits, it is not valid.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search