26

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.

Community
  • 1
  • 1
Thorkil Holm-Jacobsen
  • 7,287
  • 5
  • 30
  • 43

4 Answers4

27

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).

Community
  • 1
  • 1
Thorkil Holm-Jacobsen
  • 7,287
  • 5
  • 30
  • 43
  • The link to the "specification on attributes" is to http://dev.w3.org/html5/html-author/, a five year old editor's draft. It's basically "what some guy wrote" and of no formal standing. The syntax *may* have been invalid in 2010 for non-boolean attributes, but it isn't so today. – Alohci Jun 10 '15 at 09:42
  • I did not realize it was a draft. If you can find the relevant parts of the specification, please post an answer. – Thorkil Holm-Jacobsen Jun 10 '15 at 10:23
  • The relevant bit of the spec is at http://www.w3.org/TR/html5/syntax.html#attributes-0, but it doesn't say that empty attribute syntax is allowed for non-boolean attributes, it just doesn't disallow it. – Alohci Jun 10 '15 at 10:30
2

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.

  • 1
    You reference a general section on attributes and conclude that `

    ` is invalid. However, surely some attributes do not need an explicit value (`) so how do you draw this conclusion? Is there something i am missing here? Are boolean attributes not covered by your referenced section?

    – Thorkil Holm-Jacobsen Jun 10 '15 at 06:51
  • You're right. Some attributes do have special cases like "disabled". That doesn't apply to 'class' attribute. – Georgios Dimitriadis Jun 10 '15 at 08:23
0

Not having any values won't make it invalid. I have tested it in http://validator.w3.org/#validate_by_input

Put this code there and test:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div class>Validiity
       <input type="text" disabled>
    </div>
</body>
</html>

Without quotes, just attribute names are drafted for boolean attribute like disabled, required

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.

More here: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes

Read this Q/A on boolean attribute discussion - What does it mean in HTML 5 when an attribute is a boolean attribute?

user3840170
  • 26,597
  • 4
  • 30
  • 62
absqueued
  • 3,013
  • 3
  • 21
  • 43
-1

Class attribute should contain a value. without value its not a valid one. but it shows no impact while rendering.

Srinivas
  • 133
  • 6