Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is minifying your HTML, CSS, and Javascript a bad idea?

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?

like image 396
JohnCand Avatar asked Apr 25 '14 12:04

JohnCand


People also ask

Should I minify CSS and JS?

It is important to minify your CSS and minimise JavaScript files so they can load faster on your web pages. There are many reasons why you should minify your CSS and JavaScript: Reduce file size: The more code there is in a file, the larger it will be. Minified code is usually much smaller than the original version.

Is Minifying CSS worth it?

Now, because minification removes unnecessary characters from these HTML, CSS, and JS files, the size of these files get smaller. This in turn results in faster downloading and faster rendering of these files. Therefore minification can help improve your website speed.

Does Minifying CSS improve performance?

In simple words, minification is the method of removing all the unnecessary things including whitespaces and comments from HTML, CSS, and JavaScript code that do not affect our output. It reduces the project size and improves performance.

Does Minifying JavaScript improve performance?

Minifying strips out all comments, superfluous white space and shortens variable names. It thus reduces download time for your JavaScript files as they are (usually) a lot smaller in filesize. So, yes it does improve performance.


2 Answers

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/

like image 77
xec Avatar answered Oct 05 '22 23:10

xec


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.

like image 23
Ben Avatar answered Oct 05 '22 23:10

Ben