关于Id选择器问题的CSS问题?

时间:2010-03-04 10:32:10

标签: css css-selectors

当我在下面的代码中将它用作id选择器而不是类选择器时,如何让ba选择器工作?

这不适用于ba作为id属性。

#abc textarea#ba {
   height: 400px; 
   width: 660px;
}

这将使用ba作为类属性。

#abc textarea.ba {
   height: 400px; 
   width: 660px;
}

2 个答案:

答案 0 :(得分:4)

我不明白为什么你的例子不起作用。

您是否100%确定在ID为ba的元素中有ID为abc的textarea?请记住,name属性不计算在内。也许你在其中一个元素中有一个重复的id?此外,请记住,您可以分配多个类,但多个ID,例如id='abc def'

编辑:这对我来说很好用:

<style type="text/css">
#abc textarea#ba {
   height: 400px; 
   width: 660px;
   background-color: lightyellow;
}

</style>

<div id="abc">
  <textarea id="ba">
    I'm 400  wide, 660 tall and light yellow!
  </textarea>
</div>

答案 1 :(得分:3)

id的重点在于它们是唯一的(标识符)所以你永远不应该需要以这种方式进行选择而应该只使用

#ba {
   height: 400px; 
   width: 660px;
}

如果您有多个#ba,那么您的表现并不正确。

  

'id = name [CS]此属性指定   元素的名称。这个名字必须   在文档中是独一无二的。'

来源:http://www.w3.org/TR/html401/struct/global.html#h-7.5.2

相关问题