HTML Minifier
Remove whitespace, comments, and optional tags from HTML to reduce file size.
About this tool
HTML minification is the process of removing unnecessary characters from HTML code without changing its functionality. This includes stripping whitespace, line breaks, comments, and redundant closing tags that the browser can infer automatically. By reducing file size, minification improves page load times, reduces bandwidth consumption, and enhances overall performance—benefits that directly impact user experience and SEO rankings.
To use this HTML minifier, simply paste your HTML code into the input area and click the Minify button. The tool automatically removes unnecessary whitespace and comments while preserving the semantic structure of your markup. The minified output is instantly displayed and ready to copy, making it ideal for production deployments where every kilobyte matters.
HTML minification is especially valuable for static sites, single-page applications, and projects where code size affects hosting costs. The tool respects inline styles and attributes while cleaning up the overall structure, making it safe for both modern and legacy browsers. If you're using a build tool like webpack or Vite, they often include minification automatically, but this tool is perfect for quick optimizations or learning how minification works.
Frequently Asked Questions
Code Implementation
# pip install htmlmin
import htmlmin
html = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<!-- Page title -->
<title>My Page</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<!-- Main content -->
<header>
<h1>Hello, World!</h1>
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
</header>
<main>
<p>
Welcome to my website.
This is a paragraph with extra whitespace.
</p>
</main>
</body>
</html>"""
minified = htmlmin.minify(
html,
remove_comments=True,
remove_empty_space=True,
reduce_boolean_attributes=True,
)
print(minified)
original_size = len(html.encode("utf-8"))
minified_size = len(minified.encode("utf-8"))
print(f"Original: {original_size} bytes")
print(f"Minified: {minified_size} bytes")
print(f"Savings: {100 - minified_size / original_size * 100:.1f}%")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.