Inlining CSS in C#

Inlining CSS in C#

Since you're already 90% of the way there with your current implementation, why don't you use your existing framework but replace the XML parsing with an HTML parser instead? One of the more popular ones out there is the HTML Agility Pack. It supports XPath queries and even has a LINQ interface similar to the standard .NET interface provided for XML so it should be a fairly straightforward replacement.

How do I set up a C# project to inline css into HTML from a local .css file? Can PreMailer.Net do this?

This issue appears to be fixed in the just released 2.1.1 as per this GitHub issue:

Add support on fetching Uri with local filepath like
"file:///C:/website/style.css"

It's required when we optimize the performance by fetching the local
resource internally instead of making external web request towards
internal resource. e.g. As I know the css is actually inside the local
storage, it make sense to just make a fast local fetch
("file:///C:/website/style.css") instead of external web request
("https://www.example.com/style.css")

So I can now initialize a PreMailer instance with BaseUri "file:///" +
new Uri(System.Web.Hosting.HostingEnvironment.MapPath("~/"),
UriKind.Absolute)

It's how I boost the performance for my client, just share my code
here.

Escape inline css in c#

There are two choices:

addressInformation.Append("<style> p {font-family:\"Calibri\", \"Arial\";} </style>");

addressInformation.Append(@"<style> p {font-family:""Calibri"", ""Arial"";} </style>");

And of course you can also bypass the need to escape:

addressInformation.Append("<style> p {font-family:'Calibri', 'Arial';} </style>");

Or in this case, since the values are single words:

addressInformation.Append("<style> p {font-family:Calibri, Arial;} </style>");

.NET convert external CSS to inline CSS

Premailer.Net should do the trick it's written in C#

C# .Net library, that moves your stylesheets to inline style
attributes, for maximum compatibility with E-mail clients.

Here is the Git repo:
https://github.com/milkshakesoftware/PreMailer.Net:

Using C# variable in an element's inline CSS? (MVC)

First, calling color to string will return something like `Color [A=255, R=6, G=86, B=171]' so that will not work.

Instead you need to get the hex representation or use the RGB declaration

Now, assuming you are trying to output the p tag, when you are inside of a code block you can use @Html.Raw

 @Html.Raw("<p style='color: #" + randomColor.R.ToString("x2") + randomColor.G.ToString("x2") + randomColor.B.ToString("x2")  + "'>" + db.item + "</p>");

Using rgb you can do this:

@Html.Raw("<p style='color: rgb(" + randomColor.R + "," randomColor.G "," + randomColor.B +")'>" + db.item + "</p>");

How to set inline css width using model values in c#, mvc

Do it like this-

<div style="width: @(rat.someval+"px")" class="Traveler-ratingsImg"></div>

see here



Related Topics



Leave a reply



Submit