# Collapse whitespace (collapse_whitespace)

Removes excess whitespace from HTML. The collapse_whitespace filter: what it does, its risks and the Apache, nginx and IIS directives.

Canonical URL: https://modpagespeed.com/docs/filters/collapse_whitespace/

- Category: HTML
- CoreFilters: No
- OptimizeForBandwidth: No
- Risk: Generally safe

`collapse_whitespace` shrinks the HTML payload by folding every run of whitespace (spaces, tabs, carriage returns, newlines) down to a single character. The markup keeps its structure; only the formatting bytes go. Live demo: [collapse_whitespace](https://modpagespeed.com/examples/collapse_whitespace/).

## How it works

### When it helps and when it does not

The saving scales with how much pretty-printing the templates do: indented server-side templates carry many collapsible bytes, already-compacted HTML almost none. Gzip and Brotli compress whitespace runs well, so the on-the-wire saving is much smaller than the source saving suggests. The filter earns most where HTML is served uncompressed, and least on an already minified template. Not a CoreFilter; enable it by name.

### How it decides

A whitespace run that contains a newline collapses to a newline, any other run to a single space; a run never collapses to nothing, so inline elements keep their separating space and text layout is preserved. Content inside `pre`, `code`, `script`, `style`, and `textarea` is never touched, because whitespace is meaningful there.

## When to use it

- Not in CoreFilters: it runs only when you enable it by name.
- Risk rating on these docs: **Generally safe**.

## Risks

- Pages whose rendering depends on whitespace-sensitive CSS, such as `white-space: pre` on ordinary elements, can change appearance; the filter sees the markup, not the stylesheet. Test such pages before enabling.

- Verify with the `X-Mod-Pagespeed` response header and a `?PageSpeedFilters=-collapse_whitespace` comparison; [Is it working?](https://modpagespeed.com/docs/is-it-working/) has the steps.

## Configuration

Enable it in the module configuration, at server, virtual-host or location scope:

**Apache**

```apache
ModPagespeedEnableFilters collapse_whitespace
```

**nginx**

```nginx
pagespeed EnableFilters collapse_whitespace;
```

**IIS (pagespeed.config)**

```text
pagespeed EnableFilters collapse_whitespace
```

```text
<!-- before -->
<ul class="nav">
    <li>  <a href="/a">Alpha</a>  </li>
    <li>  <a href="/b">Beta</a>   </li>
</ul>

<!-- after: each run that held a newline keeps one newline -->
<ul class="nav">
<li> <a href="/a">Alpha</a> </li>
<li> <a href="/b">Beta</a> </li>
</ul>
```

Scoping, `ForbidFilters` and the thresholds filters read: [Choosing filters](https://modpagespeed.com/docs/filter-selection/).

## Live example

See the before and after, with the source diff: [collapse_whitespace example](https://modpagespeed.com/examples/collapse_whitespace/).

## Frequently asked questions

**What does the collapse_whitespace filter do?**

`collapse_whitespace` shrinks the HTML payload by folding every run of whitespace (spaces, tabs, carriage returns, newlines) down to a single character. The markup keeps its structure; only the formatting bytes go. Live demo: collapse_whitespace.

**Is collapse_whitespace enabled by default?**

No. `collapse_whitespace` is not in CoreFilters, the default RewriteLevel; it runs only when you enable it by name with `EnableFilters`.

**How do I enable collapse_whitespace on Apache and nginx?**

Add `ModPagespeedEnableFilters collapse_whitespace` on Apache or `pagespeed EnableFilters collapse_whitespace;` on nginx. On IIS, add `pagespeed EnableFilters collapse_whitespace` to pagespeed.config.

## Related filters

- [`add_base_tag`](https://modpagespeed.com/docs/filters/add_base_tag/): Add base tag
- [`add_head`](https://modpagespeed.com/docs/filters/add_head/): Add head
- [`add_ids`](https://modpagespeed.com/docs/filters/add_ids/): Add IDs
- [`add_instrumentation`](https://modpagespeed.com/docs/filters/add_instrumentation/): Add instrumentation
- [`combine_heads`](https://modpagespeed.com/docs/filters/combine_heads/): Combine heads
- [`compute_statistics`](https://modpagespeed.com/docs/filters/compute_statistics/): Compute HTML statistics
- [`convert_meta_tags`](https://modpagespeed.com/docs/filters/convert_meta_tags/): Convert meta tags
- [`debug`](https://modpagespeed.com/docs/filters/debug/): Debug comments

This page is drawn from the [collapse_whitespace entry on HTML filters](https://modpagespeed.com/docs/html-filters/#collapse_whitespace). Every filter in one table: [PageSpeed filters](https://modpagespeed.com/docs/filters/).
