Skip to main content

Fast CSS inlining written in Rust

Project description


Build PyPI Python versions License

Blazing-fast CSS inlining for Python implemented with Mozilla's Servo project components.


  • Removing style tags after inlining;
  • Resolving external stylesheets (including local files);
  • Control if style tags should be processed;
  • Out-of-document CSS to inline;
  • Inlining multiple documents in parallel (via Rust-level threads)

The project supports CSS Syntax Level 3.


To install css_inline via pip run the following command:

pip install css_inline

Pre-compiled wheels for most popular platforms are provided. If your platform is not in the support table below, you will need a Rust compiler to build this package from source. The minimum supported Rust version is 1.54.


To inline CSS in a HTML document:

import css_inline

HTML = """<html>
    <style>h1 { color:blue; }</style>
    <h1>Big Text</h1>

inlined = css_inline.inline(HTML)
# HTML becomes this:
# <html>
# <head>
#    <title>Test</title>
#    <style>h1 { color:blue; }</style>
# </head>
# <body>
#     <h1 style="color:blue;">Big Text</h1>
# </body>
# </html>

If you want to inline many HTML documents, you can utilize inline_many that processes the input in parallel.

import css_inline

css_inline.inline_many(["<...>", "<...>"])

inline_many will use Rust-level threads; thus, you can expect it's running faster than css_inline.inline via Python's multiprocessing or threading modules.

For customization options use the CSSInliner class:

import css_inline

inliner = css_inline.CSSInliner(remove_style_tags=True)


Due to the usage of efficient tooling from Mozilla's Servo project (html5ever, rust-cssparser and others) this library has excellent performance characteristics. In comparison with other Python projects, it is ~7-15x faster than the nearest alternative.

For inlining CSS in the html document from the Usage section above there is the following breakdown in the benchmarks:

  • css_inline 0.8.2 - 21.75 us
  • premailer 3.10.0 - 329.51 us (x15.14)
  • toronado 0.1.0 - 1.59 ms (x73.28)
  • inlinestyler 0.2.5 - 2.37 ms (x109.27)
  • pynliner 0.8.0 - 2.78 ms (x127.89)

And for a more realistic email:

  • css_inline 0.8.2 - 443.83 us
  • premailer 3.10.0 - 3.25 ms (x7.33)
  • toronado 0.1.0 - 35.35 ms (x79.65)
  • inlinestyler 0.2.5 - 61.08 ms (x137.62)
  • pynliner 0.8.0 - 99.52 ms (x224.24)

You can take a look at the benchmarks' code at benches/ file. The results above were measured with stable rustc 1.61.0, Python 3.10.4, Linux x86_64 on i8700K, and 32GB RAM.

Python support

css_inline supports CPython 3.6, 3.7, 3.8, 3.9, 3.10, and PyPy 3.7 and 3.8.

The following wheels are available:

macOS Intel macOS ARM64 Windows 64bit Windows 32bit
CPython 3.6 ✔️ ✔ ️ ✔️ ✔️
CPython 3.7 ✔️ ✔️ ✔️
CPython 3.8 ✔️ ✔️ ✔️ ✔️
CPython 3.9 ✔️ ✔️ ✔️ ✔️
CPython 3.10 ✔️ ✔️ ✔️ ✔️
PyPy 3.7 v7.3 ✔¹ ✔¹ ✔️ N/A ✔️ N/A
PyPy 3.8 v7.3 ✔¹ ✔¹ ✔️ N/A ✔️ N/A

¹ PyPy is only supported for manylinux wheels.

Extra materials

If you want to know how this library was created & how it works internally, you could take a look at these articles:


The code in this project is licensed under MIT license. By contributing to css_inline, you agree that your contributions will be licensed under its MIT license.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

css_inline-0.8.3.tar.gz (24.6 kB view hashes)

Uploaded source

Built Distributions

css_inline-0.8.3-cp310-cp310-win_amd64.whl (1.3 MB view hashes)

Uploaded cp310

css_inline-0.8.3-cp310-cp310-win32.whl (1.2 MB view hashes)

Uploaded cp310

css_inline-0.8.3-cp39-cp39-win_amd64.whl (1.3 MB view hashes)

Uploaded cp39

css_inline-0.8.3-cp39-cp39-win32.whl (1.2 MB view hashes)

Uploaded cp39

css_inline-0.8.3-cp38-cp38-win_amd64.whl (1.3 MB view hashes)

Uploaded cp38

css_inline-0.8.3-cp38-cp38-win32.whl (1.2 MB view hashes)

Uploaded cp38

css_inline-0.8.3-cp37-cp37m-win_amd64.whl (1.3 MB view hashes)

Uploaded cp37

css_inline-0.8.3-cp37-cp37m-win32.whl (1.2 MB view hashes)

Uploaded cp37

css_inline-0.8.3-cp36-cp36m-win_amd64.whl (1.3 MB view hashes)

Uploaded cp36

css_inline-0.8.3-cp36-cp36m-win32.whl (1.2 MB view hashes)

Uploaded cp36

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page