How to combine class and ID in CSS selector?(如何在 CSS 选择器中组合类和 ID?)
问题描述
如果我有以下 div:
If I have the following div:
<div class="sectionA" id="content">
Lorem Ipsum...
</div>
有没有办法定义一种样式来表达具有 id='content'
AND class='myClass'
的 div"这一理念?
Is there a way to define a style that expresses the idea "A div with id='content'
AND class='myClass'
"?
或者你只是像在
<div class="content-sectionA">
Lorem Ipsum...
</div>
或者
<div id="content-sectionA">
Lorem Ipsum...
</div>
推荐答案
在你的样式表中:
div#content.myClass
这些也可能有帮助:
div#content.myClass.aSecondClass.aThirdClass /* Won't work in IE6, but valid */
div.firstClass.secondClass /* ditto */
并且,根据您的示例:
div#content.sectionA
编辑,4 年后:因为这是超级旧的并且人们一直在寻找它:不要在你的选择器中使用 tagNames.#content.myClass
比 div#content.myClass
快,因为 tagName 添加了您不需要的过滤步骤.只在必须的地方使用选择器中的标签名!
Edit, 4 years later: Since this is super old and people keep finding it: don't use the tagNames in your selectors. #content.myClass
is faster than div#content.myClass
because the tagName adds a filtering step that you don't need. Use tagNames in selectors only where you must!
这篇关于如何在 CSS 选择器中组合类和 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 CSS 选择器中组合类和 ID?


- addEventListener 在 IE 11 中不起作用 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01