只有当存在2个类时,你能用CSS定位一个元素吗? [英] Can you target an element with CSS only if 2 classes are present?
问题描述
您可能已经知道,您可以在由空格分隔的元素上有多个类。
As you probably already know, you may have multiple classes on elements separated by a space.
<div class="content main"></div>
使用CSS,您可以定位 div
与 .content
或 .main
。
And with CSS you can target that div
with either .content
or .main
. Is there a way to target it if and only if both classes are present?
<div class="content main">I want this div</div>
<div class="content">I don't care about this one</div>
<div class="main">I don't want this</div>
我将使用哪个CSS选择器获取第一个 div
仅(假设我不能使用 .content:first-child
或类似)?
Which CSS selector would I use to get the first div
only (assume I can't use .content:first-child
or similar)?
推荐答案
是的,只是连接他们: .content.main
。请参见 CSS类选择器。
Yes, just concatenate them: .content.main
. See CSS class selector.
但请注意,Internet Explorer最高版本6不支持多个类选择器,只支持最后一个类名。
But note that the Internet Explorer up to version 6 doesn’t support multiple class selectors and just honors the last class name.
这篇关于只有当存在2个类时,你能用CSS定位一个元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!