views:

114

answers:

5

Greetings,

I'm doing some template work on a personal project and I'm trying very hard to keep my lines under 80 characters wide. This is difficult to do with HTML, especially when adding code for dynamic content as well. There are a lot of cases where it would be helpful to put line breaks inside the elements themselves, between attributes. Here's an example:

<a href="http://example.com"&gt;foobar&lt;/a&gt;

Basically, I want to be able to do this:

<a
href="http://example.com"&gt;foobar&lt;/a&gt;

without any ill effect. It seems to validate and Firefox doesn't mind, but I'd like something approaching an authoritative answer. I've tried to find the answer in the HTML specs to no avail. Google is no help either, so I thought I'd ask the knowledgeable folk.

Thanks for any insight.

+2  A: 

Any white space is allowed between attribute definitions including spaces, tabs and newlines.

(Thanks Matt) Beware that:

<b>Some text</b>

is not equivalent to:

<b>
Some text
</b>

It is however equivalent to:

<b> Some text </b>

meaning those newlines are spaces.

Within an a tag definition between attributes however they are fine.

cletus
Beware of newline gremlins:<a href="http://example.com">\nfoobar</a>The newline creates a space, which can create artifacts when styling.
Matt Olenik
A: 

This is valid. as far as you don't break the attributes themselves. For example:

This is valid:

<a
href="http://example.com"
>
foobar
</a>

This is not:

<a href="http://exampl
e.com">foobar</a>
Mendy
A: 

In the code you can do anythig with white spaces. However with some browsers if you wrote someting like:

<a href="http://example.com"&gt;foobar
</a>

The new line can have a (little) incidenx on how your link is displayed, especially on 'active ' status.

gregseth
+1  A: 

If you use it outside the tags then you will get a single whitespace for all whitespace characters in a row due to the collapsing effect.

http://library.stanford.edu/tools/tutorials/html2.0/whitespace.html

In general, a single whitespace character--including newlines--or a sequence of whitespace characters are treated as a single space and leading/trailing whitespace is eliminated. This is known as 'collapsing whitespace'.

Tahir Akhtar
A: 

You can do this without any problems. What can cause problems is to place whitespace between the opening and closing tags:

<a href="http://example.com"&gt;
foobar</a>

This will cause an extra line or extra spacing in some browsers.

Frode N. Rosand