CSS 覆盖css样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19302877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overriding css style?
提问by Miomir Dancevic
I look on Stack Overflow, and didn't find the solution, I know how to override style if style exists, just change its property. But now I have a strange style to override
我查看堆栈溢出,并没有找到解决方案,如果样式存在,我知道如何覆盖样式,只需更改其属性即可。但现在我有一个奇怪的风格要覆盖
Here is an example of what I have
这是我所拥有的一个例子
First I have this one:
首先我有这个:
.slikezamenjanje img{
max-width: 100%;
max-height:150px;
padding-right:7px;
}
Now I need to override that style with just this one:
现在我需要用这个来覆盖那个样式:
#zoomTarget .slikezamenjanje img {
max-width: 100%;
}
The problem is that first style appends second, but I don't want that, in this second style what I need is just one line, not to append from the first style?
问题是第一种样式附加第二,但我不希望那样,在第二种样式中,我需要的只是一行,而不是从第一种样式附加?
回答by Dvir
Instead of override you can add another class to the element and then you have an extra abilities. for example:
您可以向元素添加另一个类,而不是覆盖,然后您就有了额外的能力。例如:
HTML
HTML
<div class="style1 style2"></div>
CSS
CSS
//only style for the first stylesheet
.style1 {
width: 100%;
}
//only style for second stylesheet
.style2 {
width: 50%;
}
//override all
.style1.style2 {
width: 70%;
}
回答by dthree
You just have to reset the values you don't wantto their defaults. No need to get into a mess by using !important
.
您只需将不需要的值重置为默认值。无需使用!important
.
#zoomTarget .slikezamenjanje img {
max-height: auto;
padding-right: 0px;
}
Hatting
帽子
I think the key datum you are missing is that CSS comes with default values. If you want to override a value, set it back to its default, which you can look up.
我认为您缺少的关键数据是 CSS 带有默认值。如果您想覆盖某个值,请将其设置回其默认值,您可以查找该值。
For example, all CSS height
and width
attributes default to auto
.
例如,所有 CSSheight
和width
属性默认为auto
.