Apparently email clients strip out style tags (stylesheets) if included
in pretty, HTML emails. This is painful for any email template designer
as that means to ensure that the actual design is maintained, the CSS
styles have to be written inline.
Ouch!
The ideal scenario would be:
After seeing what MailChimp was offering, I figured maybe I could do the same thing using Javascript and jQuery, and open it up.
Conceptually, it's pretty straight forward.
First, you want to create a new document 'styleSheet' object. Using native Javascript, you can construct the new stylessheet using: document.createElement('style'). Since we want to allow the user to include HTML as well as their styles within a style tag, we will create a new window to process the document within and append the stylesheet we're creating.
Place the styles desired to be applied, into a stylesheet. Then loop through all the cssRules and get the 'selectorText' field as part of the native cssRule object. This selector text can be applied to what jQuery is notorious for thanks to Sizzle, as selector expressions. Passing this selector text into the jQuery object, will return an array of the various different dom elements to have the styles applied to.
Once the array of elements (to be modified) are returned, put the styles into an 'Object' so that it can then be applied to the array of elements by using the handy jQuery css method, jQuery.css({prop:value, _prop:_value}); Once applied, the styles will appear inline.
Grab the inline HTML within the window and then return it to the user by injecting it somwhere in the dom. Make sure to sanitize the "style" tag, and then the HTML should be all set.
Copy and paste your HTML snippet that you want to apply the inline styles to.
Note: You must put the stylesheet in ONE style tag, and define type='text/css'
You must include the <html>, <head> and <body> tags
Here you go. Below is your HTML with inline styles.
Nirvana Tikku, Tikku.com © 2010-2013