|
| 1 | +title: Minification |
| 2 | +category: page |
| 3 | +slug: minification |
| 4 | +sortorder: 0718 |
| 5 | +toc: False |
| 6 | +sidebartitle: Minification |
| 7 | +meta: Minification reduces the size of web assets to make pages and applications load quicker. |
| 8 | + |
| 9 | + |
| 10 | +# Minification |
| 11 | +Minification is the process of reducing the size of |
| 12 | +[static content assets](/static-content.html) that need to be transferred |
| 13 | +from a [web server](/web-servers.html) to the web browser client. The goal |
| 14 | +of minification is to make webpages and web applications load faster, |
| 15 | +*without changing how the assets are ultimately used after being downloaded*. |
| 16 | + |
| 17 | +[Cascading Style Sheet (CSS) files](/cascading-style-sheets.html), |
| 18 | +[JavaScript](/javascript.html) and even HTML can be minified. The techniques |
| 19 | +to minify an HTML file differ from a CSS file because the file's contents |
| 20 | +and syntax are different. |
| 21 | + |
| 22 | + |
| 23 | +## CSS Minification example |
| 24 | +Let's say your web application has a CSS file with the following four lines |
| 25 | +to add some padding around every element with the `pad-me` class: |
| 26 | + |
| 27 | +``` |
| 28 | +.pad-me { padding-top: 10px; |
| 29 | + padding-right: 5px; |
| 30 | + padding-left: 5px; |
| 31 | + padding-bottom: 10px; } |
| 32 | +``` |
| 33 | + |
| 34 | +That example has 122 characters. A CSS minifier would take the above four |
| 35 | +lines and convert them to the following single line: |
| 36 | + |
| 37 | +``` |
| 38 | +.pad-me{padding:10px 5px 5px 10px} |
| 39 | +``` |
| 40 | + |
| 41 | +The result would have only a single line with 35 characters, that's 87 less |
| 42 | +characters that would need to be sent from the web server to the web browser! |
| 43 | +The minification process reduced the single CSS class by 71% but kept the exact |
| 44 | +same result when the web browser applies `pad-me` to all elements with that |
| 45 | +class. |
| 46 | + |
| 47 | +The file size savings can be a major benefit when applied across hundreds or |
| 48 | +thousands of lines in a typical CSS file. When you also multiply that savings |
| 49 | +across every client that downloads the CSS from your web server or CDN it becomes |
| 50 | +clear that minification is a powerful way to improve the efficiency of your |
| 51 | +production web application. |
| 52 | + |
| 53 | + |
| 54 | +### CSS minification resources |
| 55 | +* [CSS minifier](https://cssminifier.com/) is an online form to copy and paste |
| 56 | + CSS then retrieve the minified results on the same page. |
| 57 | + |
| 58 | + |
| 59 | +### JavaScript minification resources |
| 60 | +* [JavaScript minifier](https://javascript-minifier.com/) is similar to the |
| 61 | + above CSS minifier but works with JavaScript syntax. |
| 62 | + |
| 63 | + |
0 commit comments