trim_css

The `trim_css` directive enables stripping of whitespace and comments from inline CSS in HTML responses.

Syntaxtrim_css on | off;
Defaultoff
Contexthttp, server, location
Arguments1

Description

The trim_css directive in NGINX is designed specifically for use within the ngx_http_trim_filter module, which processes HTML responses before they are sent to the client. When enabled with trim_css on;, this directive instructs NGINX to remove unnecessary whitespace and comments from inline CSS found within <style> tags in the HTML content. This can significantly decrease the size of the HTML payload, improving load times and overall performance of web pages, especially for CSS that might otherwise contain excessive whitespace or comments.

The functionality of the trim_css directive is part of a broader trimming mechanism which operates on a state machine paradigm. It analyzes the HTML and CSS content character by character, determining when to preserve or discard whitespace and comments based on context. The directive is particularly useful for optimizing web page rendering and is complementary to related directives that manage JavaScript trimming and overall HTML trimming. It is important to note that the directive respects certain tags where content is left unmodified, such as <pre>, <textarea>, and inline comments for Internet Explorer (IE).

In contexts like http, server, or location, this directive can be selectively applied depending on specific requirements of the web application or site structure.

Config Example

location / {
    trim on;
    trim_css on;
}

Ensure that trim_css is set in the correct context (http, server, location) for it to take effect.

Overusing trimming can lead to loss of important whitespace in CSS if not properly tested before deployment.

Conflicts may arise if multiple trimming directives are used incorrectly in combination.

← Back to all directives