inline-css

repository·master·Indexed 19 days ago

https://github.com/jonkemp/inline-css

A utility for inlining CSS properties directly into the style attribute of HTML elements, primarily used for preparing HTML for email clients or optimizing web performance. Version 4.0.3 includes the core inlineCss() function and supporting packages such as css-rules, extract-css, href-content, list-stylesheets, mediaquery-text, remote-content, and style-data.

Tokens
7.6K
Snippets
31
Records
37
Agent score
65%

What's inside inline-css

  1. Prevent style inlining with data-embed

    master

    To prevent extract-css from inlining styles or removing a <style></style> tag, add the data-embed attribute to the tag. This is useful for preserving CSS selectors that email clients might need for specific hacks.

    <style data-embed>
      /* This style will not be inlined or removed */
      .email-hack {
        display: block !important;
      }
    </style>
  2. Prevent inlining with the data-embed attribute

    master

    If you need to preserve specific CSS selectors (such as email client support hacks) within a <style> tag, add the data-embed attribute to that tag.

    When data-embed is present, inline-css will skip inlining the styles within that tag and will not remove the <style> tag itself.

    <style data-embed>
      /* This CSS will be preserved and not inlined */
      @media screen and (max-width: 600px) { ... }
    </style>
  3. Configure ignored code blocks in list-stylesheets

    master

    To prevent the parser from incorrectly identifying code within template tags (like Handlebars or EJS) as HTML, use the codeBlocks option.

    Important: codeBlocks is a dictionary. To avoid wiping out the default EJS and HBS configurations, you should assign your custom block to a new key rather than replacing the entire object.

    // Correct way to add a custom code block
    const options = {
      codeBlocks: {
        MY_TEMPLATE: { start: '[[', end: ']]' }
      }
    };
  4. Prevent inlining using the data-embed attribute

    master

    To prevent style-data from inlining styles or removing a specific <style> tag, add the data-embed attribute to that tag. This is useful for preserving email client support hacks that require CSS selectors to remain in the HTML.

    <style data-embed>
      /* This style will not be inlined and the tag will not be removed */
      .email-client-hack { color: red; }
    </style>
  5. Handle template languages with codeBlocks

    master

    To prevent inline-css from misinterpreting template syntax (like Handlebars {{ }} or EJS <% %>) as HTML or CSS, use the codeBlocks option. This tells the parser to ignore content within specified start and end delimiters.

    Important: codeBlocks is a dictionary. To add your own custom block, append to the existing object rather than replacing it.

    // Correct way to add a custom code block
    const options = { url: '/path/' };
    options.codeBlocks.myTemplate = { start: '[[', end: ']]' };
    
    inlineCss(html, options);