具有类.a和类.b的元素的CSS选择器 [英] CSS selector for an element having class .a and class .b

查看:142
本文介绍了具有类.a和类.b的元素的CSS选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置同时具有类.a和类.b的元素的样式.我该怎么办?

I need to style an element that has both class .a and class .b. How do I do it?

类在HTML中出现的顺序可能会有所不同.

The order the classes appear in the HTML might vary.

<style>
    div.a ? div.b {
        color:#f00;
    }
</style>
<div class="a">text not red</div>
<div class="b">text not red</div>
<div class="a b">red text</div>
<div class="b a">red text</div>

推荐答案

那是完全可能的.如果在一个元素上指定两个类(不带空格),则意味着它必须同时具有两个类才能应用规则.

That's entirely possible. If you specify two classes on an element (without any spaces), that means that it must have both for the rule to apply.

div.a {
  color: blue;
}
div.b {
  color: green;
}
div.a.b {
  color: red;
}

<div class="a">
  A
</div>
<div class="b">
  B
</div>
<div class="a b">
  AB
</div>

这篇关于具有类.a和类.b的元素的CSS选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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