Skip to main content

High-performance library for inlining CSS into HTML 'style' attributes

Project description

css_inline

build status pypi versions license codecov.io gitter

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
  • 10-400x faster than alternatives
  • Inlines CSS from style and link tags
  • Removes style and link tags
  • Resolves external stylesheets (including local files)
  • Optionally caches external stylesheets
  • Can process multiple documents in parallel
  • Works on Linux, Windows, macOS and in the browser via PyOdide
  • Supports HTML5 & CSS3
  • Tested on CPython 3.9, 3.10, 3.11, 3.12, 3.13 and PyPy 3.9, 3.10.

Playground

If you'd like to try css-inline, you can check the WebAssembly-powered playground to see the results instantly.

Installation

Install with pip:

pip install css_inline

Pre-compiled wheels are available for most popular platforms. If not available for your platform, a Rust compiler will be needed to build this package from source. Rust version 1.65 or higher is required.

Usage

import css_inline

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

inlined = css_inline.inline(HTML)
# HTML becomes this:
#
# <html>
# <head>
#    <style>h1 { color:blue; }</style>
# </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, preserving the original structure:

FRAGMENT = """<main>
<h1>Hello</h1>
<section>
<p>who am i</p>
</section>
</main>"""

CSS = """
p {
    color: red;
}

h1 {
    color: blue;
}
"""

inlined = css_inline.inline_fragment(FRAGMENT, CSS)
# HTML becomes this:
# <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 inline_many and inline_many_fragments functions. This feature allows for concurrent processing of several inputs, significantly improving performance when dealing with a large number of documents.

import css_inline

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

Under the hood, inline_many, spawns threads at the Rust layer to handle the parallel processing of inputs. This results in faster execution times compared to employing parallel processing techniques at the Python level.

Note: To fully benefit from inline_many, you should run your application on a multicore machine.

Configuration

For configuration options use the CSSInliner class:

import css_inline

inliner = css_inline.CSSInliner(keep_style_tags=True)
inliner.inline("...")
  • inline_style_tags. Specifies whether to inline CSS from "style" tags. Default: True
  • keep_style_tags. Specifies whether to keep "style" tags after inlining. Default: False
  • keep_link_tags. Specifies whether to keep "link" tags after inlining. Default: False
  • base_url. The base URL used to resolve relative URLs. If you'd like to load stylesheets from your filesystem, use the file:// scheme. Default: None
  • load_remote_stylesheets. Specifies whether remote stylesheets should be loaded. Default: True
  • cache. Specifies caching options for external stylesheets (for example, StylesheetCache(size=5)). Default: None
  • extra_css. Extra CSS to be inlined. Default: None
  • preallocate_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:

<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:

<head>
  <!-- Styles below are not removed -->
  <style data-css-inline="keep">h1 { color:blue; }</style>
</head>
<body>
  <h1>Big Text</h1>
</body>

Such tags will be kept in the resulting HTML even if the keep_style_tags option is set to false.

If you'd like to load stylesheets from your filesystem, use the file:// scheme:

import css_inline

# styles/email is relative to the current directory
inliner = css_inline.CSSInliner(base_url="file://styles/email/")
inliner.inline("...")

You can also cache external stylesheets to avoid excessive network requests:

import css_inline

inliner = css_inline.CSSInliner(
    cache=css_inline.StylesheetCache(size=5)
)
inliner.inline("...")

Caching is disabled by default.

XHTML compatibility

If you'd like to work around some XHTML compatibility issues like closing empty tags (<hr> vs. <hr/>), you can use the following snippet that involves lxml:

import css_inline
from lxml import html, etree

document = "..."  # Your HTML document
inlined = css_inline.inline(document)
tree = html.fromstring(inlined)
inlined = etree.tostring(tree).decode(encoding="utf-8")

Performance

css-inline is powered by efficient tooling from Mozilla's Servo project and significantly outperforms other Python alternatives in terms of speed. Most of the time it achieves over a 10x speed advantage compared to the next fastest alternative.

Here is the performance comparison:

Size css_inline 0.15.0 premailer 3.10.0 toronado 0.1.0 inlinestyler 0.2.5 pynliner 0.8.0
Basic 230 B 4.72 µs 111.47 µs (23.57x) 614.57 µs (129.96x) 1.02 ms (216.10x) 1.15ms (258.45x)
Realistic-1 8.58 KB 86.67 µs 1.23 ms (14.23x) 13.80 ms (159.28x) 26.37 ms (304.25x) 45.24 ms (522.05x)
Realistic-2 4.3 KB 59.55 µs 1.78 ms (29.91x) ERROR 17.71 ms (297.39x) ERROR
GitHub page 1.81 MB 147.43 ms 14.08 s (95.47x) ERROR ERROR ERROR

The "Basic" case was obtained by benchmarking the example from the Usage section. Note that the toronado, inlinestyler, and pynliner libraries both encountered errors when used to inline CSS in the last scenario.

The benchmarking code is available in the benches/bench.py file. The benchmarks were conducted using the stable rustc 1.87, Python 3.12.5 on Ryzen 9 9950X.

Comparison with other libraries

Besides performance, css-inline differs from other Python libraries for CSS inlining.

  • Generally supports more CSS features than other libraries (for example, toronado and pynliner do not support pseudo-elements);
  • It has fewer configuration options and is not as flexible as premailer;
  • Works on fewer platforms than LXML-based libraries (premailer, inlinestyler, toronado, and optionally pynliner);
  • Does not have debug logs yet;
  • Supports only HTML 5.

Further reading

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

License

This project is licensed under the terms of the 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.15.0.tar.gz (58.3 kB view details)

Uploaded Source

Built Distributions

css_inline-0.15.0-pp310-pypy310_pp73-manylinux_2_24_x86_64.whl (1.9 MB view details)

Uploaded PyPymanylinux: glibc 2.24+ x86-64

css_inline-0.15.0-pp310-pypy310_pp73-manylinux_2_24_aarch64.whl (1.8 MB view details)

Uploaded PyPymanylinux: glibc 2.24+ ARM64

css_inline-0.15.0-pp310-pypy310_pp73-macosx_10_12_x86_64.whl (1.8 MB view details)

Uploaded PyPymacOS 10.12+ x86-64

css_inline-0.15.0-pp39-pypy39_pp73-manylinux_2_24_x86_64.whl (1.9 MB view details)

Uploaded PyPymanylinux: glibc 2.24+ x86-64

css_inline-0.15.0-pp39-pypy39_pp73-manylinux_2_24_aarch64.whl (1.8 MB view details)

Uploaded PyPymanylinux: glibc 2.24+ ARM64

css_inline-0.15.0-pp39-pypy39_pp73-macosx_10_12_x86_64.whl (1.8 MB view details)

Uploaded PyPymacOS 10.12+ x86-64

css_inline-0.15.0-cp39-abi3-win_amd64.whl (1.8 MB view details)

Uploaded CPython 3.9+Windows x86-64

css_inline-0.15.0-cp39-abi3-win32.whl (1.5 MB view details)

Uploaded CPython 3.9+Windows x86

css_inline-0.15.0-cp39-abi3-musllinux_1_2_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ x86-64

css_inline-0.15.0-cp39-abi3-musllinux_1_2_armv7l.whl (1.9 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARMv7l

css_inline-0.15.0-cp39-abi3-musllinux_1_2_aarch64.whl (2.0 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

css_inline-0.15.0-cp39-abi3-manylinux_2_24_armv7l.whl (1.7 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.24+ ARMv7l

css_inline-0.15.0-cp39-abi3-manylinux_2_24_aarch64.whl (1.8 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.24+ ARM64

css_inline-0.15.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

css_inline-0.15.0-cp39-abi3-manylinux_2_12_i686.manylinux2010_i686.whl (1.8 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.12+ i686

css_inline-0.15.0-cp39-abi3-macosx_10_12_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

css_inline-0.15.0-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (3.5 MB view details)

Uploaded CPython 3.9+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file css_inline-0.15.0.tar.gz.

File metadata

  • Download URL: css_inline-0.15.0.tar.gz
  • Upload date:
  • Size: 58.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for css_inline-0.15.0.tar.gz
Algorithm Hash digest
SHA256 7492824c3acb0a7682939b536153bf9b651400987d3c3d4defbb714c8419da75
MD5 696608a8f456a27533b121499e9b7f0a
BLAKE2b-256 22b46c49ad936fbfc99abee8a0ef26eb8084c7292b3efb42e4d04f92f035ffb1

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-pp310-pypy310_pp73-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-pp310-pypy310_pp73-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 8beb07c60bc172d8b0dba94a437fc081f02dde3c7d52f6cd6e8e7420007ceb0e
MD5 3ffb372ef630e0b70de369dfe0287a47
BLAKE2b-256 e2f6c5b4f9bb535d16286fa1f1d7070d59089131e5b899591dff77b1fc85c894

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-pp310-pypy310_pp73-manylinux_2_24_aarch64.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-pp310-pypy310_pp73-manylinux_2_24_aarch64.whl
Algorithm Hash digest
SHA256 b14fcb125474c0a5770a6e4682f9c31326078bf59ca4c3a2e5a9599da1d8690b
MD5 5a6d3c760265fd8b22ffeeba7ccb46fc
BLAKE2b-256 da57c2727ecc04f9073e0f813e8135364c42fdd5867e0375c7dc7bfcebf4df8d

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-pp310-pypy310_pp73-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-pp310-pypy310_pp73-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 44f322a6939f20ffcf1abdde2f7c6b24811993ec511c74069a872d03ca399689
MD5 12f9f5b368e59dc85882f84ded8c75c8
BLAKE2b-256 0a5bd54a17876b54c23a1dfbc76ea37b74bd2498e45a6ff3096e38dc04ea8bed

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-pp39-pypy39_pp73-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-pp39-pypy39_pp73-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 4ed1c9a06be070d79ccaef26f10bcc2ae42730e7176e547ea29156d9e306845c
MD5 cfde69cfb797c0cafd9edd762f6f195e
BLAKE2b-256 a5a9115452b99d652a01560a82774a0111489d2260cb61084d46c4a52ff16a0f

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-pp39-pypy39_pp73-manylinux_2_24_aarch64.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-pp39-pypy39_pp73-manylinux_2_24_aarch64.whl
Algorithm Hash digest
SHA256 b7b9439ece460359a51080ab875bc58c74b625f7eab184ce608f783a5ea252a5
MD5 cacbca05cace7883a04dc08170b50271
BLAKE2b-256 4581d995cb85cce8f7fd81b1fe3368e3239b4d62835df3541c9ff7a7cc4c60c0

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-pp39-pypy39_pp73-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-pp39-pypy39_pp73-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 172c73f8e7171d1f66be26dc21c1cefc3b81551d0f450ba19c8e654ce3a7b389
MD5 a2a37c14c14e671867a2dcad6460df64
BLAKE2b-256 1bc2997a1b2ab556fdd0ede451e9da6685e123aaaf13df56f2bd8af179744bbb

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: css_inline-0.15.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for css_inline-0.15.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 00ea17bf11268ddc15cf25df404b739b3d6cc2abdf86c004633a46135f189023
MD5 55031e12ee1bb454bd848591be01d9e0
BLAKE2b-256 1fef3e26bc07db9575cbb884756d853b6ff6ba18936a56a3003363a37c747214

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-cp39-abi3-win32.whl.

File metadata

  • Download URL: css_inline-0.15.0-cp39-abi3-win32.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.9+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for css_inline-0.15.0-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 1bd3ceef85c7cd677a202bd8b7ff3d8eb497bef1b754d2207b679a42358f2d84
MD5 97f66732445335a8901f8879c734241f
BLAKE2b-256 2e9a53031a6db57a48deb4540180ad5d76b9d16c2273a6efb8b6e402710151f3

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7b6396b3643e07ecc282119b62950258359311d4f655e86f121e59a916a685db
MD5 01ef38478aab894585413ea116f53349
BLAKE2b-256 f764316982af88b52eb87f8f8c17da900022253f026caac7d3b592a85899e546

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-cp39-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-cp39-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 27e43f8d67b1a3d043523f2a1025a4407fb7f2fab2ae6d09f14065a5b2976266
MD5 dee6777487de50a53fbaf9d44481b925
BLAKE2b-256 531457e7ab0a5422cc5897292776130411b3f8a07c4f2350cd5c0b5627b2e079

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 b55e70303e11c14323efca83aba99524cffdd74b7c831a67ed1971197a01e231
MD5 6b4b67f3a6c2c8ef678322f1d355239a
BLAKE2b-256 18a437e198a7b5cda5ba32b5d499f31efdd9cbd9b1a01edc1b10fe6603845fd7

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-cp39-abi3-manylinux_2_24_armv7l.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-cp39-abi3-manylinux_2_24_armv7l.whl
Algorithm Hash digest
SHA256 ee026777d945e3fb9b51351c01c5f2ff7eb77820be84d29d7afe5333e8d561b6
MD5 2a32b2913285911caccd2afe682aa505
BLAKE2b-256 5e53c8e3f92782a3634685558a5b57e06800c05eaeffb5880fc5d833c4bc22af

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-cp39-abi3-manylinux_2_24_aarch64.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-cp39-abi3-manylinux_2_24_aarch64.whl
Algorithm Hash digest
SHA256 1eea097695b00c0fa491bd07b7a0592ac275c8e2fc284e5e25d9cfaa6b59c66d
MD5 84bd7ad7609ddad3edeea86dc0ff36f0
BLAKE2b-256 b8379b6767346c0d91fa68d2727b98068c5f569a2d40e3bc3ac8653ce66ad181

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 593e6c7551800a243e84a245ade976f0fc8302807e95f4002b959f39812b3a3a
MD5 9e04608316a688e0d583e75b356af3f2
BLAKE2b-256 26098671a197ff29c528f9f533c7ffb6852cf65e7835357e020df7ca62938cde

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-cp39-abi3-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-cp39-abi3-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 898d3a4c8a475052703119fb72767eb106284026fd35774b4b992ba5fc696fba
MD5 cdc711597f920e50ca4a8b622fd733d9
BLAKE2b-256 87fe74084c0451a6d6015a27d3b79c7a6ccbcd5006439b4f610f4e60b17d7be3

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6013623085c1bb13b69cf401d4b85820930b8233fe2e5fc54c244cb661f277f0
MD5 a25c65a4a0c5ae1b4472889477091c30
BLAKE2b-256 30243273f8153fce0112f01edc3ede188c88d4f04c3bb96a44a6c35f58b0b81d

See more details on using hashes here.

File details

Details for the file css_inline-0.15.0-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for css_inline-0.15.0-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 fc9388d79f90b2bff4e87f3448b1ce354581462b8984ce396605730f640b7608
MD5 d4683db891f448fa3f714dd102498aff
BLAKE2b-256 15f580b16cdab790c27f08fda78a77771b6c53ccb9459bb6f4ca8569fe0f6aa4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page