91

I seem to recall most (maybe all) attributes in previously versions of HTML (before HTML5) required attributes to have values, like readonly="readonly".

Is this true for HTML5 and the autofocus attribute?

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261

3 Answers3

95

In HTML, you use boolean attributes with or without values as you like. A boolean, for W3C, like autofocus can be written like that autofocus or autofocus="autofocus" or also autofocus="".

If you don't want autofocus just don't write it.

I think you are confused because XHTML requires values for all attributes: attributes="values".

Here is some information about boolean attribute use in HTML: http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#boolean-attribute

Community
  • 1
  • 1
Tim
  • 1,938
  • 1
  • 13
  • 20
  • 19
    +1 for mentioning XHTML. The XML conformity of XHTML is the only reason why there has ever been a `disabled="disabled"`. The same thing goes for closing tags. In HTML not every tag needs to be closed (e.g. br or input) but since XHTML must be valid XML, you need closing tags as well. – Tim Büthe Jan 06 '14 at 13:23
  • 5
    "XHTML is the only reason why there has ever been a `disabled="disabled"`". And SGML is the only reason, why XHTML has the `silly="silly"` bool syntax instead of just `whatever="true"`, and HTML is the only reason why `this="false"` can mean this = true. :) – Sz. Oct 10 '18 at 18:57
53

Quoting the HTML5 spec and expanding a bit on Pekka:

http://www.w3.org/TR/html5/forms.html#autofocusing-a-form-control:-the-autofocus-attribute :

The autofocus attribute is a boolean attribute.

http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes :

The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

Conclusion:

The following are valid, equivalent and true:

<input type="text" autofocus />
<input type="text" autofocus="" />
<input type="text" autofocus="autofocus" />
<input type="text" autofocus="AuToFoCuS" />

The following are invalid:

<input type="text" autofocus="0" />
<input type="text" autofocus="1" />
<input type="text" autofocus="false" />
<input type="text" autofocus="true" />

The absence of the attribute is the only valid syntax for false:

<input type="text"/>

Recommendation

If you care about writing valid XHTML, use autofocus="autofocus", since <input autofocus> is invalid and other alternatives are less readable. Else, just use <input autofocus> as it is shorter.

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
22

No, it's enough to specify the attribute itself. It was that way also in HTML 4.

A number of attributes are boolean attributes. The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

Example:

<label><input type=checkbox checked name=cheese disabled> Cheese</label>
Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088