HTML minification?

Viewed 80748

Is there a online tool that we can input the HTML source of a page into and will minify the code?

I would do that for aspx files as it is not a good idea to make the webserver gzip them...

8 Answers

Don't do this. Or rather, if you insist on it, do it after any more significant site optimizations are complete. Chances are very high that the cost/benefit for this effort is negligible, especially if you were planning to manually use online tools to deal with each page.

Use YSlow or Page Speed to determine what you really need to do to optimize your pages. My guess is that reducing bytes of HTML will not be your site's biggest problem. It's much more likely that compression, cache management, image optimization, etc will make a bigger difference to the performance of your site overall. Those tools will show you what the biggest problems are -- if you've dealt with them all and still find that HTML minification makes a significant difference, go for it.

(If you're sure you want to go for it, and you use Apache httpd, you might consider using mod_pagespeed and turning on some of the options to reduce whitespace, etc., but be aware of the risks.)

This worked for me:

http://minify.googlecode.com/git/min/lib/Minify/HTML.php

It's not an already available online tool, but being a simple PHP include it's easy enough you can just run it yourself.

I would not save compressed files though, do this dynamically if you really have to, and it's always a better idea to enable Gzip server compression. I don't know how involved that is in IIS/.Net, but in PHP it's as trivial as adding one line to the global include file

CodeProject has a published sample project (http://www.codeproject.com/KB/aspnet/AspNetOptimizer.aspx?fid=1528916&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2794900) to handle some of the following situations...

  • Combining ScriptResource.axd calls into a single call
  • Compress all client side scripts based on the browser capability including gzip/deflate
  • A ScriptMinifier to remove comments, indentations, and line breaks.
  • An HTML compressor to compress all html markup based on the browser capability including gzip/deflate.
  • And - most importantly - an HTML Minifier to write complete html into single line and minify it at possible level (under construction).
Related