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?


基础教程推荐
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01