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-500x 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, 3.14 and PyPy 3.11.

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
  • keep_at_rules. Specifies whether to keep "at-rules" (starting with @) after inlining. Default: False
  • minify_css. Specifies whether to remove trailing semicolons and spaces between properties and values. 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
  • remove_inlined_selectors. Specifies whether to remove selectors that were successfully inlined from <style> blocks. Default: False
  • apply_width_attributes. Specifies whether to add width HTML attributes from CSS width properties on supported elements (table, td, th, img). Default: False
  • apply_height_attributes. Specifies whether to add height HTML attributes from CSS height properties 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 keep_style_tags 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 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.

<head>
  <!-- With keep_at_rules=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 the minify_css option to true, the inlined styles will be minified by removing trailing semicolons and spaces between properties and values.

<head>
  <!-- With minify_css=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:

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.19.0 premailer 3.10.0 toronado 0.1.0 inlinestyler 0.2.5 pynliner 0.8.0
Basic 230 B 4.27 µs 85.05 µs (19.93x) 495.30 µs (116.05x) 1.02 ms (238.87x) 867.79 µs (203.32x)
Realistic-1 8.58 KB 80.59 µs 1.03 ms (12.76x) 11.55 ms (143.29x) 26.37 ms (327.21x) 11.71 ms (145.36x)
Realistic-2 4.3 KB 46.88 µs 1.44 ms (30.73x) ERROR 17.71 ms (377.77x) ERROR
GitHub page 1.81 MB 17.57 ms 10.78 s (613.48x) ERROR ERROR ERROR

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

The benchmarking code is available in the benches/bench.py file. The benchmarks were conducted using the stable rustc 1.91, Python 3.14.2 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.20.1.tar.gz (72.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

css_inline-0.20.1-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl (2.0 MB view details)

Uploaded PyPymanylinux: glibc 2.24+ x86-64

css_inline-0.20.1-pp311-pypy311_pp73-manylinux_2_24_aarch64.whl (1.9 MB view details)

Uploaded PyPymanylinux: glibc 2.24+ ARM64

css_inline-0.20.1-pp311-pypy311_pp73-macosx_10_12_x86_64.whl (1.9 MB view details)

Uploaded PyPymacOS 10.12+ x86-64

css_inline-0.20.1-cp39-abi3-win_amd64.whl (1.9 MB view details)

Uploaded CPython 3.9+Windows x86-64

css_inline-0.20.1-cp39-abi3-win32.whl (1.6 MB view details)

Uploaded CPython 3.9+Windows x86

css_inline-0.20.1-cp39-abi3-musllinux_1_2_x86_64.whl (2.2 MB view details)

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

css_inline-0.20.1-cp39-abi3-musllinux_1_2_armv7l.whl (2.0 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARMv7l

css_inline-0.20.1-cp39-abi3-musllinux_1_2_aarch64.whl (2.1 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

css_inline-0.20.1-cp39-abi3-manylinux_2_24_armv7l.whl (1.8 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.24+ ARMv7l

css_inline-0.20.1-cp39-abi3-manylinux_2_24_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.24+ ARM64

css_inline-0.20.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

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

css_inline-0.20.1-cp39-abi3-manylinux_2_12_i686.manylinux2010_i686.whl (1.9 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.12+ i686

css_inline-0.20.1-cp39-abi3-macosx_10_12_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

css_inline-0.20.1-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (3.6 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.20.1.tar.gz.

File metadata

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

File hashes

Hashes for css_inline-0.20.1.tar.gz
Algorithm Hash digest
SHA256 31feb709e515bf1e432d21856d3ed78d99282bd8249cc13bb9e2d3f52e0229d6
MD5 f47314418916272e24ee3ec2844d6fbd
BLAKE2b-256 3f53d796b1837687d302b677dc07a575eaa089950a263ff9d91a4aea0ce9f45a

See more details on using hashes here.

File details

Details for the file css_inline-0.20.1-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for css_inline-0.20.1-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 e8913194b620c5adaafc4f4906b70ca2ed0eb67b526611475c3e7848e270c71a
MD5 46f7dc06beac03852cc4d601ead79d7b
BLAKE2b-256 a169d77969f2e7d94d3f6dc8f505b02d9957e8a854aba3b14d2245c1ad13df57

See more details on using hashes here.

File details

Details for the file css_inline-0.20.1-pp311-pypy311_pp73-manylinux_2_24_aarch64.whl.

File metadata

File hashes

Hashes for css_inline-0.20.1-pp311-pypy311_pp73-manylinux_2_24_aarch64.whl
Algorithm Hash digest
SHA256 e70b9d5e73893c1b784949964f820793ecb786f29e13bbf71a2777d1d7fa209c
MD5 0c617abf540fcc2fdfa5e51aebd8cfea
BLAKE2b-256 a4955dc3e745f0b4cd09665d12f0477ecd5e0811969c7672b7aa680477db2361

See more details on using hashes here.

File details

Details for the file css_inline-0.20.1-pp311-pypy311_pp73-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for css_inline-0.20.1-pp311-pypy311_pp73-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5fb5ca1e6a562f5cf25414e08118b695f170013c21db54b38adf43d16d315192
MD5 686df0e04edbe162fc4b31aa3a5de035
BLAKE2b-256 ab21cee8a2678b483467197c47a4a8b14525b3b58593204307b7a8e84e7272f4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for css_inline-0.20.1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 786c0899eed5dc5331f918fe969bfa7e80140cec1b2d3553765c514d06e9b0d5
MD5 8dbd232bb72c7cbaec5956c824caef7e
BLAKE2b-256 ba34333eee73d197c9338c073438759b8f5f20222c556ddbd318cc5251de4e80

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for css_inline-0.20.1-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 778b2e194cf62d7b9ffd157a35576ae52a93d84cceda4ad4d8ea9feaa95e31fc
MD5 fc52e1e3976030d0884c0b36a161a7c2
BLAKE2b-256 d46bc12aa1d1593e275d43362544dba927f5b1184014140a50b5e1c2c5ed2205

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for css_inline-0.20.1-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 689e68f1a3c5eadc5f38c00b36f3bb142009063c7c951edcf00f128f6b09a053
MD5 af87dd6959a92d1626d906093f526616
BLAKE2b-256 e32a5e6012b134f41051e976bcde73633496620fe4f6d559228a2479e0965857

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for css_inline-0.20.1-cp39-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 fb7ef298dd131e9b942bd3f5353bc56d7a79e23403044d9a154ab69a8a336b20
MD5 da098910cbe37af9baa9c6e3725cfa9e
BLAKE2b-256 0801dba4f0a109d11f8b27d548fcd87daf80a2adaf241ff0bee6b4c8f7c2ec18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for css_inline-0.20.1-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 39d122c53dc66ff05b65ca5087b18b5e7a62ed42bcab905d0669c6c2218905c0
MD5 128cb266608029ef4db6c08a5193a851
BLAKE2b-256 a1887966576b2cc7bf3837fd75b0426659e25f8fd236082096c63086b87a5f60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for css_inline-0.20.1-cp39-abi3-manylinux_2_24_armv7l.whl
Algorithm Hash digest
SHA256 8492179e53009e49a046c8cee5af5817deb0934fb0f99ee8d20fb10dc9a78f18
MD5 f5309dd83d3d3e9b4a551b9d6be2ff7a
BLAKE2b-256 d751db2ce9592ed074ca83faa0dea83bd8a1a7f907f5052f3f2a722528509612

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for css_inline-0.20.1-cp39-abi3-manylinux_2_24_aarch64.whl
Algorithm Hash digest
SHA256 047e855d02ec0422625f346cad2d4bdec1126b48ac256f42ad4814f926f91acb
MD5 b12caaf6b496f26a631a22d474a9021a
BLAKE2b-256 4091284d7e5f4721331b4df1705a00ecdf1f33830603634ad60da196ac0b6754

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for css_inline-0.20.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a4e9160617765e020b4dd421e1216fead1fc612de026f1274b5087c5c66ea298
MD5 8d67d86bb815207935817f27b092bb23
BLAKE2b-256 02bf66d6609e8a66131f1322da8da4af72463f1ee66613dc8e2d894edf90d87e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for css_inline-0.20.1-cp39-abi3-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 5e38b76fdc78f0f194413f017a0689cba1736814febe8df58470d7e625b6a986
MD5 59fa768fe7bf5c6eb1d22906d6f2e88f
BLAKE2b-256 108b42167e57f5a527bb9b4eab2ea22a7ba5fd5a5a7ad4b12aca4e3d951a9c33

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for css_inline-0.20.1-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 02b40a24944de39774c1f35f84ce80ceffca3c1d8d0ff78f7a968d14eb4be9a1
MD5 3bc02020a80b982a327c06bc663b15f5
BLAKE2b-256 8fe38506d47edf94146863eb9310b6ce866f9addd85efbf782cb9fcccaa7dc59

See more details on using hashes here.

File details

Details for the file css_inline-0.20.1-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.20.1-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 9f5a3f60656bcc4eeb06bdc9d088753f5594f16f6440f7c9633862212fc82a4d
MD5 3fa7f1135dc4b78bc8824ea55e2d8d67
BLAKE2b-256 7685dba4fba954ceb96090bdf5d4830cd7abd3f63bbe9931ebdd7940d7e6c41d

See more details on using hashes here.

Supported by

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