Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 206
Luke Franklin Avatar asked Feb 18 '13 02:02

Luke Franklin


People also ask

Which attribute is used to break a line?

The <BR> element is used to force a line break.

What is the HTML tag for a line break?

The <br> HTML element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division of lines is significant.

How do you not break a line in HTML?

In this code, we have used the display attribute and set it to inline which will change the default behavior of paragraph tag and make it behave like an inline element. By using the inline property we can avoid the new line.

How do you break a line in HTML without br?

Use block-level elements to break the line without using <br> tag.


2 Answers

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.

like image 173
Dancrumb Avatar answered Sep 18 '22 21:09

Dancrumb


Can the [class] attribute of HTML5 elements contain line breaks?

Yes. The HTML5 spec says:

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.

The link proceeds to say:

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.

And space characters include:

  • space (' ')
  • tab (\t)
  • line feed (\n)
  • form feed (\f)
  • carriage return (\r)

The space characters, for the purposes of this specification, are U+0020 SPACE, "tab" (U+0009), "LF" (U+000A), "FF" (U+000C), and "CR" (U+000D).

Newlines as you would add to UTF-8 documents are:

  • line feeds (\n)
  • carriage returns (\r)
  • a carriage return followed immediately by a line feed (\r\n)
like image 45
zzzzBov Avatar answered Sep 22 '22 21:09

zzzzBov