Can the HTML 'class' element attribute contain line breaks?

css, html

Solution

Per the HTML 4 spec, the `class` attribute is `CDATA`:

User agents should interpret attribute values as follows:

o Replace character entities with characters

o Ignore line feeds

o Replace each carriage return or tab with a single space.

so you're in good shape there.

The HTML5 spec describes a class as a set of space separated tokens, where a 'space' includes newlines.

So you should be good there, too.

Problem

Can the 'class' attribute of HTML5 elements contain line breaks? Is it allowable in the specs and do browsers support it? I ask because I have some code that dynamically inserts various classes into the element and this has created one very long line that is hard to manage. Normally I would build the class value using a variable but the CMS I'm using requires the template conditional tags to be positioned inline with the HTML. I can't use variables or PHP. What I found in my research is that some HTML tag attributes need to be a single line, but I haven't been able to discover if the class attribute is one of those. Does anyone know something about this?

Original source