css-inline
css-inline inlines CSS into HTML documents, using components from Mozilla's Servo project.
This process is essential for sending HTML emails as you need to use "style" attributes instead of "style" tags.
For instance, the crate transforms HTML like this:
Test
Big Text
into:
Test
Big Text
- Uses reliable components from Mozilla's Servo
- Inlines CSS from
styleandlinktags - Removes
styleandlinktags - Resolves external stylesheets (including local files)
- Works on Linux, Windows, and macOS
- Supports HTML5 & CSS3
Installation
To include it in your project, add the following line to the dependencies section in your project's Cargo.toml file:
[]
= "0.10"
The Minimum Supported Rust Version is 1.60.
Usage
const HTML: &str = r#"<html>
<head>
<title>Test</title>
<style>h1 { color:blue; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>
</html>"#;
Configuration
css-inline can be configured by using CSSInliner::options() that implements the Builder pattern:
const HTML: &str = "...";
keep_style_tags. Specifies whether to keep "style" tags after inlining. Default:falsekeep_link_tags. Specifies whether to keep "link" tags after inlining. 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:trueextra_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:
Test
<!-- 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:
Test
<!-- Styles below are ignored -->
Big Text
If you'd like to load stylesheets from your filesystem, use the file:// scheme:
const HTML: &str = "...";
Standards support & restrictions
css-inline is built on top of html5ever and cssparser and relies on their behavior for HTML & CSS parsing.
- Only HTML 5 is supported, not XHTML.
- Only CSS 3 is supported.
- Only UTF-8 encoding for string representation. Other document encodings are not yet supported.
Bindings
We provide bindings for Python and WebAssembly. Check the bindings directory for more information.
Command Line Interface
Installation
Install with cargo:
cargo install css-inline
Usage
The following command inlines CSS in multiple documents in parallel. 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
Extra materials
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.