Are attributes without a value permitted in HTML4?

I wonder if HTML 4 allows attributes without a value, equivalent to attributes with a null value. For instance:

<h2 section>foobar</h2> 

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

Are these two fragments equally valid? If not, are they valid in HTML version 5?

thanks!

+6
source share
3 answers

Logical attributes, yes, they are fully valid.

From W3C: (in SGML and HTML)

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

Logical attributes can legally take one value: the name of the attribute (for example, selected="selected" ).

This indicates that the boolean attributes are valid in HTML4 too, but if you use something like that, it would be invalid .. because this boolean belongs to the OPTION tag. Thanks to @Ronni Skansing for clarifying doubts.

 <p selected>Hello</p> 

enter image description here


HTML5 docs

From W3C :

Empty attribution syntax

Some attributes can be specified by providing only the attribute name, without value.


From the W3C: (HTML 5.1 Nightly)

Several attributes are logical attributes. The presence of a boolean attribute for an element represents a true value, and the absence of an attribute represents a false value.


BUT

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


If you are hesitant to do this, we always have a validator for verification - W3C Markup Validation Service

enter image description here

^ Verified as HTML5


NOTE Although I pointed out that data- applicable to HTML5, using custom attributes in HTML4 is not valid, even if you specify data- in front of the attribute name, but logical attributes are valid in HTML4 as well.

+10
source

As formally defined, HTML 4 does not allow attributes without a value. What is usually regarded as an attribute without a value, as in <input checked> , is formally the value of an attribute without an attribute name (and an equal sign). Although they are mistakenly described as “logical attributes” with special minimization rules in the HTML 4 specifications, these specifications normatively refer to the SGML standard.

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

In practice, part of the attribute minimization rules supported by browsers consists of only special cases when an attribute is declared as allowing only one keyword value, for example, the checked attribute, which is formally declared using p>

  <!ATTLIST INPUT checked (checked) #IMPLIED> 

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

But that means that the minimized checked attribute means checked=checked . The value is not empty, but the checked keyword. Browsers, on the other hand, consider attributes such as “presence attributes”: it matters whether the element has this attribute or not, and not its value.

In HTML5, serialized as XHTML (i.e., as XML), everything is simple: each attribute specification must be of the form name = "value" or name = 'value', so an equal sign is required, and therefore, quotation marks; logically, the value is always present, although it may be an empty string, as in alt="" .

In HTML5, serialized as HTML, some attributes are defined so that the attribute value (and the equal sign) is not required. Rather confusing, they are attributes declared as “logical attributes” (they are confusing, for example, because true and false not allowed, but this name partially reflects the principle that the corresponding DOM property or IDL attribute “as they call it, have true and false values ​​as the only valid values.) For such attributes, by definition, the value is not even significant; only the presence of the attribute matters. For example, for the checked attribute, the value is not used, but if the value is set, it must be either an empty string ( checked="" ), or identical to the attribute name if the case is case insensitive (for example, checked=checked ), Any other value is inappropriate, but should work with the same value (for example, checked=false means the same, as checked ).

For a specific example, it is not valid in any version of HTML since the section attribute is not declared.

+3
source

Both fragments are syntactically valid in html4 and html5. The first is not xhtml because xhtml requires an attribute value.

A section, on the other hand, is not a specific attribute, but is a valid tag in html5. Therefore, your code is invalid.

0
source

All Articles