css_inline is a high-performance library for inlining CSS into HTML 'style' attributes.
This library is designed for scenarios such as preparing HTML emails or embedding HTML into third-party web pages.
For instance, the library transforms HTML like this:
<html>
<head>
<style>h1 { color:blue; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>
</html>into:
<html>
<head></head>
<body>
<h1 style="color:blue;">Big Text</h1>
</body>
</html>- Uses reliable components from Mozilla's Servo project
- 3-25x faster than alternatives
- Inlines CSS from
styleandlinktags - Removes
styleandlinktags - Resolves external stylesheets (including local files)
- Optionally caches external stylesheets
- Can process multiple documents in parallel
- Works on Linux and macOS (Windows is not supported)
- Supports HTML5 & CSS3
If you'd like to try css-inline, you can check the WebAssembly-powered playground to see the results instantly.
css_inline is distributed as a PHP extension. You'll need to compile it from source:
git clone https://github.com/Stranger6667/css-inline.git
cd css-inline/bindings/php
cargo build --releaseThen copy the compiled extension to your PHP extensions directory:
# Linux
cp target/release/libcss_inline_php.so $(php-config --extension-dir)/css_inline.so
# macOS
cp target/release/libcss_inline_php.dylib $(php-config --extension-dir)/css_inline.soEnable the extension in your php.ini:
extension=css_inlineRequirements:
- PHP 8.2 or higher
- Rust toolchain (for building from source)
- Linux or macOS (Windows is not supported by the underlying
ext-php-rslibrary)
<?php
$html = <<<HTML
<html>
<head>
<style>h1 { color:blue; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>
</html>
HTML;
$inlined = CssInline\inline($html);
// HTML becomes:
// <html>
// <head></head>
// <body>
// <h1 style="color:blue;">Big Text</h1>
// </body>
// </html>Note that css_inline automatically adds missing html and body tags, so the output is a valid HTML document.
Alternatively, you can inline CSS into an HTML fragment. Structural tags (<html>, <head>, <body>) are stripped from the output; only their contents are preserved. Use CssInline\inline if you need to keep the full document structure:
<?php
$fragment = <<<HTML
<main>
<h1>Hello</h1>
<section>
<p>who am i</p>
</section>
</main>
HTML;
$css = <<<CSS
p {
color: red;
}
h1 {
color: blue;
}
CSS;
$inlined = CssInline\inlineFragment($fragment, $css);
// HTML becomes:
// <main>
// <h1 style="color: blue;">Hello</h1>
// <section>
// <p style="color: red;">who am i</p>
// </section>
// </main>When there is a need to inline multiple HTML documents simultaneously, css_inline offers inlineMany and inlineManyFragments functions.
This feature allows for concurrent processing of several inputs, significantly improving performance when dealing with a large number of documents.
<?php
$results = CssInline\inlineMany([$html1, $html2, $html3]);Under the hood, inlineMany spawns threads at the Rust layer to handle the parallel processing of inputs.
Note: To fully benefit from inlineMany, you should run your application on a multicore machine.
For configuration options use the CssInliner class:
<?php
use CssInline\CssInliner;
$inliner = new CssInliner(keepStyleTags: true);
$inliner->inline($html);inlineStyleTags. Specifies whether to inline CSS from "style" tags. Default:truekeepStyleTags. Specifies whether to keep "style" tags after inlining. Default:falsekeepLinkTags. Specifies whether to keep "link" tags after inlining. Default:falsekeepAtRules. Specifies whether to keep "at-rules" (starting with@) after inlining. Default:falseminifyCss. Specifies whether to remove trailing semicolons and spaces between properties and values. Default:falsebaseUrl. The base URL used to resolve relative URLs. If you'd like to load stylesheets from your filesystem, use thefile://scheme. Default:nullloadRemoteStylesheets. Specifies whether remote stylesheets should be loaded. Default:truecache. Specifies caching options for external stylesheets (for example,new StylesheetCache(size: 5)). Default:nullextraCss. Extra CSS to be inlined. Default:nullpreallocateNodeCapacity. Advanced. Preallocates capacity for HTML nodes during parsing. This can improve performance when you have an estimate of the number of nodes in your HTML document. Default:32removeInlinedSelectors. Specifies whether to remove selectors that were successfully inlined from<style>blocks. Default:falseapplyWidthAttributes. Specifies whether to addwidthHTML attributes from CSSwidthproperties on supported elements (table,td,th,img). Default:falseapplyHeightAttributes. Specifies whether to addheightHTML attributes from CSSheightproperties on supported elements (table,td,th,img). Default:false
You can also skip CSS inlining for an HTML tag by adding the data-css-inline="ignore" attribute to it:
<head>
<style>h1 { color:blue; }</style>
</head>
<body>
<!-- The tag below won't receive additional styles -->
<h1 data-css-inline="ignore">Big Text</h1>
</body>The data-css-inline="ignore" attribute also allows you to skip link and style tags:
<head>
<!-- Styles below are ignored -->
<style data-css-inline="ignore">h1 { color:blue; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>Alternatively, you may keep style from being removed by using the data-css-inline="keep" attribute.
This is useful if you want to keep @media queries for responsive emails in separate style tags.
Such tags will be kept in the resulting HTML even if the keepStyleTags option is set to false.
<head>
<!-- Styles below are not removed -->
<style data-css-inline="keep">h1 { color:blue; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>Another possibility is to set keepAtRules option to true. At-rules cannot be inlined into HTML therefore they
get removed by default. This is useful if you want to keep at-rules, e.g. @media queries for responsive emails in
separate style tags but inline any styles which can be inlined.
Such tags will be kept in the resulting HTML even if the keepStyleTags option is explicitly set to false.
<head>
<!-- With keepAtRules=true "color:blue" will get inlined into <h1> but @media will be kept in <style> -->
<style>h1 { color: blue; } @media (max-width: 600px) { h1 { font-size: 18px; } }</style>
</head>
<body>
<h1>Big Text</h1>
</body>If you set the minifyCss option to true, the inlined styles will be minified by removing trailing semicolons
and spaces between properties and values.
<head>
<!-- With minifyCss=true, the <h1> will have `style="color:blue;font-weight:bold"` -->
<style>h1 { color: blue; font-weight: bold; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>If you'd like to load stylesheets from your filesystem, use the file:// scheme:
<?php
use CssInline\CssInliner;
// styles/email is relative to the current directory
$inliner = new CssInliner(baseUrl: "file://styles/email/");
$inliner->inline($html);You can also cache external stylesheets to avoid excessive network requests:
<?php
use CssInline\CssInliner;
use CssInline\StylesheetCache;
$inliner = new CssInliner(
cache: new StylesheetCache(size: 5)
);
$inliner->inline($html);Caching is disabled by default.
css_inline is powered by efficient tooling from Mozilla's Servo project and significantly outperforms other PHP alternatives in terms of speed.
Here is the performance comparison:
| Size | css_inline 0.19.0 |
css-to-inline-styles 2.3.0 |
emogrifier 7.3.0 |
|
|---|---|---|---|---|
| Simple | 230 B | 5.69 µs | 26.22 µs (4.61x) | 134.37 µs (23.61x) |
| Realistic email 1 | 8.58 KB | 94.07 µs | 288.20 µs (3.06x) | 588.00 µs (6.25x) |
| Realistic email 2 | 4.3 KB | 58.15 µs | 585.24 µs (10.07x) | 2.24 ms (38.58x) |
| GitHub Page† | 1.81 MB | 24.78 ms | ERROR | ERROR |
† The GitHub page benchmark contains complex modern CSS that neither css-to-inline-styles nor emogrifier can process.
Please refer to the benchmarks/InlineBench.php file to review the benchmark code.
The results displayed above were measured using stable rustc 1.91 on PHP 8.4.14.
If you want to know how this library was created & how it works internally, you could take a look at these articles:
This project is licensed under the terms of the MIT license.