【问题标题】:Add Style attribute to HTML tags from code behind using C#使用 C# 将 Style 属性从后面的代码添加到 HTML 标记
【发布时间】:2011-11-16 10:04:44
【问题描述】:

大家好,

我想使用 C# 在我的 HTML 标记中添加样式属性。请找到以下代码。

            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();

输出是:

图像字体样式中显示的文字是“Time New Roman”。我怎样才能将其更改为以任何其他字体类型显示。我怎样才能在上面的 HTML 标记中添加它。

提前致谢。

【问题讨论】:

    标签: c# html styles code-behind


    【解决方案1】:
    <p><span style="font-family:Verdana">Please note:</span></p>
    

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

    【讨论】:

    • 嗨,蒂姆,当我对标签进行这样的小改动时,它就起作用了。感谢你的回答。 :)

      请注意:

    【解决方案2】:

    将样式添加到标签中,如下所示:

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

    在此处查看更多信息:http://w3schools.com/html/html_styles.asp

    【讨论】:

      【解决方案3】:
         <p style="font-family:arial black">Please note:</p> 
      

      请通过此链接了解更多信息

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

      【讨论】:

        【解决方案4】:

        更简单的方法是将它放在 CSS 中

        BODY P
        {
        }
        

        那么你就不需要把它放到所有的P标签里了

        或者如果你想让它只适用于一个部分 你应该把它放在一个 div 中,然后

        .<DivClassName> P
        {
        }
        

        【讨论】:

          【解决方案5】:

          我会这样做,所以你正在利用 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();
          

          另外请注意,您不需要使用 AppendFormat 方法,除非您计划将参数传递到嵌入在字符串中的标记({0}{1} 等)。

          【讨论】:

            猜你喜欢
            • 2014-06-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-03
            • 2011-11-26
            • 1970-01-01
            • 1970-01-01
            • 2012-01-04
            相关资源
            最近更新 更多