Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason not to strip whitespace in HTML

Tags:

html

The question is pretty self explanatory. Why shouldn't I strip it? It seems to me that most of the whitespace is used purely for formatting in the text editor and has no impact on the final page.

What's more, when these random nodes of whitespace do have an impact on the final page, it is usually an impact I do not want, such as a mysterious one character (after whitespace collapse) gap between inline-blocks.

I can strip all these whitespace text nodes pretty easily. Is there any reason I shouldn't?

edit:

It's mainly for the strange behaviour where whitespace, rather than for performance. One example is me wanting to put images side by side using inline-block instead of float, while preventing wrapping to next line and allowing them to spill out of the parent.

The whitespace causes these mysterious gaps, which can be removed by basically minifying the HTML source code to remove the whitespace between inline-blocks manually (and completely messing up your source code formatting in the process).

like image 869
Li Haoyi Avatar asked Dec 09 '22 06:12

Li Haoyi


2 Answers

There's no reason not to, really. It can be done very easily with something like htmlcompressor.

However, assuming you're delivering all your html, css, and js files via gzip, then the amount of real-world bandwidth savings you'll see from stripping whitespace will be very small. The question then becomes, is it worth the trouble?

UPDATE:

Perhaps this will affect your decision. I performed a simple minification on a page of my website just to see what kind of difference it would make. Here are the results:

BEFORE minification

  • 22232 bytes (uncompressed)
  • 5276 bytes (gzip)

AFTER minification

  • 19207 bytes (uncompressed)
  • 5146 bytes (gzip) - 130 bytes saved

The uncompressed file is about 3 KB smaller after minification. But that's not really what matters. The gzip compressed file is what is sent over the wire. And you can clearly see that gzip does a pretty good job even with the non-minified HTML.

I see the benefit of minifying js libraries, or things that aren't changing constantly. But I don't think it's worth the trouble doing this to your HTML for a measly 130 bytes.

like image 156
Steve Wortham Avatar answered Dec 26 '22 17:12

Steve Wortham


Let me give one reason why you shouldn't minify html:

How html eventually gets rendered is strongly tie to the CSS applied up on it, but the minifiers usually work without expecting the influence of CSS. All minifiers you can get out there at the time of writing, they remove the spaces in html based on certain assumptions of your coding and CSS styling, if you don't code it the way they expected, the minified rendering result in browser will be different from before minification.

For example, some of minifiers assume the space between "block elements" (such as <div/>, <p/>) can be removed, this is usually true, because spaces between them has no effect on rendering the final result. But what if in the CSS you set "display: inline" or "inline-block" for elements whose default display property is block?

Will below html snippet still rendering as it should be if you remove the spaces between <div/>s ?

<div style="display: inline">will</div> <div style="display: inline">this</div> <div style="display: inline">still</div> <div style="display: inline">work?</div>

You may argue that, we can reserve at least 1 space, and remove remaining consecutive spaces and that still save a lot bytes. Then how about <pre> tag and white-space: pre?

Try copy the html code snippet from below url and paste into your minifier, see if it produces result as before the minification:

https://jsfiddle.net/normanzb/58rpazL2/

like image 27
Norman Xu Avatar answered Dec 26 '22 17:12

Norman Xu