is it autofocus = "autofocus" or autofocus?

It seems to me that most (possibly all) of the attributes in previous versions of HTML (prior to HTML5) required that the attributes have values, for example readonly="readonly" .

Is this true for HTML5 and the autofocus attribute?

+77
html5
Dec 15 '10 at 0:50
source share
3 answers

In HTML, you use boolean attributes with or without values. A boolean value for W3C, such as autofocus, can be written as autofocus or autofocus="autofocus" , as well as autofocus="" .

If you don't want autofocus to just not record it.

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

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

+82
Dec 15 '10 at 1:00
source share

By labeling the HTML5 specification and adding a bit to 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 logical attribute for an element represents a true value, and the absence of an attribute represents a false value.

If an attribute is present, its value must be either an empty string or a value that does not match the ASCII value for the canonical attribute name, without a leading or trailing space.

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" /> 

Missing an attribute is the only valid syntax for false :

 <input type="text"/> 

Recommendation

If you want to write valid XHTML, use autofocus="autofocus" since <input autofocus> is not valid and other alternatives are less readable. Otherwise, just use <input autofocus> as it is shorter.

+40
Aug 22 '14 at
source share

No , just specify the attribute itself. So it was in HTML 4 .

Attributes include logical attributes . The presence of a logical attribute for an element represents a true value, and the absence of an attribute represents a false value.

If an attribute is present, its value must be either an empty string or a value that does not match the ASCII value for the canonical attribute name, without a leading or trailing space.

Example:

 <label><input type=checkbox checked name=cheese disabled> Cheese</label> 
+22
Dec 15 2018-10-12T00:
source share



All Articles