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

I use the html code inside the .cs file to create a template for sending emails. I have added a style tag to the <a href> tag, but it does not take effect when an email is 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>"); } 
+6
source share
5 answers

Escape quotes with a 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\" 
+6
source

The problem is how you built 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 stops here style='font-family:' , since the single quotes that open the style rules are closed ... Therefore, your other rules are not even considered in this arrtibute style.

Solution: You really don't need to wrap the font-family values ​​in quotation marks .. you can just say style='font-family:Helvetica,Arial,sans-serif;

If you have a font family name with spaces in it, you must enclose it in quotation marks.


Edit 1: If your font name has a space, you need to wrap them in quotation marks. Therefore, you need to wrap the Helvetica Neue in quotation marks. Add a double quotation mark and escape its meaning using backslash. Like below ..

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

+3
source

It's simple, define the style in your .css file as a class and assign the class here at the end. Example:

.css

 .custom-style-a { font-family:"Helvetica Neue Helvetica,Arial,sans-serif"; font-size:14px; color:#fff; text-decoration:none; line-height:2em; /*rest of styles here*/ .... } 

And the added html will be:

 sb.Append("<a href='#' class=' custom-style-a' .....>"); 

Benefits:

easy to implement, can easily support styles later. Complex formatting is not required; use styles if necessary.

+1
source

Use double quotes, but with escaped characters, for example:

 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 concatenation method:

 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>"; 
+1
source

You cannot insert single quotes in single quotes. You use the 'Helvetica Neue' inside style = '... 'Helvetica Neue' ...' . You should do it this way: style = "... 'Helvetica Neue' ..." .

Also, I see that you already have double quotes around the anchor element, so adding direct double quotes after the style will be trial. You will have to use the escape character \" instead " after the style, as Bhojendro already explained.

0
source

All Articles