是否有CSS样式来选择具有任何id的元素?

时间:2016-12-31 15:53:03

标签: html css

我有html文档,我想要定位具有任何id值的h2标记,而不是没有id的目标h2。我知道用类重写html是一种可能的选择,但是现在只想改变CSS。

我想用css规则来定位以下内容:

<h2 id="one">
<h2 id="two">

但不能同时定位以下内容

<h2>

是否有像

这样的东西
h2#[*] { font-size: 12pt}

会实现这个目标吗?我在这个网站和其他人搜索过,但没有找到答案。我确定这是许多人的基本CSS,但我很难过。 谢谢,MB

2 个答案:

答案 0 :(得分:6)

使用CSS属性选择器,仅选择具有特定属性的元素,例如 - 如您的问题 - id

h1[id], h2[id] {
  /* CSS rules */
}

h1[id],
h2[id] {
  color: limegreen;
  font-size: 2em;
}
<h1>Heading level 1, with no id</h1>
<h1 id="h1">Heading level 1, with an id</h1>

<h2>Heading level 2, with no id</h1>
<h2 id="h2">Heading level 2, with an id</h1>

参考文献:

答案 1 :(得分:1)

是的,您可以在css中使用属性选择器

[id]{ /* css here */ }

这将选择 id 属性

的任何元素

您需要h2[id]

这里的工作示例http://jsbin.com/fabayaqusa/edit?html,css,output