4.关系选择符
包含选择符(Descendant combinator)
E F 选择所有被E元素包含的F元素
<style type="text/css"> h1 a { color: red; font-size: 30px; font-family: "微软雅黑"; text-decoration: none; } </style>
子选择符(Child combinator)
E>F 选择所有被E元素的子元素F
<style type="text/css">
/* none 无 */ .nav > li { list-style: none; } </style>
相邻选择符(Adjacentsibling combinator)
E+F 选择紧贴在E元素之后的F元素
<style type="text/css"> h3 + small { color: red; } </style>
兄弟选择符(General sibling combinator)
E~F 选择E元素所有兄弟元素F
<style type="text/css"> h3 ~ p { color: red; } </style>
5.id及class选择符
id及class均是CSS提供由用户自定义标签名称的选择符,用户可以使用选择符id及class来对页面中的XTML标签进行自定义名称,从而达到扩展XTML标签及组合HTML标签的目的。
id选择器可以为标有特定的id的HTML元素指定特定的样式。
id选择器以“#”来定义。class选择器以“.”来定义。
在网页中,每个id名称中只能使用一次,不得重复。与id不同,class允许重复使用。比如页面中的多个元素,都可以使用同一个样式定义。
id选择符
<style type="text/css"> #p1 { font-size:12px; font-weight:bold; } </style>
class选择符
<style type="text/css"> .p1 {
font-size:12px;
font-weight:bold;
} </style>
6.伪类选择符
选择符 | 版本 | 描述 |
E:link | CSS1 | 设置超链接a在未被访问前的样式 |
E:visited | CSS1 | 设置超链接a在其链接地址已被访问过时的样式 |
E:hover | CSS1/2 | 设置元素在其鼠标悬停时的样式 |
E:active | CSS1/2 | 设置元素在被用户激活时的样式 |
E:focus | CSS1/2 | 设置元素在成为输入焦点时的样式 |
E:lang(fr) | CSS2 | 匹配使用特殊语言的E元素。很少用 |
E:not(s) | CSS3 | 匹配不含有s选择符的元素E |
E:root | CSS3 | 匹配E元素在文档的根元素。常指html元素 |
E:first-child | CSS2 | 匹配父元素的第一个子元素E |
E:last-child | CSS3 | 匹配父元素的最后一个子元素E |
E:only-child | CSS3 | 匹配父元素仅有的一个子元素E |
E:nth-child(n) | CSS3 | 匹配父元素的第n个子元素E |
E:nth-last-child(n) | CSS3 | 匹配父元素的倒数第n个子元素E |
E:first-of-type | CSS3 | 匹配同类型中的第一个同级兄弟元素E |
E:last-of-type | CSS3 | 匹配同类型中的最后一个同级兄弟元素E |
E:only-of-type | CSS3 | 匹配同类型中的唯一的一个同级兄弟元素E |
E:nth-of-type(n) | CSS3 | 匹配同类型中的第n个同级兄弟元素E |
E:nth-last-of-type(n) | CSS3 | 匹配同类型中的倒数第n个同级兄弟元素E |
E:empty | CSS3 | 匹配没有任何子元素(包括text节点)的元素E |
E:checked | CSS3 | 匹配用户界面上处于选中状态的元素E |
E:enabled | CSS3 | 匹配用户界面上处于可用状态的元素E |
E:disabled | CSS3 | 匹配用户界面上处于禁用状态的元素E |
E:target | CSS3 | 匹配相关URL指向的元素E |