【问题标题】:How to override CSS for TextBoxFor field?如何覆盖 TextBoxFor 字段的 CSS?
【发布时间】:2013-10-23 14:16:06
【问题描述】:

在 Site.css 中有以下类:

input[type="textbox"].TextBoxAsLabel {
     background:#f2f3f3  !IMPORTANT;
}

想要覆盖 Edit.cshtml 中 TextBoxFor 字段的默认背景

@Html.TextBoxFor(model => model.ClientNumber, new {@readonly = "readonly", @class = "TextBoxAsLabel"})

它仍然使用默认的白色背景。我应该改变什么?

【问题讨论】:

  • 你用的是什么浏览器?

标签: html css asp.net-mvc html.textboxfor html.textbox


【解决方案1】:

没有input[type="textbox"]
你的意思是text

【讨论】:

  • 改为:input[type="text"].TextBoxAsLabel { background:#f2f3f3 !important; } 这仍然不起作用
  • @user2911759:使用浏览器的检查器找出原因。
  • 检查员说: 没有背景信息.我应该改变什么?
  • 更改您的 CSS。看到我回答的结尾
  • @Malachi:从 Malachi 的答案末尾尝试摘录“请尝试这个”。没有帮助 - 同样的效果
【解决方案2】:

我意识到问题出在 CSS 文件的位置。现在在单独的样式中编写代码。CSS 是:

  .message-label {
    background-color: #f2f3f3;
  }

Styles.css 在 _Layout.cshtml 中被引用:

 <link href="@Url.Content("~/Content/styles.css")" rel="stylesheet" type="text/css" />

在 CHTML 中:

 @Html.TextBoxFor(model => model.ClientNumber, new {@readonly = "readonly", @class ="message-label"})

它有效!

【讨论】:

    【解决方案3】:
    input[type=text].TextBoxAsLabel
    {
       background:#f2f3f3 !important;
    }
    

    如果有覆盖,则使用内联

    @Html.TextBoxFor(model => model.ClientNumber, new {@readonly = "readonly",
                  @class = "TextBoxAsLabel", @style='background:#f2f3f3 !important'})
    

    【讨论】:

    • @Malachi 没有说要删除 type=text 中的双引号。我试了一下
    • 更改为: input[type=text].TextBoxAsLabel { background-color:#f2f3f3 !IMPORTANT; } 这也没有帮助
    • 您需要使用“!important”,全部小写。不是“!重要”
    • @rossisdead,我只为important使用小写
    • 更改为: input[type=text].TextBoxAsLabel { background-color:#f2f3f3 !important; } 仍然无法正常工作
    【解决方案4】:

    当您处理 CSS 时,您必须记住,应用于该元素的最后一件事就是显示为最终产品的内容。

    CSS 从上到下,最后才是最终结果。

    order of precedence

    在此站点中,您似乎可以使用 !Important!important

    但它没有说明所有的大写字母

    另一个关于!important 覆盖的Question,答案谈到给元素一个ID 或做一些&lt;style&gt; 标签来实现这种风格。

    加法

    我假设最上面的代码集是您的 CSS。在 CSS 中你需要使用选择器,比如如果你有一个类似的标签

    <input type="button" id="btn1" />
    

    然后你用

    选择它
    #btn1
    

    你通过 ID 抓取

    如果你有

    <input type="button" class="btnClass" />
    

    然后您可以像这样选择带有btnClass 的所有按钮

    input.btnClass
    {
    }
    

    请试试这个

    input.TextBoxAsLabel {
        background:#f2f3f3  !IMPORTANT;
    }
    

    #ClientNumber input {
        background:#f2f3f3  !IMPORTANT;
    }
    

    我认为语法是正确的

    还有

    这非常简单,如果这是答案,我们都将把头放在手中

    CSS 应该是

    background-color:#f2f3f3 !Important
    

    而不是

    background:#f2f3f3 !Important
    

    不确定这是否是问题的一部分,但我也会尝试从选择器中删除 [type=text]

    【讨论】:

    • 改为:input[type="text"].TextBoxAsLabel { background:#f2f3f3 !important; } 这仍然不起作用
    • 进行了更改以尝试“请尝试此”的摘录。没有帮助 - 同样的效果
    猜你喜欢
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 2012-12-17
    • 2011-10-17
    • 1970-01-01
    • 2016-06-29
    相关资源
    最近更新 更多