Can you target an element with CSS only if 2 classes are present?(仅当存在 2 个类时,您可以使用 CSS 定位元素吗?)
问题描述
您可能已经知道,元素上可能有多个类,以空格分隔.
As you probably already know, you may have multiple classes on elements separated by a space.
<div class="content main"></div>
而使用 CSS,您可以使用 .content
或 .main
来定位该 div
.如果且仅当两个类都存在时,有没有办法定位它?
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.
但请注意,最高版本 6 的 Internet Explorer 不支持多个类选择器,仅支持最后一个类名.
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 定位元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!