Stranger6667/css-inline: High-performance library for inlining CSS into HTML ‘style’ attributes

4 phút đọc

Stranger6667/css-inline: High-performance library for inlining CSS into HTML 'style' attributes – Repository Stranger6667/css-inline. Mô tả: High-performance library for inlining CSS into HTML 'style' attributes. 320 stars, 45 forks, ngôn ngữ chính Rust, cập nhật 2026-11-06.

1. Repository làm gì?

css_inline inlines CSS into HTML style attributes. Use it to prepare HTML emails or to embed HTML into third-party web pages.

Try css-inline in the WebAssembly-powered playground.

Add css-inline to the dependencies in your Cargo.toml:

Minimum Supported Rust Version: 1.88.

For library-only use, list the features you need:

Without http and file, css-inline returns an error for any link stylesheet it cannot load.

2. Dữ liệu và cấu trúc đáng chú ý

inline adds missing html and body tags, so the output is a complete HTML document.

To inline CSS into an HTML fragment, call inline_fragment with the fragment and the CSS. The output stays a fragment: inline_fragment does not add , or , and if the input contains them, it keeps only their contents. Use inline for full documents:

inline and inline_fragment return css_inline::Result , with css_inline::InlineError as the error type:

A stylesheet that fails to load aborts inlining with an error. Inside a style tag or stylesheet, css-inline skips rules with invalid or unsupported selectors and inlines the rest.

Configure css-inline with the builder that CSSInliner::options() returns:

CSSInliner is Send + Sync: build it once and reuse it across calls and threads.

Add data-css-inline="ignore" to a tag to skip inlining styles into it:

3. Khả năng ứng dụng và giới hạn

On a link or style tag, the same attribute excludes its styles from inlining:

Add data-css-inline="keep" to a style tag to keep it in the output, for example to preserve @media queries for responsive emails. The tag stays even when keep_style_tags is false.

A style attribute cannot hold at-rules, so css-inline drops them by default. Set keep_at_rules to true to inline regular rules and keep at-rules such as @media in a style tag. That tag stays even when keep_style_tags is false. @layer is the exception: rules inside cascade layers are inlined with layer precedence, and at-rules nested in a layer are kept wrapped in it.

Set minify_css to true to drop trailing semicolons and spaces between properties and values in inlined styles:

Set resolve_css_variables to true for CSS that uses custom properties (e.g. Tailwind v4 or design tokens), which email clients do not support. var() references are replaced with the values the properties have on each element, with fallbacks for undefined or cyclic ones, and the –* declarations are dropped:

A declaration whose var() cannot be resolved and has no fallback is dropped. Custom properties defined inside @media and other at-rules are not inlined, so they do not take part.

To load stylesheets from the filesystem, set base_url with the file:// scheme. css-inline joins relative link hrefs onto it, so end the directory with /: with file://styles/email/, href="main.css" resolves to styles/email/main.css; without the slash, to styles/main.css. A standard URL parser reads styles in file://styles/email/ as a host. css-inline instead treats styles/email/ as a path relative to the current directory. Use file:///abs/path/ for an absolute path.

To control how css-inline fetches external stylesheets, implement StylesheetResolver. Override retrieve, or only retrieve_from_url / retrieve_from_path. The trait's default unsupported method builds an InlineError::IO with std::io::ErrorKind::Unsupported for locations your resolver rejects:

Every inlining function has a non-blocking *_async counterpart behind the async feature:

Số liệu repository có thể thay đổi; nên mở liên kết nguồn để kiểm tra README, giấy phép và trạng thái phát hành.

Nguồn tham khảo: Xem bài gốc

📅 Âm: 4/10 · Canh Dần

📅 Lịch Âm Dương NsN

×
Hôm Nay · Thứ Năm
Âm Lịch: 4 Tháng 10
Năm Bính Ngọ
📌 Can Chi: Canh Dần
✨ Hoàng Đạo: Tý (23-1), Sửu (1-3), Thìn (7-9), Tỵ (9-11), Mùi (13-15), Tuất (19-21)
Vĩnh Phúc (Liên Bảo - Vĩnh Yên)
31°C
Nắng Đẹp
💧 83% | 💨 18 km/h
Hôm nay 32°
02/11 28°
03/11 30°
04/11 31°
05/11 31°