文本颜色格式Razor View Engine C#

问题描述 投票:0回答:1

我正在学习C#,并且对我正在研究的问题有要求。任务状态:

如果数组中的字符串以字母x开头,请用红色文本设置<p>元素的样式。

我该怎么做?我了解如何设置

       <div>
            <p>@word</p>
            @if(word.Length <4) <!--anything under 4 char will be known as a short word.-->
            {
            <p>@word is a short word</p>
            }
            @elseif(word <!--beginning with c should be red-->) <!-- I don't know if "elseif" is usable or appropriate here-->
            {
            <p>@word <!-- but you know, in red--></p>
            }
      </div>

我很抱歉,如果有人问我,我搜索了却没有找到任何东西。

c# razorengine
1个回答
0
投票

我假设您在这里寻求一些CSS建议。样式是使用CSS https://developer.mozilla.org/en-US/docs/Web/CSS/color完成的。您可以设置内联样式,但一般而言,应始终考虑使用CSS类。

// this normally goes into the `<head>` section of your view
<style type="text/css">
    .long-word {
        color: red
    }
</style>
// ..........................
   <div>
        <p>@word</p> 
        @if(word.Length <4) <!--anything under 4 char will be known as a short word.-->
        {
        <p>@word is a short word</p>
        }
        @elseif(word.StartsWith("x")) <!-- this is your check-->
        {
        <p class="long-word">@word</p> <!-- and this is where you apply your style  -->
        }
  </div>
© www.soinside.com 2019 - 2024. All rights reserved.