Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an empty class attribute valid HTML?

Tags:

Is an empty class attribute valid HTML in the following formats:

<p class="">something</p> <p class>something</p> 

I found this question which is similar, but asks specifically about custom data attributes.

like image 805
Thorkil Holm-Jacobsen Avatar asked Jun 10 '15 06:06

Thorkil Holm-Jacobsen


People also ask

What is empty class in HTML?

The :empty CSS pseudo-class represents any element that has no children. Children can be either element nodes or text (including whitespace). Comments, processing instructions, and CSS content do not affect whether an element is considered empty.

What is empty attribute in HTML?

When an HTML tag contains an attribute with no value set (indicated by no information between the quotation marks) it is said to have an "empty" attribute.

Is an empty string valid HTML?

No, it doesn't. It adds the element to the DOM, not to the HTML. When you look at the DOM, using your browser's developer tools, it will displayed using HTML-like syntax. In this syntax, a value attribute where the value is an empty string will be rendered without the ="" portion.

Can you have two classes in HTML?

HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to separate them.


2 Answers

After looking at the specifications referred to in the other answers, I have found the sections that actually do answer the raised question.

<p class> is not allowed

The specification on attributes section 3.2.3.1 on Empty Attribute Syntax states the following:

An empty attribute is one where the value has been omitted. This is a syntactic shorthand for specifying the attribute with an empty value, and is commonly used for boolean attributes. This syntax may be used in the HTML syntax, but not in the XHTML syntax.

(...)

This syntax is permitted only for boolean attributes.

Seeing that the description of the class attribute (obviously) does not mention it being a boolean attribute, omitting the value is not permitted.

<p class=""> is allowed

From the section on class we learn that:

Every HTML element may have a class attribute specified.

The attribute, if specified, must have a value that is a set of space-separated tokens representing the various classes that the element belongs to.

and from the definition of space-seperated tokens:

A set of space-separated tokens is a string containing zero or more words (known as tokens) separated by one or more space characters, where words consist of any string of one or more characters, none of which are space characters.

we can conclude that the attribute value can in fact be empty (i.e. containing zero tokens).

like image 101
Thorkil Holm-Jacobsen Avatar answered Sep 17 '22 12:09

Thorkil Holm-Jacobsen


From the HTML5 Reference page, section 3.2.3 Attributes:

Elements may have attributes that are used to specify additional information about them. Some attributes are defined globally and can be used on any HTML element, while others are defined for specific elements only. Every attribute must have an attribute name that is used to identify it. Every attribute also has an associated attribute value, which, depending on the attribute's definition, may represent one of several different types. The permitted syntax for each attribute depends on the given value.

So to answer your question,

Invalid:

<p class> 

Valid (empty value)

<p class=""> 

See http://dev.w3.org/html5/html-author/ For all the reference regarding HTML5 you need.

like image 31
Georgios Dimitriadis Avatar answered Sep 18 '22 12:09

Georgios Dimitriadis