Crystal bindings for css-inline — a high-performance library for inlining CSS into HTML. Perfect for preparing HTML emails or embedding HTML into third-party web pages.
- Rust toolchain (the C library is compiled from source during installation)
Add the dependency to your shard.yml:
dependencies:
css-inline:
github: osbre/css-inlineRun shards install — this will automatically build the native library.
require"css-inline"# Inline CSS from <style> tags
html ="<html><head><style>h1 { color: blue; }</style></head><body><h1>Hello</h1></body></html>"
result =CssInline.inline(html)
# => <html><head></head><body><h1 style="color: blue;">Hello</h1></body></html># Inline a CSS fragment
result =CssInline.inline_fragment("<h1>Hello</h1>", "h1 { color: blue; }")
# => <h1 style="color: blue;">Hello</h1>inliner =CssInline::CSSInliner.new(
keep_style_tags:true,
extra_css:"h1 { font-size: 2em; }",
minify_css:true,
)
result = inliner.inline(html)All available options:
| Option | Default | Description |
|---|---|---|
inline_style_tags | true | Inline CSS from <style> tags |
keep_style_tags | false | Keep <style> tags after inlining |
keep_link_tags | false | Keep <link> tags after inlining |
keep_at_rules | false | Keep @media and other at-rules |
load_remote_stylesheets | true | Fetch and inline remote stylesheets |
minify_css | false | Minify the inlined CSS |
remove_inlined_selectors | false | Remove selectors that were inlined |
apply_width_attributes | false | Set width HTML attributes from CSS |
apply_height_attributes | false | Set height HTML attributes from CSS |
base_url | nil | Base URL for resolving relative URLs |
extra_css | nil | Additional CSS to inline |
cache | nil | Stylesheet cache (see below) |
Reuse parsed stylesheets across multiple calls:
cache =CssInline::StylesheetCache.new(size:64)
inliner =CssInline::CSSInliner.new(cache: cache)
inliner.inline(html1)
inliner.inline(html2) # reuses cached stylesheets- Fork it (https://github.com/osbre/css-inline/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
- Ostap Brehin - creator and maintainer