It's because the browser has no concept of linebreaks or tabs outside of special situations like the <pre>
tag so wherever it finds them it converts them to whitespace. Keep in mind it will ignore all whitespaces, line breaks and tabs except the first one. You could have 30 consecutive line breaks and 100 spaces in your code, but it will render as 1 space in the browser.
Even code that only has a line break but no spaces or indents will still show a space when rendered.
An example: code with a line break but no space:
<button>text1</button>
<button>text2</button>
It will still render 1 whitespace character between them because of the line break. You can verify this in the fiddle.
Typically any sort of formatting like this is handled by CSS.
MDN's explanation is about as good as any. The actual spec.