HTML output minification is important to improve website performance by reducing the page load times and reduce the overall page size. Minifying HTML output also reduces the data usage of the user requesting the site.
Minification can be done by removing unnecessary details and eliminating excessive whitespaces, newlines, comments, etc. However, minification reduces the readability of the code. Minification can reduce file size upto 70%. PHP is used to transfer files from development to production environment. HTML files can be minified both manually and automatically. Minification can be undone using several tools that add whitespaces in the code. However, any comments removed during minification cannot be restored.
Example: This is the HTML file without minifying the the code.
HTML file after minification
<html><head><title>Title Page</title></head><body><h1>Hello world!</h1></body></html>
Approach 1: Using GZip Compression in Apache:
Steps to enable Gzip Compression in Apache
- Open the Apache configuration file
- Check the following line in the configuration file.
LoadModule deflate_module modules/mod_deflate.so
- Add the following lines at the end of configuration file.
- Restart the Apache Server
sudo service httpd restart
Approach 2: HTML code can be minified with ob_start() function with a callback.
<!DOCTYPE html><html><head><title>Demo for minifier</title></head><body><h1>Hello World</h1></body></html>
Steps to use HTMLMinifier:
- Download the HTMLMinifier file from https://www.terresquall.com/download/HTMLMinifier.php
- Include the following code into the php file
// Import the HTMLMinifier
// HTML source to be minified
// Minified version of the page
- Run the php file
- Nodejs | GM minify() Function
- How to pass form variables from one page to other page in PHP ?
- How to redirect a page to another page in HTML ?
- How to display search result of another page on same page using ajax in JSP?
- How to show Page Loading div until the page has finished loading?
- How to load jQuery code after loading the page?
- PHP Program to count Page Views
- Refresh a page using PHP
- How to add API function to a simple PHP Page ?
- How to create admin login page using PHP ?
- How to connect the Database with PHP DOM page ?
- How to add a PHP page to WordPress?
- HTML | Responsive full page image using CSS
- HTML Course | First Web Page | Printing Hello World
- How to get the title of an HTML page ?
- How to use Meta Tag to redirect an HTML page?
- How to design Meet the Team Page using HTML and CSS ?
- How to get title of current HTML page using jQuery ?
- How to select all visible or hidden elements in a HTML page using jQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.