Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying style to <a href> tag placed inside C# code

Tags:

html

c#

css

asp.net

I am using html code inside a .cs file to design a template for sending emails. I have added style tag to the <a href> tag, but it does not take effect when the email is actually sent. Here is the code:

public static void Email()
{
   StringBuilder sb= new StringBuilder();
   sb.Append("<a href='#' style='font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:14px;color:#fff;text-decoration:none;line-height:2em;font-weight:bold;text-align:center;display:inline-block;border-radius:5px;text-transform:capitalize;background-color:#208ed5;margin:0;border-color:#208ed5;border-style:solid;border-width:10px 20px' target='_blank'>");
   sb.Append("link</a>");
}
like image 578
Sumedha Vangury Avatar asked Apr 25 '16 06:04

Sumedha Vangury


3 Answers

Escape the quotes with backslash:

style=\"font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;font-size:14px;color:#fff;text-decoration:none;line-height:2em;font-weight:bold;text-align:center;display:inline-block;border-radius:5px;text-transform:capitalize;background-color:#208ed5;margin:0;border-color:#208ed5;border-style:solid;border-width:10px 20px\"
like image 54
Bhojendra Rauniyar Avatar answered Nov 14 '22 04:11

Bhojendra Rauniyar


The problem is with the way you have constructed your HTML string...

In this code

"<a href='#' style='font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:14px;color:#fff;text-decoration:none;line-height:2em;font-weight:bold;text-align:center;display:inline-block;border-radius:5px;text-transform:capitalize;background-color:#208ed5;margin:0;border-color:#208ed5;border-style:solid;border-width:10px 20px' target='_blank'>"

The style attribute will stop here style='font-family:' since the single quotes which opens the style rules is closed... Hence your other rules are not even considered into this style arrtibute.

Solution: You really dont need to wrap the font-family values in quotes.. you can just say style='font-family:Helvetica,Arial,sans-serif;

If at all you have a font-family name with spaces in it then you have to wrap it in quotes.


Edit 1: If at your font-family name contains space you need to wrap them in quotes.. So you have to wrap the Helvetica Neue in quotes. Add a double quote and escape its meaning using backslash.. Like below..

style='font-family:\"Helvetica Neue\",Helvetica,Arial,sans-serif;

like image 30
Rajshekar Reddy Avatar answered Nov 14 '22 04:11

Rajshekar Reddy


Use double quotes but with escaped characters, like so:

sb.Append("<a href=\"#\" style=\"font-family:\"Helvetica Neue\",Helvetica,Arial,sans-serif;font-size:14px;color:#fff;text-decoration:none;line-height:2em;font-weight:bold;text-align:center;display:inline-block;border-radius:5px;text-transform:capitalize;background-color:#208ed5;margin:0;border-color:#208ed5;border-style:solid;border-width:10px 20px\" target=\"_blank\">");
sb.Append("link</a>");

Or you can always use the old way of concatenating:

string sb = "<a href=\"#\" style=\"font-family:\"Helvetica Neue\",Helvetica,Arial,sans-serif;font-size:14px;color:#fff;text-decoration:none;line-height:2em;font-weight:bold;text-align:center;display:inline-block;border-radius:5px;text-transform:capitalize;background-color:#208ed5;margin:0;border-color:#208ed5;border-style:solid;border-width:10px 20px\" target=\"_blank\">";
sb += "link</a>";
like image 1
Ahs N Avatar answered Nov 14 '22 05:11

Ahs N