Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean in HTML 5 when an attribute is a boolean attribute?

Tags:

html

What does it mean when an attribute like the hidden attribute is a boolean attribute? Can someone explain this in layman's terms?

like image 536
HELP Avatar asked Nov 09 '10 23:11

HELP


People also ask

What is a boolean attribute in HTML?

A Boolean attribute is an attribute that can only be true or false. How does a Boolean attribute work? According to the HTML specification: The presence of a boolean attribute on an element represents the “true” value, and the absence of the attribute represents the “false” value.

How do you add a boolean attribute in HTML?

To add a Boolean attribute: node. setAttribute(attributeName, ''); // example: document. body.

Which of the following attributes is not a boolean attribute?

Correct Option: C Muted is Boolean attribute whose value is set as false by default. Buffered is not boolean attribute in <audio> element.


2 Answers

2.5.2 Boolean attributes

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.

The values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

like image 28
Amir Raminfar Avatar answered Sep 21 '22 05:09

Amir Raminfar


As already stated boolean attributes are attributes that are evaluated either true or false.

However, from HTML5 Spec - http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes

2.5.2 Boolean attributes

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.

The values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

Note that this means that <div hidden="true"> is not allowed in HTML5.

Correct would be either <div hidden> or <div hidden=""> or <div hidden="hidden"> or case-insensitive and single quotes/unquoted variations of any of them.

like image 50
Alohci Avatar answered Sep 25 '22 05:09

Alohci