为了更好地理解,通过高级CSS选择器为类提供优先级是否有时好?

时间:2011-04-18 14:40:14

标签: css css3

例如我的代码就是这个。我只在#contacts div下面有 2 项。

#contacts {list-style:none; overflow:hidden; margin-left:7px; margin-bottom:10px}
.address { float:left}
.tel { float:right}

我也可以用这个

做同样的事情
#contacts {list-style:none; overflow:hidden; margin-left:7px; margin-bottom:10px}
#contacts span:first-child { float:left}
#contacts span:last-child { float:left}

2 个答案:

答案 0 :(得分:2)

而是使用

#contacts .address {...}
#contacts .tel {...}
如果你需要漂浮

,那么

甚至更好

#contacts .address, #contacts .tel {float:left;}

如果有一天你必须添加.fax,你的工作要少得多。此外,我认为对于一个程序员来说,要明白这一点会更加清晰。

答案 1 :(得分:1)

如果孩子不传达具体信息,:first-child:last-child伪元素是有意义的。如果子元素(如本例所示)确实传达了特定的细节,那么类(或更好的,HTML5 microdata attributes)更适合。