Class CssInliner
java.lang.Object
dev.jcputney.mjml.css.CssInliner
Inlines CSS styles into HTML elements' style attributes.
This is a standalone utility that can be used independently of the MJML renderer. It operates
on the final HTML output, matching CSS rules to elements and merging styles into inline
style="" attributes.
Rules with pseudo-classes (:hover), pseudo-elements (::before), @media
queries, @font-face, and @keyframes are preserved in <style> blocks
rather than being inlined.
Usage
// Inline styles from <style> blocks in the HTML
String inlined = CssInliner.inline(html);
// Inline additional CSS alongside <style> blocks
String inlined = CssInliner.inline(html, additionalCss);
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringInlines CSS from<style>blocks in the HTML into element style attributes.static StringInlines CSS from<style>blocks and additional CSS into element style attributes.static StringinlineAdditionalOnly(String html, HtmlElement root, String css) Inlines only the provided CSS into HTML elements' style attributes using a pre-parsed element tree, avoiding redundant HTML re-parsing when the tree is already available.static StringinlineAdditionalOnly(String html, String css) Inlines only the provided CSS into HTML elements' style attributes, without extracting or removing any existing<style>blocks from the HTML.
-
Method Details
-
inline
-
inline
-
inlineAdditionalOnly
Inlines only the provided CSS into HTML elements' style attributes, without extracting or removing any existing<style>blocks from the HTML. This is used by the MJML renderer for<mj-style inline="inline">content, where base CSS resets in the head must be preserved.- Parameters:
html- the HTML stringcss- the CSS to inline- Returns:
- the HTML with the provided CSS inlined into matching elements
-
inlineAdditionalOnly
Inlines only the provided CSS into HTML elements' style attributes using a pre-parsed element tree, avoiding redundant HTML re-parsing when the tree is already available.- Parameters:
html- the original HTML string (for position-based modification)root- the pre-parsed element treecss- the CSS to inline- Returns:
- the HTML with the provided CSS inlined into matching elements
-