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 定位元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:仅当存在 2 个类时,您可以使用 CSS 定位元素吗
基础教程推荐
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
