CssToInlineStyles Documentation

repository·master·Indexed 26 days ago

https://github.com/tijsverkoyen/csstoinlinestyles

A 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.

Tokens
673
Snippets
3
Records
7
Agent score
41%

What's inside CssToInlineStyles

  1. Ensure UTF-8 charset detection

    master

    To 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" />
  2. Convert HTML and CSS to inline styles

    master

    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
    );
  3. Known limitations of CssToInlineStyles

    master

    When using this library, be aware of the following limitations:

    • Pseudo selectors: There is no support for pseudo-selectors (e.g., :hover, :before).
    • CSS escapes: There is no support for css-escapes.
    • Charset detection: UTF-8 charset detection requires the specific http-equiv meta-tag mentioned in the troubleshooting guide.
  4. Convert HTML and CSS to inline styles with convert()

    master

    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.