代码之家  ›  专栏  ›  技术社区  ›  john Gu

我的自定义站点。我的asp中的css。net mvc-5没有覆盖内置样式

  •  0
  • john Gu  · 技术社区  · 10 年前

    我正在做一个asp。net mvc-5网络应用程序。我的共享布局视图中有以下内容:-

    <link id="bs-css" href="~/Content/css/bootstrap-cerulean.css" rel="stylesheet">
    
    <link href="~/Content/css/charisma-app.css" rel="stylesheet">
    <link href="~/Content/css/bootstrap-responsive.css" rel="stylesheet">
    <link href="~/Content/start/jquery-ui-1.10.3.custom.min.css" rel="stylesheet" />
    @Styles.Render("~/Content/templete")
    

    因此,我主要是先参考内置的bootstrap.css文件,然后再参考捆绑包 @Styles.Render("~/Content/templete") 其中包含我们的site.css。所以这意味着站点。css应该覆盖内置的.css文件,因为我引用的是站点。css在他们后面。。。

    现在我面临的问题是,我在我的site.css中定义了以下样式:-

    .input-validation-error {
        border: 1px solid #ff0000;
        background-color: #ffeeee;
    }
    

    如果输入有任何验证错误,则显示红色边框。现在在我的应用程序中,我不会得到如下错误行为:-

    enter image description here

    如图所示,与下拉字段不同,输入字段将没有任何红色边框。现在,如firebug所示,以下css规则在引导程序cerulean中定义。css将覆盖站点。css样式(尽管我在引导程序cerulean.css之后引用了site.css):-

    textarea, input[type="text"], input[type="password"], input[type="datetime"], input[type="datetime-local"], input[type="date"], input[type="month"], input[type="time"], input[type="week"], input[type="number"], input[type="email"], input[type="url"], input[type="search"], input[type="tel"], input[type="color"], .uneditable-input {
        background-color: #ffffff;
        border: 1px solid #cccccc;
        border-radius: 3px;
        box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset;
        transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
    }
    select, textarea, input[type="text"], input[type="password"], input[type="datetime"], input[type="datetime-local"], input[type="date"], input[type="month"], input[type="time"], input[type="week"], input[type="number"], input[type="email"], input[type="url"], input[type="search"], input[type="tel"], input[type="color"], .uneditable-input {
        color: #555555;
        display: inline-block;
        font-size: 13px;
        height: 18px;
        line-height: 18px;
        margin-bottom: 9px;
        padding: 4px;
    }
    

    那么,有人能告诉我如何强制使用这种样式,以对输入字段产生预期效果吗?

    .输入验证错误{
    边框:1px实心#ff0000;
    背景色:#ffeeee;
    }
    
    1 回复  |  直到 10 年前
        1
  •  2
  •   Joseph Marikle    10 年前

    属性选择器比类选择器具有更高的特异性。即使您的类选择器在样式表的后面,它仍然被文件中更高的特定属性扇区覆盖。

    input[type=text] {
      background: #FFF;
      border: 1px solid #DDD;
    }
    
    .error {
      background: rgba(255,0,0,0.3);
      border: 1px solid #F88;
    }
    <input class="error" type="text">

    尝试将属性部分添加到错误类中,以增加其特殊性。

    input[type=text] {
      background: #FFF;
      border: 1px solid #DDD;
    }
    
    input[type=text].error {
      background: rgba(255,0,0,0.3);
      border: 1px solid #F88;
    }
    <input class=“error”type=“text”>

    我没有读过,但这篇文章看起来很不错: https://css-tricks.com/specifics-on-css-specificity/