Install CssToInlineStyles via Composer
masterInstall the package using Composer to add it to your PHP project.
$ composer require tijsverkoyen/css-to-inline-stylesrepository·master·Indexed 26 days ago
https://github.com/tijsverkoyen/csstoinlinestylesA PHP library that converts HTML and CSS into HTML with inline styles, primarily for improving HTML email compatibility across different clients. It provides the CssToInlineStyles class with methods to convert content, inline specific CSS properties on DOMElements, and retrieve existing inline styles.
Install the package using Composer to add it to your PHP project.
$ composer require tijsverkoyen/css-to-inline-stylesTo ensure the UTF-8 charset is detected correctly, you must include a specific meta-tag in the <head> of your HTML. Note that the standard <meta charset="UTF-8"> is NOT supported for this purpose.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />Use the CssToInlineStyles class to convert HTML content and a separate CSS string into HTML with inline styles. This is particularly useful for email templates where external or header-based stylesheets may not be supported.
use TijsVerkoyen\
CssToInlineStyles\\nCssToInlineStyles;
// create instance
$cssToInlineStyles = new CssToInlineStyles();
$html = file_get_contents(__DIR__ . '/examples/sumo/index.htm');
$css = file_get_contents(__DIR__ . '/examples/sumo/style.css');
// output
echo $cssToInlineStyles->convert(
$html,
$css
);When using this library, be aware of the following limitations:
:hover, :before).http-equiv meta-tag mentioned in the troubleshooting guide.The convert() method takes an HTML string and an optional CSS string, then returns the HTML with the CSS rules applied as inline style attributes on the elements.
If the provided HTML contains <style> tags, the rules within those tags will be processed first, and the provided $css string will be appended to them. This ensures that the $css argument can override or supplement existing styles in the HTML.
inlineCssOnElement() to manually apply an array of Property objects to a specific ext{DOMElement}. This method will merge the new properties with any existing inline styles on the element, respecting CSS importance and specificity rules.getInlineStyles() method parses the style attribute of a given ext{DOMElement} and returns an array of Property objects representing the current inline styles.