css-inline: High-Performance CSS Inliner for HTML

0.20.2 · active · verified Sat Apr 11

css-inline is a high-performance Python library (version 0.20.2) for inlining CSS into HTML 'style' attributes. Built on components from Mozilla's Servo project, it is significantly faster than other alternatives and is primarily designed for use cases such as preparing HTML emails or embedding HTML into third-party web pages where inline styles are a requirement. It's actively maintained with regular releases.

Warnings

Install

Imports

Quickstart

This example demonstrates basic CSS inlining, showing how styles from a <style> tag are applied to elements. It also illustrates how to use `keep_at_rules` and `keep_style_tags` options to prevent the default removal of `@media` queries and the original style tags.

from css_inline import inline

html_content = """
<html>
<head>
    <style>
        h1 { color: blue; }
        p { font-size: 14px; margin-bottom: 5px; }
        .highlight { background-color: yellow; }
        @media screen and (max-width: 600px) { p { color: #555; } }
    </style>
</head>
<body>
    <h1 class="highlight">Hello, World!</h1>
    <p>This is a paragraph.</p>
    <p style="color: green;">This paragraph has an existing inline style.</p>
</body>
</html>
"""

inlined_html = inline(html_content, keep_at_rules=True, keep_style_tags=True)
print(inlined_html)

view raw JSON →