Jitendra Vyas
Jitendra Vyas

Reputation: 152627

Does it ever matter , Whitespace between HTML elements?

Does it matter ever , Whitespace between HTML elements in source? when we give style through CSS? and need cross browser compatibility

For any browser?

Upvotes: 8

Views: 2730

Answers (8)

Andy
Andy

Reputation: 1421

Text areas are also affected with whitespace between opening and closing tags as it assumes any content between the two are its content that it should show....

Upvotes: 2

Donal Fellows
Donal Fellows

Reputation: 137567

It depends on the content model of the element containing the whitespace. If the model is text-derived, including when it is mixed elements and text, then the whitespace matters (though multiple whitespace characters are usually collapsed into one and leading/trailing space removed entirely, with the exception of inside a <pre>). If the model only admits element content, then whitespace has no significance at all; e.g., whitespace between a <ul> and its <li>s is supposed to be wholly unimportant.

Upvotes: 0

Graham Clark
Graham Clark

Reputation: 12966

Whitespace does matter, but all whitespace is treated as one space. For example,

<span>hello</span> <span>there</span>

Will be rendered by a browser exactly the same as

<span>hello</span>         <span>there</span>

Unless a <pre> tag is being used.

Upvotes: 5

fmark
fmark

Reputation: 58547

As pointed out by Douglas and David Dorward, white space does matter.

However, in HTML blocks (i.e. not javascript or other embedded content-types), all consecutive white space are equivalent to a single white space. That is, hello <b>world</b> is equivalent to

hello   
            <b>world</b>

The exception to this rule is within <pre>..</pre> blocks, which are white space sensitive by specification.

Upvotes: 0

David Yell
David Yell

Reputation: 11855

ie6 used to put gaps inbetween some tags when rendered. It also matters when Office 2009 renders it's html emails using word. If you have linespaces it can put in 2px gaps.

Upvotes: 1

Mike Chess
Mike Chess

Reputation: 2798

Depending on the amount of whitespace within the html, css, or other files, it could have an impact on how long it takes to download to the user's system.

Upvotes: 1

Quentin
Quentin

Reputation: 943150

Yes, for example: pretty much any time the data is inline.

Compare:

<p>H<b>e</b>llo, world</p>

and

<p>H <b>e</b> llo, world</p>

Upvotes: 9

Douglas
Douglas

Reputation: 37763

Compare these two lines in a browser:

<img src="..." /> <img src="..." />
<img src="..." /><img src="..." />

You'll see that there is a space between the images in the first line, but not the pair in the second.

Upvotes: 2

Related Questions