Which spaces in HTML can go: minifying a menu and a paragraph
Take a short piece of HTML. It starts with a comment, <!-- nav -->. Then comes a list with the class "menu" and two items. The first item is a link to the home page. The second is a link that says "Tea", then the word "and", then "cake" in bold. After the list is a paragraph that says "Open daily from 9.", written over three lines with extra spaces, and with "daily" in italics. Last is a pre element holding two indented lines. With its line breaks and indents, the snippet is 188 bytes.
The comment goes first, because the browser never shows it. Then the minifier looks at each run of whitespace and asks one question: is it next to a block element? The line break between the comment and the ul, the indents before each li, and the line break before the closing tag of the list are all next to blocks. A browser ignores whitespace there, so the minifier removes it. The line break after the opening p tag and the one before its closing tag go for the same reason.
Other spaces stay. Between the "Tea" link and the word "and" there is a space, and it shows on the page. Remove it and the reader sees "Teaand". The minifier keeps it. Inside the paragraph, "Open daily" has three spaces, and a line break and an indent sit before "from". A browser shows any run of whitespace in normal text as one space, so the minifier writes one space in each place, and the page looks the same. The pre element is copied exactly, indents and line break included, because a pre shows every space. The result is 158 bytes, about 16% smaller, and the page looks the same.
The hard part of an HTML minifier is not removing spaces. It is knowing which ones are safe to remove. A minifier that removes all whitespace between tags would join "Tea" and "and". One that collapses every run of whitespace would break a pre block or the text in a textarea. This tool knows which HTML elements are blocks and which are inline, and it treats pre, textarea, script and style as special. That is why the output can be trusted without checking every line.
There are limits. The minifier cannot see your CSS, so if you set white-space: pre on a div, it will still collapse the spaces inside that div. It does not minify inline JavaScript, only trims it, and it does not remove optional closing tags or attribute quotes. It also does not check your HTML: an unclosed tag stays unclosed. And if your server already compresses pages with gzip or Brotli, the saving on the wire is smaller than the saving on disk shown here.