Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Compile" CSS into HTML as inline styles

I am writing an e-mail HTML template, and some e-mail clients do not support <style> for specifying CSS. The only alternative for applying CSS is to use inline styles (style attribute). Is there a tool or library (Node.JS) for applying a stylesheet to some HTML and getting back the HTML with the styles applied?

The tool does not have to support many selectors; id, class, and element name selectors should be sufficient for my needs.

Example of what is needed:

// stylesheet.css a { color: red; }  // email.html <p>This is a <a href="http://example.com/">test</a></p>  // Expected result <p>This is a <a href="http://example.com/" style="color: red;">test</a></p> 
like image 747
strager Avatar asked Feb 04 '11 02:02

strager


People also ask

How do you create an inline CSS in HTML?

CSS can be added to HTML documents in 3 ways: Inline - by using the style attribute inside HTML elements. Internal - by using a <style> element in the <head> section.

What is inline style of CSS in HTML?

Joel Olawanle. Cascading Style Sheets (CSS) is a markup language that determines how your web pages will appear. It manages the colors, fonts, and layouts of your website elements, as well as allowing you to add effects or animations to your pages.

Why inline CSS is not recommendable?

Inline styles, while they have a purpose, generally are not the best way to maintain your website. They go against every one of the best practices: Inline styles don't separate content from design: Inline styles are exactly the same as embedded font and other clunky design tags that modern developers rail against.

How do you inline in HTML?

The <span> element is an inline container used to mark up a part of a text, or a part of a document. The <span> element has no required attributes, but style , class and id are common.


2 Answers

Here's the alive javascript projects that does what you want:

  • juice. 1.7Mb with dependencies.
  • juice2. 5.9Mb with dependencies. This is a fork of juice, seems to be containing more options than juice. This one doesn't drop media queries as juice does. Sorts inline css rules alphabetically.
  • styliner. 4.0Mb with dependencies. This one uses promises instead. Have a couple of different options than juice2. Has a compact option that other ones don't have that minifies the html. Doesn't read the html file itself as others do. Also extends margin and padding shorthands. And in case you somehow modify your native objects(like if you are using sugar) I don't suggest using this till this issue is resolved.

So which one to use? Well it depends on the way you write CSS. They each have different support for edge cases. Better check each and do some tests to understand perfectly.

like image 24
Farid Nouri Neshat Avatar answered Sep 18 '22 19:09

Farid Nouri Neshat


I think juice is what you're looking for.

Simply require it, then pass it your html and css and let it do the heavy lifting for you like this:

var juice = require('juice'); var inlinedcss = juice('<p>Test</p>', 'p { color: red; }'); 

It builds on a number of mature libraries including mootools' slick, and supports a broad range of selectors.

You may also be interested in node-email-templates, which is a nice wrapper for dynamic emails in node.

like image 57
Jed Watson Avatar answered Sep 21 '22 19:09

Jed Watson