Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are attributes without value allowed in HTML4?

Tags:

html

html4

I wonder if HTML 4 allows attributes without value, as being equivalent to attributes with an empty value. For example:

<h2 section>foobar</h2>

instead of:<h2 section="">foobar</h2>

Are the two snippets equally valid? If not, are they valid in HTML version 5?

thanks!

like image 512
fstab Avatar asked Feb 15 '14 10:02

fstab


2 Answers

As formally defined, HTML 4 does not allow attributes without a value. What is commonly regarded as attribute without value, as in <input checked>, is formally an attribute value without an attribute name (and an equals sign). Though misleadingly characterized as “boolean attributes” with special minimization rules in HTML 4 specs, those specs normatively cite the SGML standard.

By the SGML standard, whenever an attribute is declared by enumerating keywords that are the only allowed values, an attribute specification may, under certain conditions, be minimized to the value. This means that in HTML 4, the tag <input checkbox> is valid; the attribute is a minimized form of type=checkbox. No browser supports that (they parse checkbox as attribute name), but in validators, the construct passes.

In practice, the part of the attribute minimization rules that browsers support consists of just the special cases where an attribute is declared as allowing a single keyword value only, such as the checked attribute, which is formally declared with

 <!ATTLIST INPUT checked     (checked)      #IMPLIED>

So it depends on how the attribute is declared in the HTML 4 spec.

But this means that the minimized attribute checked means checked=checked. The value is not empty but the keyword checked. On the other hand, browsers treat such attributes as “presence attributes”: what matters is whether an element has that attribute or not, not its value.

In HTML5 serialized as XHTML (i.e., as XML), things are simple: every attribute specification must be of the form name="value" or name='value', so the equals sign is required, and so are the quotation marks; logically, the value is always there, though it can be the empty string, as in alt="".

In HTML5 serialized as HTML, some attributes are defined so that an attribute value (and an equals sign) is not required. Rather confusingly, they are the attributes declared as being “boolean attributes” (it’s confusing e.g. because the values true and false are not allowed, but the name partly reflects the principle that the corresponding DOM property, or “IDL attribute” as they call it, has the truth values true and false as the only permitted values). For such attributes, by definition, the value is even immaterial; only the presence of the attribute matters. For example, for the checked attribute, no value is used, but if a value is given, it must be either the empty string (checked="") or identical with the attribute name, case insensitively (e.g., checked=Checked). Any other value is nonconforming but is required to work, with the same meaning (e.g., checked=false means the same as checked).

Regarding the specific example, it is not valid in any version of HTML, since there is no attribute section declared.

like image 155
Jukka K. Korpela Avatar answered Sep 23 '22 13:09

Jukka K. Korpela


Boolean Attributes, Yes they are completely valid.

From W3C: (On SGML & HTML)

Some attributes play the role of boolean variables (e.g., the selected attribute for the OPTION element). Their appearance in the start tag of an element implies that the value of the attribute is "true". Their absence implies a value of "false".

Boolean attributes may legally take a single value: the name of the attribute itself (e.g., selected="selected").

This states that Boolean attributes are valid in HTML4 as well, but if you use something like, would be invalid.. because that boolean belongs to option tag.. Thanks to @Ronni Skansing for clarifying the doubt..

<p selected>Hello</p>

enter image description here


HTML5 Docs :

From W3C :

Empty Attribute Syntax

Certain attributes may be specified by providing just the attribute name, with no value.


From W3C: (HTML 5.1 Nightly )

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.


BUT

section is an invalid attribute, if you want to define your own attributes, HTML5 provides a way to do that.. you need to use data- prefix, for example, your section should be written as data-section, this way your attribute will be counted as valid.


If you hesitate to do so, we always have a validator to check - W3C Markup Validation Service

enter image description here

^ Validated As HTML5


NOTE: Though I provided data- is applicable for HTML5, using custom attributes in HTML4 is invalid, no matter even if you define data- before the attribute name, but, boolean attributes are valid in HTML4 as well.

like image 24
Mr. Alien Avatar answered Sep 25 '22 13:09

Mr. Alien