0

I am curious as to how browsers interpret line breaks in HTML and how this translates into white space in the rendered markup. For example consider the following two divs:

<div> <select> <option>one</option> </select> <input /> </div> <div> <select> <option>one</option> </select><input /> </div> 

The first div will have more white space between the select and input elements because the line break between them is being rendered into white space; seen here. I am curious as to the reason behind this and what the best method to avoid this extra spacing while maintaining readable HTML is as I do not feel that the second div is very readable.

1
  • 5
    If at least one white-space character appears between two HTML elements in the HTML source code, the browser will create a TextNode between those two elements in the DOM. Commented Jul 12, 2013 at 15:12

1 Answer 1

1

Any whitespace in html, including 40 empty characters, will be interpretted as one ' ' character. So a line break and 500 spaces will still display as one space. The best way to avoid the spacing is with css:

select{float:left} 

http://jsfiddle.net/FSnhH/4/

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah floating the select would fix this, it just seems like a weird thing to have to do. I noticed it when I went to append a third div created in JavaScript and the rendered white space was different
You wouldn't have to float necessarily... just use css to format your rendered/generated html
How could CSS besides float deal with the fact that there is a rendered text node in between the elements?
@rpf3 - word-spacing and letter-spacing are sometimes used, See css-tricks.com/fighting-the-space-between-inline-block-elements for a run-down of the most common solutions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.