[英]Confused about overriding CSS styles
I understand CSS basics, but I keep running into trouble with conflicting styles. 我理解CSS基础知识,但是我仍然遇到有冲突的样式的问题。 Consider the following styles.
请考虑以下样式。
First, the default font color in my style sheets is black. 首先,我的样式表中的默认字体颜色是黑色。 I want that color applied to all picture captions - unless they're contained in divs with a class CoolL or CoolR...
我希望将这种颜色应用于所有图片标题 - 除非它们包含在具有CoolL或CoolR类的div中...
.CoolL .Caption, .CoolR .Caption { color: #900; }
Now all the captions in the Cool series have brown text. 现在Cool系列中的所有标题都有棕色文字。 But there are situations where I want the captions to have a black background with white text, so I created this rule:
但有些情况下我希望字幕有黑色背景和白色文字,所以我创建了这个规则:
.Black { background: #000; color: #fff; }
Now consider the following HTML. 现在考虑以下HTML。 Class Caption by itself should have black text.
类标题本身应该有黑色文本。 However, this is inside a div with a class CoolR, so it displays brown text instead.
但是,这是一个带有CoolR类的div,所以它显示的是棕色文本。 But I added the class Black to the last div, which should change the background to black and the text color to white...
但是我把班级Black添加到最后一个div,它应该将背景改为黑色,文本颜色改为白色......
<div class="CoolR Plus Max300">
<div class="Shadow2">
<img src="">
<div class="Caption Black">Text</div>
</div>
</div>
In fact, the background is displaying black, but the text color is still brown. 事实上,背景显示黑色,但文字颜色仍然是棕色。
I get these problems all the time, and the only way I can fix them is to write long, detailed styles, like this... 我一直都会遇到这些问题,而我能解决这些问题的唯一方法就是写出详细的长款,就像这样......
.Black, .Caption .Black, .CoolR .Caption.Black, .EverythingElseThatCouldBeBlack .Black { background: #000; color: #fff; }
What am I missing? 我错过了什么? Thanks.
谢谢。
I think you are over complicating things. 我认为你过于复杂化了。 This will become a maintenance issue as you add more styles.
添加更多样式时,这将成为维护问题。 I would define separate classes and keep things simple.
我会定义单独的类并保持简单。 It's also important to understand CSS specificity.
了解CSS特异性也很重要。
.caption {
color: #000;
}
.cool-caption {
color: #900;
}
.caption-with-background {
background-color: #000;
color: #fff;
}
你可以尝试:
.Black { background: #000 !important; color: #fff !important; }
There are a few fixes, but as previously recommended you should mark all of the settings you want to override previous ones with !important
. 有一些修复,但如前所述,您应该使用
!important
标记要覆盖之前的所有设置。 With that, your code would look like this: 有了它,您的代码将如下所示:
.Black {
background: #000;
color: #fff;
}
Also, not sure if you asked this, but you can apply CSS
to all components by using the *
, like so: 此外,不确定您是否问过此问题,但您可以使用
*
将CSS
应用于所有组件,如下所示:
* {
//blahblahblah
}
you are defining the first case with a descendant selector which overrides the second class, which is merely a class. 你正在定义第一个带有后代选择器的情况,它会覆盖第二个类,它只是一个类。 every answer given already will work but are entirely unnecessary.
已经给出的每个答案都可行,但完全没必要。 just add this to your style sheet:
只需将其添加到样式表:
.CoolR1 .Black, .Black{ background: #000; color: #fff;}
/** you could also chain your classes for specificity power **/
.Black.Caption{color:#fff}
that should do it. 应该这样做。 you can read more about selectors here:
你可以在这里阅读更多关于选择器的信息
http://docs.webplatform.org/wiki/css/selectors http://docs.webplatform.org/wiki/css/selectors
I think that generally a more specific rule overrides a more general one, thus the more specific '.CoolR .Caption' is overriding the more general .Black
. 我认为通常一个更具体的规则会覆盖一个更普遍的规则,因此更具体的'.CoolR .Caption'会覆盖更普遍的
.Black
。 You'll probably be able to override this with !important
, but a better style might be to reduce the complexity of your rules: 您可能能够使用
!important
来覆盖它,但更好的风格可能是降低规则的复杂性:
.Cool .caption { color: #900; }
.Cool .caption.black { color: background: #000; color: #fff; }
And put .L
and .R
in separate classes 并将
.L
和.R
放在不同的类中
.Cool.L { . . . } /*
.Cool.L { . . . } /*
For things specific to CoolL
, but not CoolR */
.Cool.L { . . . } /*
对于特定于CoolL
,但不是CoolR */
.Cool.R { . . . } /* and vice-versa *
.Cool.R { . . . } /* and vice-versa *
/ .Cool.R { . . . } /* and vice-versa *
/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.