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?
基础教程推荐
- fetch 是否支持原生多文件上传? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
