简体   繁体   中英

Add Style attribute to HTML tags from code behind using C#

在此处输入图片说明

Hi all,

I want to add style attributes in my HTML tags using C#. Please find the below code.

            StringBuilder mailBody = new StringBuilder();
            mailBody.AppendFormat("<html>");
            mailBody.AppendFormat("<p>Please note:</p>");
            mailBody.AppendFormat("<p> " + data + " folder no longer exists on the network. Please review at your earliest convenience. </p>");
            mailBody.AppendFormat("<br/>");
            mailBody.AppendFormat("<p>Thank you</p>");
            mailBody.AppendFormat("<p>Development Team</p>");
            mailBody.AppendFormat("</html>");
            emailBody = mailBody.ToString();

And the output is :

The text displayed in the image font style is "Time New Roman". How could I change that to display in any other font type. How could I add that in above HTML tags.

Thanks in advance.

Add the style to the tag, like this:

<p style="font-family:courier">Please note:</p>

Check for more info here: http://w3schools.com/html/html_styles.asp

<p><span style="font-family:Verdana">Please note:</span></p>

http://www.w3schools.com/html/html_styles.asp

   <p style="font-family:arial black">Please note:</p> 

pls go through this link for more info

http://w3schools.com/html/html_styles.asp

I would do this, so you're taking advantage of CSS:

var mailBody = new StringBuilder();

// put in the font(s) you'd like to use. If font 1 isn't installed,
// it will move on to the next font in line, and so forth.
var font = "Arial, Calibri, 'Trebuchet MS', sans-serif";

// the color of the text. If you'd like to use more colors, take
// advantage of CSS classes.
var color = "red";

mailBody.Append("<html><head>");
mailBody.Append("<style type=\"text/css\">");
mailBody.AppendFormat("body { font-family: {0}; color: {1}; }", 
        font, color);
mailBody.Append("</style>");
mailBody.Append("<p>Please note:</p>");
mailBody.AppendFormat("<p>{0} folder no longer exists on the network. Please review at your earliest convenience.</p>", 
        data);
mailBody.Append("<br/>");
mailBody.Append("<p>Thank you</p>");
mailBody.Append("<p>Development Team</p>");
mailBody.Append("</html>");
emailBody = mailBody.ToString();

Also note, you don't need to use the AppendFormat method unless you plan on passing in parameters into the tokens embedded in the string ( {0} , {1} etc).

An even easier way would be to put it in the CSS

BODY P
{
}

Then you wouldnt need to put it into all the P tags

Or if you want to make it only apply to a section You should surround this in a div and then

.<DivClassName> P
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM