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.
pip install css-inlineVerified import paths — ran on the pinned version, not inferred.
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.
Pass `keep_style_tags=True` and/or `keep_link_tags=True` to the `inline()` function or `CSSInliner` constructor.
Pass `keep_at_rules=True` to the `inline()` function or `CSSInliner` constructor. Note that preserved `@rules` will remain in `<style>` tags if `keep_style_tags` is also `True`.
Be aware of this behavior for fragments. If strict fragment output is needed, post-process the output or use specialized methods if available for fragments.
Replace `inline_with_options(html, options)` with `inline(html, **options)` or instantiate `CSSInliner(options).inline(html)`.
Update calls from `inline_many([html1, html2])` to `inline_many([(html1, options1), (html2, options2)])`. An empty dictionary `{}` can be used for `options` if no specific options are needed for a given HTML string.Understand the inherent limitations of inline CSS for these features. If dynamic or interactive styles are critical, they must be handled via client-side JavaScript or by preserving `<style>` tags.
Ensure the library is installed using pip: `pip install css-inline`. The Python import statement should use an underscore: `import css_inline`.
Ensure that the HTML input passed to `css_inline.inline()` or `CSSInliner.inline()` is a valid string: `css_inline.inline('<html>...</html>')` or `inliner.inline('<html>...</html>')`.Use the correct top-level function `css_inline.inline()` or instantiate `css_inline.CSSInliner()` for more control: `import css_inline; inlined_html = css_inline.inline(your_html_string)`.
Provide a well-formed URL string to the `base_url` parameter, for example: `css_inline.CSSInliner(base_url='http://example.com/styles/')` or `css_inline.CSSInliner(base_url='file:///path/to/styles/')`.
No dependency data recorded yet.