Minify HTML

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Viewer MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-viewer -- dnx GroupDocs.Viewer.Mcp --yes
Folders, licensing and more →

When you are looking for ways to optimize the rendering of documents to HTML, one of the solutions is the compression of the output content (HTML, CSS, and SVG). This is suitable if you provide your content from the web server over the Internet. The less the user has to download, the faster he accesses the rendered document.

Note
Minification does not reduce content size as much as Gzip compression, so use it in addition to Gzip compression.

Minification

Minification provides the output that looks identically with the original content in all browsers, but minified HTML content does not pass strict HTML validation. The following describes the size reduction methods used in minification.

HTML Minification

HTML minification includes the following:

  • Complete removal of comments (except containing IE conditional statements).
  • Conditional comments compression.
  • Removal of spaces and line breaks inside the tags and between the tags.
  • Simplification of document type declaration to <!DOCTYPE html>.
  • Removal of all HTML tag properties.
  • Removal of protocol declarations like http:, https:, and javascript: from path values.
  • Replace multiple spaces between words (except when they occur inside the pre or textarea tag) with a single space.
  • Removal of quotes around tag property values (except inline events).
  • Removal of default attributes for “script”, “style” and “link” tags.
  • Simplification of boolean attributes, so <input type="text" disabled="disabled"> becomes <input type=text disabled>.

CSS Minification

CSS minification includes the following:

  • Removal of all insignificant white spaces.
  • Removal of all comments.
  • Removal of all unnecessary semicolon separators.
  • Reducing color values.
  • Reducing integer representations by removing leading and trailing zeros.
  • Removal of unit specifiers from numeric zero values.

How to minify HTML and CSS

Use the setMinify method of the HtmlViewOptions class to enable minifying the output content.

The following code snippet shows how to enable minification:

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.HtmlViewOptions;
// ...

try (Viewer viewer = new Viewer("sample.docx")){
     HtmlViewOptions viewOptions = HtmlViewOptions.forEmbeddedResources();
     // Minify HTML output.
     viewOptions.setMinify(true);
     viewer.view(viewOptions);
}