Spaces are compacted in HTML because there’s a distinction between how HTML is formatted and how it should be rendered. Consider a page like this:
<html>
<body>
<a href="https://stackoverflow.com/questions/433493/mylink">A link</a>
</body>
</html>
If the HTML was indented using spaces for example, the link would be preceded by several spaces.