RickStrahl / Westwind.HtmlPackager

A small utility class used to package HTML content into a self contained HTML document both as a single file, or a folder with all dependencies copied to local.
35 stars 9 forks source link

Consider adding an option for CSS styles to be injected directly into HTML elements #1

Closed j2jensen closed 5 years ago

j2jensen commented 5 years ago

First let me say I really like that you've created this library and made it available. The documentation was clear and the examples were easy to adapt. I can imagine a lot of scenarios where this will be useful to people.

One use case that this library gets about half of the way to supporting is writing HTML for emails. This is a notoriously annoying thing to have to do because if you want your HTML to display correctly in browser-based email clients like gmail, you can't use CSS styling defined in the head of the document. Instead, you have to put inline styles on each HTML element.

I don't know how hard it would be with the tools you're already using here, but it seems a logical next-step feature would be to provide an optional mode where CSS selectors that match a given element have their styles embedded in the element's style property.

RickStrahl commented 5 years ago

I don't know - it seems to me this appears to be much less of a problem than it used to be. The SO post is from 2009 with the most recent responses from 2011 - a bit dated.

The article linked actually links to a reference that's been kept up to date. Check this:

https://www.campaignmonitor.com/css/style-element/style-in-head/

Looks like most modern mail clients do allow style both in the head and body of the document.

Ultimately though it seems to me if you're building for an email client specifically you should explicitly make sure you use minimal styling and perhaps explicitly put your styling into elements in the first place.

Jogai commented 5 years ago

There is a library for it: https://github.com/milkshakesoftware/PreMailer.Net

So if you use this in your own project it seems easy to integrate both.

j2jensen commented 5 years ago

@Jogai That is a great find! I've got a co-worker who was struggling with this recently. I'm not sure why the style element wasn't working for him, given what Rick said. I put together a quick proof-of-concept LINQPad script that combines these two technologies, and it works very smoothly!

I agree with Rick that when building HTML for emails the ideal approach would be to carefully craft it to have simple styling and limited size. But there are business scenarios where it's probably not worth that effort.

I'm going to mark this issue closed since PreMailer.Net makes this possible. Composition FTW.