27

Wikipedia defines minification as...

[...] the process of removing all unnecessary characters from source code without changing its functionality. These unnecessary characters usually include white space characters, new line characters, comments, and sometimes block delimiters, which are used to add readability to the code but are not required for it to execute.

I'm currently doing this to my HTML, CSS and Javascript in order to save bandwidth, but someone told me that he remembers a browser misbehaving when there's no white space between certain tags (namely ul and li items). Is this true?

Are there any notable browsers, proxies, or other user agents that misbehave when dealing with minified code?

Other than losing readability when viewing the source, is there any other downside to minification?

JohnCand
  • 1,197
  • 2
  • 12
  • 19
  • 3
    *"but someone told me that he remembers a browser misbehaving when there's no white space between certain tags"* Maybe you can ask that person for more information. I believe the person is wrong though. – Felix Kling Apr 25 '14 at 12:29
  • 8
    There is no standard which states that HTML, CSS or JavaScript must be spaced out. If a browser does render this incorrectly then it is likely a bug. The only issue you may face is when handling `inline` or `inline-block` elements, as these elements take spaces into account. – James Donnelly Apr 25 '14 at 12:29
  • Minifying your CSS and javascript is fine and only presents issues in rare scenario's. (Such as the dependency injection mechanism in AngularJs where it inspects variable names) HTML is more tricky though, so I would be careful with this. – Moeri Apr 25 '14 at 12:29
  • It is not bad with css and JavaScript. But I haven't tried with html yet. What I would say is that You don't need to worry about spaces in html, since the browser skips them. – Bellash Apr 25 '14 at 12:29
  • 2
    To the anonymous trolls who vote to close this perfectly fine question because "it is too opinion based", could you **please** explain where exactly the answer to this question depends on one's opinion? – JohnCand Apr 25 '14 at 12:31
  • 1
    _Is minifying your HTML, CSS, and Javascript a bad idea?_ Somebody will tell you _no, the standard allows it, so go fo it, if it does not work its a bug in the browser_; somebody else will tell you _yes, you should workaround all bugs in all browsers_; most people would just think _it depends_ or start suggesting the favorite minification tool. – guido Apr 25 '14 at 12:42
  • @JohnCand as one of the "anonymous trolls", I voted to close this question purely because there are far too many different answers which could be given - and the majority of which could just be given as comments. It's a good question, but it doesn't really fit in with StackOverflow's Q&A flow. – James Donnelly Apr 25 '14 at 12:48
  • 3
    @guido That line of argument doesn't make *any* sense. By that logic, you could close any question on SO because *some* users might stray off into the woods and provide an opinion-based answer where you really should provide facts. There are two simple questions: Do browsers misbehave and is there a downside to minification? As you can see from the already provided answer, it's *not at all* dependent on one's own opinion, but facts. – JohnCand Apr 25 '14 at 12:49
  • The "is it bad?" wording was merely chosen because I wanted a short title that "represents" the overall tone of the two questions contained within. – JohnCand Apr 25 '14 at 12:52
  • Why would you minify your HTML? thats nonsense – Adrian Salazar Apr 25 '14 at 13:21
  • @AdrianSalazar Well, there's this weird, obscure site- [google.com](https://google.com)- which does it. But those guys are notoriously incompetent. Personally, I maxify my HTML. If it's less than 30 MB per file, I'm not really trying. – Parthian Shot Apr 24 '15 at 23:15
  • @ParthianShot wow that's a nice showing off of sheep-like consciousness... just because Google does! way to go... what's next, should I bring goats to my home to cut my grass and wear google glasses on a date? oh yeah, darling I thing we should now kiss, because I googled it and that's what people normally does... – Adrian Salazar Apr 26 '15 at 07:15
  • @AdrianSalazar You do get that there's a difference between saying "you should do it because everyone's doing it" and "you should do it because a company universally recognized for its efficient and expert use of the web has adopted it as a standard"? I'm not saying "everyone who uses PHP does it", I'm saying "google does it". They're a company with a bottom line, and they invested a decent amount of money and time into something, which they continue to use. If it were useless or harmful to their bottom line, they'd stop. The way they stopped selling google glass, as a matter of fact. – Parthian Shot Apr 26 '15 at 16:57
  • And I still don't get to the point here... This is supposed to be answered with a technical fact, not a fanboy argument... – Adrian Salazar Apr 26 '15 at 17:02
  • @JohnCand At first, I agreed with your comment about people voting to close this question, because it is actually a very good question. BUt then I started to realise, and your answers back up my realisation, that there will never really be a solid answer to this question that will be marked, as all answers will depend on a person's opinion. I think you have actually asked a question that is too opinion-based, (within the scope of this site at least anyway). This question has been here for over 2 years, and you haven't accepted an answer... is my evidence. – Lee Sep 01 '16 at 13:40

6 Answers6

17

someone told me that he remembers a browser misbehaving when there's no white space between certain tags (namely ul and li items). Is this true?

Yes, in certain circumstances, like if the elements are set to display inline-block or inline.

The following two lists will render differently, because of the whitespace between the <li> elements:

html

<ul>
    <li>simple</li>
    <li>list</li>
</ul>

<ul><li>minified</li><li>list</li></ul>

css

li {
    display: inline-block;
    background: blue;
    color: white;
}

rendered output

enter image description here

http://jsfiddle.net/Uwv3e/

xec
  • 17,349
  • 3
  • 46
  • 54
  • 10
    Technically this would be the minifying script's bug, as by definition minifying is _"the process of removing all unnecessary characters from source code **without changing its functionality**"_. So a good minifier would be expected to minify that to `
    • minified
    • list
    ` or similar instead.
    – Etheryte Apr 25 '14 at 13:08
  • 2
    @Nit Yes, absolutely. Personally i wouldn't trust a HTML minifier to know when to keep spaces and when not to, and all just to shave off a few bytes. I do minify my JS and CSS though. – xec Apr 25 '14 at 13:12
  • Remember also that the minified will remove all "unnecessary" comments in the code... wait... so my knockout bindings using virtual elements will be gone, and probably some conditional IE9- comments as well... hmm... seriously, knowing that all of my HTML is not minified is somehow something that won't keep myself from sleeping good every night... – Adrian Salazar Apr 27 '15 at 09:08
16

Minification removes maintainability for the sake of... usually about 4-8kb of savings on a site size. You can get more savings by compressing a single jpg on the site and removing the image's meta-data.

Unless you're building a website that has a MASSIVE amount of pages and subpages and templates and over 5,000 lines of CSS and JS, you're going to find minification is a waste of effort, especially when maintenance comes to play and you have to keep unminified versions of files floating around just to do fixes, minify, overwrite the minified files with the new version, pray the next guy that maintains the site uses your same workflow and doesn't make changes to the minfiied CSS file, then when you come back in and wipe out his changes...

I bring this up because I've seen this happen. I've done GTmetrics and Pingdom scores on sites pre and post minification and the score and load speed barely changes enough to make it worth it.

I've always called minification "Spending dollars to save pennies". Your efforts can be better spent elsewhere on a dev project.

Ben
  • 283
  • 2
  • 10
  • 4
    You describe a bad process for minification. Usually a good setup would use something like gulp or grunt to minify files from one directory (or prefixed filename) to another. You would usually keep your unminified files out of the web root, and move them there after minification. – Daniel Dewhurst May 25 '17 at 14:21
  • 5
    Introducing yet another dependency like Gulp or Grunt, purely for the sake of minifying files, does not make things simpler. It's simplest just to commit the files and be done with it. The added benefit is that you can then view the source code on the live site and diagnose problems there, which is nice if there is a hidden difference between the dev and live sites that you are unaware of. Plus, if the developers who originally implemented the workflow are replaced, the new ones don't have to deal with Gulp or Grunt to get their new workflow running. – kloddant May 02 '19 at 15:46
  • +1 @kloddant. I inherited a project with the gulp/grunt dependency and it is a pain because those tools are dog slow (when building inside Visual Studio). And what Ben says about workflow in his answer is important: in one project I had a plugin doing the minification. When I upgraded the IDE I didn't install that plugin so the CSS changes I was making were not being minified...end result, discrepancy between dev/prod sites that was driving me nuts. It certainly is "spending dollars to save pennies" and just ain't worth it IMO. – Eric Mutta Nov 05 '21 at 17:23
10

Is minifying your HTML, CSS, and Javascript a bad idea? Yes of course!

A good idea is, however, to minify your CSS, and Javascript

Why?

  • Minification works by (a) replacing human-readable variable names for compact names, like "mySuperTollesFunctionCall()" to "m()" and by (b) Removing Whitespaces
  • Your JavaScript you can benefit from both (a) and (b)
  • Your CSS can only benefit from (b)
  • Your HTML code, in theory could benefit only from (b) but that is just not worth the try.

Any simple good HTML editor can nice-format your HTML file for you. Take care of whitespaces inside "pre" blocks, gives you hint on W3C compliance, etc, etc, etc.

If you're worried about the size of your HTML file... You should enable GZIP compression of static files in your web server, most living fossils will handle it, and your customers will appreciate it.

http://www.schroepl.net/projekte/mod_gzip/browser.htm

Adrian Salazar
  • 5,279
  • 34
  • 51
  • +1 For all the absolutely true details. But if asker looks for maximum performance, he absolutely can minify html then gzip it (then cache the result is applicable) – ngasull Apr 25 '14 at 14:13
  • Google would argue your point. Minifying HTML does not mean it becomes unfunctional. It removes whitespace, it's now a metric for page speed. – Kyias Oct 16 '19 at 13:56
4

Some browsers IE versions have problem with minified font-face declarations, see:

Community
  • 1
  • 1
leo
  • 8,106
  • 7
  • 48
  • 80
1

someone told me that he remembers a browser misbehaving when there's no white space between certain tags (namely ul and li items). Is this true?

No that statement is false. This is how browsers should work according to the HTML specification regarding whitespace. Any sequence of whitespace (tabs, newlines, spaces) means the same thing.

Take the following markup:

<ul>
    <li>A</li>
    <li>B</li>
</ul>

The correct minification of this after also taking into account SGML line break rules stating that the whitespace after the opening tag and before the closing tag can be ignored, then the correct minifcation would be:

<ul><li>A</li> <li>B</li></ul>

If a minification tool gave the following it would be appropriate to say that the minifier was broken.

<ul><li>A</li><li>B</li></ul>

The exception to this rule is PRE tags, which specify preformatted content and browsers are supposed to render all whitespace characters, and I image you'd also need to leave CDATA as is since it's technically not part of the HTML content.

Brett
  • 843
  • 1
  • 7
  • 18
0

As explained, minifying HTML does in fact alter lists in some cases, I have seen it.

For JS/CSS I only minify very large files like jQuery or the CSS that accompanies FontAwesome.

If the server is set-up properly, static resources will be served with an appropriate header telling the client they can cache it for awhile, and when that period expires most clients will simply ask the server if the file has changed and when it has not changed, a new period of time the client doesn't even ask starts.

Making sure the proper expires header is sent will save more bandwidth than minifying.

Especially with brotli compression, I am not convinced the space saved by minifying makes that big of a deal, with the possible exception of massive libraries like jQuery. Readability of the source as served is more valuable to maintain in my opinion than saving a few kilobytes when the browser does not already have the file cached.

Brotli compression is now supported everywhere except IE11, Opera Mini, and Baidu browser. Use it and I really suspect minification is a moot point.