简体   繁体   English

CSS类重写属性以使用另一个类的值

[英]css class override property to use the value from another class

I have two predefined classes (btn, fc-btn) 我有两个预定义的类(btn,fc-btn)

Both define the value for background-color and other properties: 两者都定义了background-color和其他属性的值:

.btn {
    background-color: red;
    ....
}

.fc-btn {
    background-color: blue;
    ....
}

Now I have an element that uses both classes: 现在,我有一个使用这两个类的元素:

<button class="btn fc-btn">Action</button>

Is there a way to make sure that the fc-btn background-color is ignored and the value from btn is used (regardless of selector hierarchy). 有没有一种方法可以确保忽略fc-btn background-color并使用btn中的值(与选择器层次结构无关)。

eg override the fc-btn property. 例如,覆盖fc-btn属性。

.fc-btn {
        background-color: unset/inherit/???;
}

Problem: unset seems to also unset the value from btn inherit does not work because btn is not a class of the parent but of itself 问题:未设置似乎还取消了btn继承的值,因为btn不是父类的类,而是btn的类

Comment: I know that I could just remove the background-color from the original fc-btn class, but it comes from an external library and I would rather override it 评论:我知道我可以从原始fc-btn类中删除background-color,但是它来自外部库,我想重写它

Add a new stlye with .btn.fc-btn 使用.btn.fc-btn添加新的.btn.fc-btn

.btn.fc-btn{
  background-color: red;
}

or you can add !important to background in .btn style which is not recommended, 或者您也可以不推荐将!important添加到.btn样式的background中,

.btn {
    background-color: red !important;
    ....
}

Or If you can't modify CSS then set an inline style to the button 或者,如果您不能修改CSS,则将内嵌样式设置为按钮

<button class="btn fc-btn" style="background:red">Action</button>

You can add !important to the red background color 您可以在红色背景色中添加!important

https://jsfiddle.net/r6yp9wof/ https://jsfiddle.net/r6yp9wof/

.btn {
    background-color: red!important;
}

@Chris In your css file place your ".btn " class below to ".fc-btn" class. @Chris在您的css文件中,将“ .btn”类放在“ .fc-btn”类下面。 A browser will automatically give priority to ".btn" class because it reads from the bottom. 浏览器将从底部开始自动为“ .btn”类赋予优先级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM