Is it safe to minify HTML?


Like JS and CSS, HTML can also be "minified":

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src=""></script><scriptsrc=""></script>
    <h1>Hello, world!</h1>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src=""></script><!--Includeallcompiledplugins(below),orincludeindividualfilesasneeded--><scriptsrc="js/bootstrap.min.js"></script>


<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Bootstrap 101 Template</title><link href="css/bootstrap.min.css" rel="stylesheet"><!--[if lt IE 9]><script src=""></script><scriptsrc=""></script><![endif]--></head><body><h1>Hello, world!</h1><script src=""></script><scriptsrc="js/bootstrap.min.js"></script></body></html>

HTML example taken from here: link

Minimized here: link

This reduces the size a bit and should decrease the download time.

My question is: Is it safe? Or is there a risk of the browser interpreting something wrong? (mainly browsers as IE

asked by anonymous 10.07.2014 / 18:47

2 answers


Whitespace may have meaning in a CSS-dependent way, for example when using white-space: pre .

Therefore, an HTML minifier, which does not take into account the CSS styles applied to it, will be insecure by definition.

Obviously, you can assume some rules, which are probably never cheated. The head tag, for example, will never be visible ... soon the spaces can be removed.

Comments, could be removed if it were not for these conditional comments. But generally, these comments have a pattern, and you can tell when to remove them or not.

10.07.2014 / 19:00

The answer I'm giving does not exactly answer your question but I believe it includes valid knowledge for it .

HTML is an XML in itself and for this reason it can be larger than a JSON for example. The emerging innovations are all focused on JSON and not XML, so there are emerging technologies in JS to render an HTML page using JSON. This way, JSON is trafficked and these frameworks make the parse and the browser renders normal HTML5. I can cite 2, "AngularJS from Google" and Sencha's ExtJS .

The gain in size and end of pages and speed is brutal using these frameworks. If you are a web developer, I strongly advise you to study these technologies.

Good studies.

10.07.2014 / 19:34