Why do line breaks between elements cause space between them?

Asked

Viewed 520 times

7

I was doing a few code organization here before commiting and realized that an indentation in HTML caused a problem in my layout, so I went to inspect to find out what I had done wrong besides indenting, and found that the line breaks for indentation ended up increasing the space between 2 elements.

Follow an example that demonstrates the same indented html and inline structure (also here):

<div style="font-size: 44px;">
  <span>28.3</span>
  <span>km</span>
</div>

<div style="font-size: 44px;"><span>28.3</span><span>km</span></div>

Questions:

  • Why does this happen?
  • How to indent my code and not have that space?
  • makes sense to me, because between the span tags, there is some element (line break and space) only spaces before or after the tag would be disregarded

  • @h3nr1ke, there is only the line break and the indentations there, but if you remove all the indents, it will still keep the space in bank, then it is the line break that causes the blank. And could you tell any source about the statement: "only spaces before or after the tag would be disregarded"?

  • I don’t have a source... it was the test I did on your fiddle... but the explanations below were very complete and full of references V

3 answers

6

According to the W3, all characters below are considered white space.

  • ASCII space (&#x0020;)
  • ASCII tab (&#x0009;)
  • ASCII form feed (&#x000C;)
  • Zero-width space (&#x200B;)

A line break is defined as &#x000D; (Carriage Return) and &#x000A; (Line Feed). And that also makes it a blank.

To ISO-8879 specifies that a line break immediately after a start mark should be ignored, as well as a line break immediately before the closing of a tag.

Note 1: Although there is a blank space + line break (which is also considered white space), the browser ends up condensing these characters. It’s the same as doing a b and the output being a b

Note 2: It is worth remembering that browsers interpret this way all HTML elements, except <pre> and elements with css white-space: pre.


References:
¹ https://www.w3.org/TR/REC-html40/struct/text.html#h-9.1
² https://www.w3.org/TR/REC-html40/struct/text.html#line-breaks

  • 0x000D I get the impression that it’s not the guy having null. As I recall, the null character is 0x0000

  • Truth @Jeffersonquesado. Corrected.

  • 1

    About the space of zero length, as it is something that I had never seen, I leave this reference to the curious reader: https://en.wikipedia.org/wiki/Zero-width_space?wprov=sfti1

2

In reality it is precisely because of the indentation that the spacing occurs, because if one observes well a space is being generated, the rendering engine of html browsers ignores the additional spaces (when there is more than 1 space), but still the first of them is rendered, the ideal when you do not want the space is to be placed in the same line:

<div style="font-size: 44px;">
  <span>28.3</span><span>km</span>
</div>

This eliminates the problem.

  • 1

    It is not because of indentation, it is the same line break. If you leave the second span at the beginning of the second line, will have line break and in this case there are no spaces.

  • Actually what you said @LINQ, the problem is also the indentation, because there are spaces in it. But the line break as well. Note that the render engine interprets some of the break characters - n or r- (I don’t know which OS it is in), but renders as space as well. In this fiddle you have an example with all cases:https://jsfiddle.net/L287m1k4/

  • Well, taking into account @LINQ’s statement, which is correct, it’s not the space but the line break that is causing the space, and then how does it render a space if there is none? Or he converts, as the rule goes?

  • In reality @Valdeir Psr’s answer is fine but complete that mine, is that some special characters are also rendered as white space

1

Why does this happen?

This difference happens because, when displaying HTML, the browsers condense a sequence of blank characters (i.e. spaces, line breaks, etc) into a single space. This behavior is standard for any element that does not own the property white-space defined as pre or pre-wrap.

In fact, that’s why, if you want to display more than one space, you can’t just use the space characters ( ) in the markup, since, as we have seen, these are condensed. In this case, you can use several HTML entities &nbsp; (non-breaking space). Behold:

body {
  font-family: monospace;
}
A&nbsp;&nbsp;&nbsp;&nbsp;B <br />
|----|

How to indent my code and not have that space?

To remove spaces between spans, you should find some way to remove (or hide) the blank characters that exist between them in HTML (the line breaks and indentation spaces).

One way to do this is simply not to indent them, as done in the first example:

<div>
  <span>1</span><span>2</span><span>3</span>
</div>

Another alternative is to use comments, as shown this answer soen:

<div>
     <span>1</span><!--
  --><span>2</span><!--
  --><span>3</span>
</div>

Note that now all blank characters (in the example, line breaks and spaces) are now part of the comment, not the markup that will be displayed by browser.

If you can’t handle HTML, another option, using CSS, is to reduce the font size to zero for the <div> (which contains the spaces) and set the normal size for each <span>. Something like that:

.without-spacing {
  font-size: 0;
}

.without-spacing span {
  font-size: initial;
}
<div>
  <span>1</span>
  <span>2</span>
  <span>3</span>
</div>

<div class="without-spacing">
  <span>1</span>
  <span>2</span>
  <span>3</span>
</div>

Note that, in this last example, we are not actually removing the blank characters, but hiding them with this little ruse. If you select them and "copy", you will see that there is still a space between each element. Therefore, use this alternative for visual purposes only.

  • To those who negatively: please explain. The comment of feedback is important so that I can improve the answer. I would never ignore a comment from feedback.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.