【问题标题】:Code on lower line number is taking priority over code on a higher line. Why?较低行号上的代码优先于较高行上的代码。为什么?
【发布时间】:2016-04-01 19:22:20
【问题描述】:

我试图弄清楚为什么此代码优先于样式表中的后续代码。

/* Line 612 */
input[type='submit'], input[type='button'], button { 
    background-color: #d3dce0;
    border: 1px solid #787878;
    cursor: pointer;
    font-size: 1.2em;
    font-weight: 600;
    padding: 7px;
    margin-right: 8px;
    width: auto;
}

下面应该改变边框和背景颜色吧?

/* Line 764 */
.fakelink {
    border: 0px;
    color: blue;
    background-color: transparent;
    margin-top: 0px;
    margin-bottom: 0px;
}

目前边框和背景仍然是第 612 行描述的内容。我希望它是描述 764 的内容。有没有办法覆盖第 612 行而不更改它?

【问题讨论】:

标签: css css-selectors css-specificity


【解决方案1】:

更具体的选择器将始终优先,无论天气如何,它们高于或低于同一元素的其他规则。在您的情况下,type 属性被视为比 class 更具体的选择器,因此它具有优先权。

在此处进一步阅读: https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity

您可以通过在您需要优先级的规则上使用相同级别的特异性来解决此问题。在你的情况下,这样的事情应该可以工作:

input[type='submit'].fakelink{ ... }

【讨论】:

  • 太好了 - 很高兴为您提供帮助!
【解决方案2】:

你可以使用!important:

.fakelink {
    border: 0px !important;
    color: blue !important;
    background-color: transparent !important;
    margin-top: 0px !important;
    margin-bottom: 0px !important;
}

【讨论】:

  • 错误代码。想用自然的方式来点东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-22
  • 1970-01-01
相关资源
最近更新 更多