(function(h,o,t,j,a,r){ h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)}; h._hjSettings={hjid:3011767,hjsv:6}; a=o.getElementsByTagName('head')[0]; r=o.createElement('script');r.defer=1; r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv; a.appendChild(r); })(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');

Minification Guide: GZip, Brotli & Code Optimization

Published on April 25, 2025
Last Updated on July 1, 2025

Written by

Morgan Frank - Specialist in Page Speed

Key Takeaways

  • Minification removes unnecessary characters from HTML, CSS, and JavaScript files without changing their functionality.
  • Compression (Gzip and Brotli) encodes files in a more efficient way, significantly reducing their size.
  • Use both minification and compression for maximum file size reduction.
  • Minification is typically done as part of your build process or using online tools.
  • Compression is typically enabled on the web server.
  • Brotli offers better performance, however, Gzip is still used.

After images, your website’s code files (HTML, CSS, and JavaScript) are often the next biggest contributors to page size. Minification and compression are two crucial techniques for reducing the size of these text-based files, leading to faster download times and improved page speed. These techniques are relatively easy to implement and can have a significant impact on performance.

html css js minification guide

What is Minification?

Minification is the process of removing unnecessary characters from your HTML, CSS, and JavaScript code without changing its functionality. These unnecessary characters include:

  • Whitespace: Spaces, tabs, and line breaks.
  • Comments: Explanatory notes within the code that are ignored by the browser.
  • Optional delimiters: Sometimes some characters could be removed.

Why Minify?

  • Smaller File Sizes: Removing these extra characters reduces the file size, leading to faster downloads.
  • Faster Parsing: The browser can parse (read and understand) minified code more quickly.
  • Improved Page Speed: Smaller files and faster parsing contribute to overall faster page load times.

Example (Before Minification – CSS):

 /* This is a CSS comment */
 body {
  font-family: Arial, sans-serif;  /* Set the font */
  background-color: #f0f0f0;
  margin: 0;
  padding: 20px;
 }

 h1 
  color: #333;  /* Dark gray */
  font-size: 2em;
 }

Example (After Minification CSS):

body{font-family:Arial,sans-serif;background-
color:#f0f0f0;margin:0;padding:20px}h1{color:#333;font-size:2em}

Example (Before Minification HTML):

 <!DOCTYPE html>
 <html>
  <head>
   <title>My Website</title>
  </head>
  <body>
   <h1> Hello, world! </h1>
   <p>This is a paragraph.</p>
  </body>
 </html>

Example (After Minification HTML):

<!DOCTYPE html><html><head><title>My Website</title></head><body>
<h1>Hello, world!</h1><p>This is a paragraph.</p></body></html>

Example (Before Minification Javascript):

// Function to add two numbers
 function addNumbers(a, b) {
 // Return the sum
 return a + b;
}

 // Call the function
 let result = addNumbers(5, 3);

 // Display the result
 console.log("The result is: " + result);

Example (After Minification Javascript):

function addNumbers(a,b){return a+b;}let 
result=addNumbers(5,3);console.log("The result is: "+result);

Notice how the minified code is much harder to read for humans, but it works exactly the same way for the browser.

Tools for Minification:

1. Online Minifiers:

  • Minifier.org: A versatile online minifier for HTML, CSS, and JavaScript.
  • CSS Minifier: Specifically for CSS.
  • JavaScript Minifier: Specifically for JavaScript.

2. Build Tools:

  • Webpack, Parcel, Rollup: These module bundlers often include minification as part of their build process.

3. Task Runners:

  • Gulp, Grunt: These task runners can be configured to minify files automatically.

4. Code Editors and IDEs:

  • Many code editors and IDEs (like VS Code, Sublime Text, WebStorm) have extensions or built-in features for minification.

5. WordPress plugins:

  • Autoptimize
  • W3 Total Cache.
online minification

What is Compression? (Gzip and Brotli)

While minification reduces file size by removing unnecessary characters, compression reduces file size by encoding the data in a more efficient way. Think of it like zipping a file on your computer. The most common compression methods used for web files are Gzip and Brotli.

  • Gzip: The most widely supported compression method. It’s been around for a long time and is supported by virtually all browsers.
  • Brotli: A newer compression algorithm that generally provides better compression than Gzip (smaller file sizes). It’s supported by most modern browsers.

How Compression Works (Simplified):

Compression algorithms look for repeating patterns in the text-based files (HTML, CSS, JavaScript) and replace them with shorter codes. The browser then decompresses the files on the fly when it receives them. This happens transparently to the user.

Why Use Compression?

  • Significantly Smaller File Sizes: Compression can often reduce file sizes by 70 – 90%, even after minification.
  • Faster Download Times: Smaller files download much faster, especially on slower connections.
  • Improved Page Speed: Faster downloads lead to significant improvements in overall page speed.

Enabling Compression (Server-Side):

Compression is typically enabled on the web server. You don’t need to change your code files themselves. The server compresses the files before sending them to the browser.

  • Apache: Use the mod_deflate module (for Gzip) and the mod_brotli module (for Brotli). You’ll typically configure this in your .htaccess file or your Apache configuration file.
  • Nginx: Use the gzip directive (for Gzip) and the brotli directive (for Brotli). Configure this in your Nginx configuration file
  • Other Servers: Consult your server’s documentation for instructions on enabling Gzip and Brotli compression.
  • Hosting Providers: Many good host provide by default.
server to browser infographic

Conclusion

Minification and compression are two essential techniques for optimizing your website’s performance. They’re relatively easy to implement, and the benefits in terms of reduced file sizes and faster loading times are substantial. By combining these techniques with other optimization strategies (like image optimization), you can create a significantly faster and more user-friendly website.

Shakeeb Sadikeen

The expert that experts learn from

About Author

Determined to change that, he built RapidLoad — a smart, AI-driven tool that empowers site owners to dramatically improve speed scores, enhance user experience, and meet Google’s Core Web Vitals without needing to touch a single line of code.
Connect with Shakeeb Sadikeen

Table of content