CSS:样式应用于类的组合? [英] CSS: Style applied to a combination of classes?

查看:139
本文介绍了CSS:样式应用于类的组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是可能的,但是当你想基于应用于它的类的组合来对元素进行样式时,在CSS中使用的语法吗?

I'm not sure this is possible, but is there a syntax to be used in CSS when you want to style an element based on the combination of classes applied to it?

我知道我可以使用jQuery或者其他东西来检查一个元素,并根据它的类来改变它的样式,但是有一个纯CSS的方式来做这个?

I understand that I can check an element with jQuery or something and change it's style based on the classes it has, but is there a pure CSS way to do this?

例如,如果我有一个粗体和绿色的类:

For example, if I have a class for bold and green:

.bold_green { color:green; font-weight:bold; }

和一个粗体和蓝色的类:

And a class for bold and blue:

.bold_blue { color:blue; font-weight:bold. }

现在,假设我使用jQuery动态添加和删除类,

Now, say I am using jQuery to add and remove classes dynamically and want any element that has both classes to turn italic pink.

类似:

.bold_green AND .bold_blue { color:pink; font-style:italic; }

或者,如果我想设置一个有aclass的元素,有另一个类吗?

Or, if I want to style an element that has aclass, and is a descendant of another element that has another class?

有点像:

.bold_green HAS_CHILD .bold_blue { color:black; background-color:yellow; }

感谢!

修改

感谢所有答案。这些都是我想的(只是把类作为常规选择器),但他们似乎并不是为我工作。我将不得不检查我的代码,并确保他们不被覆盖某种方式...

Thanks for all the answers. These are pretty much what I thought (just treating the classes as regular selectors), but they don't seem to be working for me. I will have to check my code and make sure they aren't being overridden somehow...

推荐答案

$('.bold_green.bold_blue').addClass('something-else');

或在CSS中:

.bold_green.bold_blue { color: pink; }

请注意,选择器之间没有空格。

Notice there's no space between the selectors.

这篇关于CSS:样式应用于类的组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆