css-inline
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 crate transforms HTML like this:
Big Text
into:
Big Text
- Uses reliable components from Mozilla's Servo project
- Inlines CSS from
styleandlinktags - Removes
styleandlinktags - Resolves external stylesheets (including local files)
- Optionally caches external stylesheets
- Works on Linux, Windows, and macOS
- Supports HTML5 & CSS3
- Bindings for Python, Ruby, JavaScript, Java, C, and a WebAssembly module to run in browsers.
- Command Line Interface
Playground
If you'd like to try css-inline, you can check the WebAssembly-powered playground to see the results instantly.
Installation
To include it in your project, add the following line to the dependencies section in your project's Cargo.toml file:
[]
= "0.18"
The Minimum Supported Rust Version is 1.80.
Usage
const HTML: &str = r#"<html>
<head>
<style>h1 { color:blue; }</style>
</head>
<body>
<h1>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, preserving the original structure:
const FRAGMENT: &str = r#"<main>
<h1>Hello</h1>
<section>
<p>who am i</p>
</section>
</main>"#;
const CSS: &str = r#"
p {
color: red;
}
h1 {
color: blue;
}
"#;
Configuration
css-inline can be configured by using CSSInliner::options() that implements the Builder pattern:
const HTML: &str = "...";
inline_style_tags. Specifies whether to inline CSS from "style" tags. Default:truekeep_style_tags. Specifies whether to keep "style" tags after inlining. Default:falsekeep_link_tags. Specifies whether to keep "link" tags after inlining. Default:falsekeep_at_rules. Specifies whether to keep "at-rules" (starting with@) after inlining. Default:falseminify_css. Specifies whether to remove trailing semicolons and spaces between properties and values. Default:falsebase_url. The base URL used to resolve relative URLs. If you'd like to load stylesheets from your filesystem, use thefile://scheme. Default:Noneload_remote_stylesheets. Specifies whether remote stylesheets should be loaded. Default:truecache. Specifies cache for external stylesheets. Default:Noneextra_css. Extra CSS to be inlined. Default:Nonepreallocate_node_capacity. 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:32
You can also skip CSS inlining for an HTML tag by adding the data-css-inline="ignore" attribute to it:
<!-- The tag below won't receive additional styles -->
Big Text
The data-css-inline="ignore" attribute also allows you to skip link and style tags:
<!-- Styles below are ignored -->
Big Text
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 keep_style_tags option is set to false.
<!-- Styles below are not removed -->
Big Text
Another possibility is to set keep_at_rules 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 keep_style_tags option is explicitly set to false.
<!-- With keep_at_rules=true "color:blue" will get inlined into <h1> but @media will be kept in <style> -->
Big Text
If you set the the minify_css option to true, the inlined styles will be minified by removing trailing semicolons
and spaces between properties and values.
<!-- With minify_css=true, the <h1> will have `style="color:blue;font-weight:bold"` -->
Big Text
If you'd like to load stylesheets from your filesystem, use the file:// scheme:
const HTML: &str = "...";
For resolving remote stylesheets it is possible to implement a custom resolver:
;
You can also cache external stylesheets to avoid excessive network requests:
use NonZeroUsize;
// This block is here for testing purposes
Caching is disabled by default.
Performance
css-inline typically inlines HTML emails within hundreds of microseconds, though results may vary with input complexity.
Benchmarks for css-inline==0.17.0:
- Basic: 4.10 µs, 230 bytes
- Realistic-1: 84.30 µs, 8.58 KB
- Realistic-2: 57.31 µs, 4.3 KB
- GitHub page: 135.06 ms, 1.81 MB
These benchmarks, conducted using rustc 1.87 on Ryzen 9 9950X, can be found in css-inline/benches/inliner.rs.
Command Line Interface
Installation
Install with cargo:
cargo install css-inline
Usage
The following command inlines CSS in multiple documents in parallel. The resulting files will be saved
as inlined.email1.html and inlined.email2.html:
css-inline email1.html email2.html
For full details of the options available, you can use the --help flag:
css-inline --help
Further reading
If you're interested in learning how this library was created and how it works internally, check out these articles:
Support
If you have any questions or discussions related to this library, please join our gitter!
License
This project is licensed under the terms of the MIT license.