blob: 8b9e6a382ff69fccc3235723f97abc69e65fb9e4 [file] [log] [blame]
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Rewrite Style Attributes</title>
<link rel="stylesheet" href="doc.css">
</head>
<body>
<!--#include virtual="_header.html" -->
<div id=content>
<h1>Rewrite Style Attributes</h1>
<h2>Configuration</h2>
<p>
The 'Rewrite Style Attributes' filter is enabled by specifying <em>either</em>:
<dl>
<dt>Apache:<dd><pre class="prettyprint"
>ModPagespeedEnableFilters rewrite_style_attributes,rewrite_css</pre>
<dt>Nginx:<dd><pre class="prettyprint"
>pagespeed EnableFilters rewrite_style_attributes,rewrite_css;</pre>
</dl>
or
<dl>
<dt>Apache:<dd><pre class="prettyprint"
>ModPagespeedEnableFilters rewrite_style_attributes_with_url,rewrite_css,rewrite_images</pre>
<dt>Nginx:<dd><pre class="prettyprint"
>pagespeed EnableFilters rewrite_style_attributes_with_url,rewrite_css,rewrite_images;</pre>
</dl>
<p>
in the configuration file. If both are enabled,
<code>rewrite_style_sttributes</code> takes precedence.
</p>
<h2>Description</h2>
<p>
The "Rewrite Style Attributes" filter rewrites CSS inside elements' style
attribute. It requires <code>rewrite_css</code> to also be enabled. See the settings for
<a href="filter-css-rewrite.html">rewrite_css</a> to control CSS minification, image rewriting,
image recompression, and cache extension.</p>
<p>
<code>rewrite_style_attributes</code> enables the filter for all style
attributes. <code>rewrite_style_attributes_with_url</code> enables it only
for style attributes that contain the text '<code>url(</code>', for which you will generally
want to enable one or more image optimization filters.</p>
<p>
<code>rewrite_style_attributes_with_url</code> is more efficient because it
does not always parse the CSS, but it will not optimize CSS that doesn't
reference any URLs. <code>rewrite_style_attributes</code> will always parse
the CSS and optimize everything possible. Because images are generally the
source for greatest improvement and are enclosed in <code>url()</code>,
<code>rewrite_style_attributes_with_url</code> is a good balance for most uses,
while <code>rewrite_style_attributes</code> is available for more aggressive
optimization.
</p>
<h2>Operation</h2>
<p>
This filter inspects the style attributes of all tags other than
<code>&lt;style&gt;</code> (since they cannot have a style attribute) and
rewrites them according to the configured <a href="filter-css-rewrite.html"
>rewrite_css</a> filter, which can include CSS minification, image rewriting,
image recompression, and cache extension.
</p>
<p>
As explained above, if <code>rewrite_style_attributes_with_url</code> is
enabled the style attribute's contents are first scanned to ensure that they
contain the text <code>url(</code> and are processed only if so.
</p>
<p>
For example, if a <code>&lt;div&gt;</code> tag looks like this:
</p>
<pre class="prettyprint">
&lt;div style=&quot;background-image: url('images/Puzzle.jpg');&quot;/&gt;
</pre>
<p>
Then, if the image is optimizable, PageSpeed will convert it to something
like:
<pre class="prettyprint">
&lt;div style=&quot;background-image:url('images/Puzzle.jpg.pagespeed.ic.7X5cYtoCx-.jpg');&quot;/&gt;
</pre>
<h3>Example</h3>
<p>
You can see the filter in action at <code>www.modpagespeed.com</code> on this
<a href="https://www.modpagespeed.com/examples/rewrite_style_attributes.html?ModPagespeed=on&amp;ModPagespeedFilters=rewrite_style_attributes_with_url,rewrite_css,rewrite_images">example</a>.
</p>
<h2>Risks</h2>
<p>
This filter is considered minimal risk.
<code>rewrite_style_attributes_with_url</code> is a core filter but
<code>rewrite_style_attributes</code> is not.
</p>
</div>
<!--#include virtual="_footer.html" -->
</body>
</html>