如果要将网页中H2标题和段落都设置为灰色,可以使用以下选择符群组方式:
- h2, p {color: gray;}
不同的HTML标签之间用英文逗号进行分隔,告诉浏览器这里有多个标签元素,当然如果不加这个逗号也是一种选择符群组的方式,但却是完全不同的含义,这点稍后会提到。
当CSS规则比较多的时候,选择符的群组可以很灵活,类似于数学中的交集和并集(不知道这个比喻恰不恰当,数学是偶的死穴…),可以衍生出多种不同的CSS编写方式,当然我们要从中总结最简洁的一种。书中举例相同效果的三种不同群组方式,我看着有点头晕,就不解释了:
- /* group 1 */
- h1 {color: silver; background: white;}
- h2 {color: silver; background: gray;}
- h3 {color: white; background: gray;}
- h4 {color: silver; background: white;}
- b {color: gray; background: white;}
- /* group 2 */
- h1, h2, h4 {color: silver;}
- h2, h3 {background: gray;}
- h1, h4, b {background: white;}
- h3 {color: white;}
- b {color: gray;}
- /* group 3 */
- h1, h4 {color: silver; background: white;}
- h2 {color: silver;}
- h3 {color: white;}
- h2, h3 {background: gray;}
- b {color: gray; background: white;}
《CSS选择符群组》参考资料
《CSS选择符群组》相关视频教程下载【FLV格式 – 5.36MB】
《CSS权威指南 – 第3版》撷英笔记及配套视频:Grouping Selector–选择符群组




