深入理解CSS伪类

来源:互联网 发布:ug星空自动编程教程 编辑:程序博客网 时间:2024/05/23 01:58

锚点

  关于锚点<a>,有常见的5个伪类,分别是:link,:hover,:active,:focus,:visited

a:link{background-color:pink;}/*品红,未访问*/

a:hover{background-color:lightblue;}/*浅蓝,鼠标悬停*/

a:active{background-color:lightgreen;}/*浅绿,正被点击*/

a:focus{background-color:lightgrey;}/*浅灰,拥有焦点*/

a:visited{color:orange;}/*字体颜色为橙色,已被访问*//*[注意]visited伪类只能设置字体颜色、边框颜色、outline颜色的样式*/

伪类顺序

  对于伪类顺序,有一个口诀是love-hate,代表着伪类的顺序是link、visited、focus、hover、active。但是否伪类的顺序只能如此呢?为什么是这个顺序呢?

  CSS层叠中有一条法则十分重要,就是后面覆盖前面,所以伪类的顺序是需要精心考虑的。

  【1】link和visited必须在最前面,且没有先后顺序,否则link或visited的效果将被覆盖

  [注意]link和visited称为静态伪类,只能应用于超链接

  【2】hover、active、focus这三个伪类必须是focus、hover、active的顺序,因为在focus状态下,也需要触发hover和active,而要触发active一定要先触发hover,所以active要放在hover后面

  [注意]hover、active、focus称为动态伪类,可应用于任何元素,但IE7-浏览器不支持:focus,:hover和:active在IE6-浏览器下只支持给<a>设置

  所以最终的顺序只有两种:link、visited、focus、hover、active或visited、link、focus、hover、active

a:link{background-color:pink;}/*品红,未访问*/a:visited{color:orange;}/*字体颜色为橙色,已被访问*/a:focus{background-color:lightgrey;}/*浅灰,拥有焦点*/a:hover{background-color:lightblue;}/*浅蓝,鼠标悬停*/a:active{background-color:lightgreen;}/*浅绿,正被点击*/

 

UI元素伪类

  UI元素伪类包括:enabled、:disabled、:checked三个,主要针对于HTML中的form元素,IE8-浏览器不支持

:enabled    可用状态:disabled   不可用状态:checked    选中状态    
复制代码
input:enabled{    border: 1px solid black;    background-color: transparent;}input:disabled{    border: none;    background-color: gray;}input:checked{    outline: 2px solid lightblue;}
复制代码
复制代码
<button onclick = "btn.disabled = false;">按钮可用</button><button onclick = "btn.disabled = true;">按钮不可用</button><input type="button" id="btn" value="按钮"><br><label>Male<input type="radio" name="sex" /></label><label>Female<input type="radio" name="sex"  /></label>
复制代码

  [注意]input和:和enabled之间都不可以有空格

结构伪类

  结构伪类可分为以下3种情况,IE8-浏览器不支持

  //以下情况都是E为父元素,F为子元素

【1】:nth-child(n)、:nth-last-child(n)、first-child、last-child、:only-child

E F:nth-child(n)           选择父元素的第n个子元素E F:nth-last-child(n)      选择父元素的倒数第n个子元素E F:first-child            父元素的第一个子元素,与E F:nth-child(1)等同E F:last-child             父元素的最后一个子元素,与E F:nth-last-child(1)等同E F:only-child             选择父元素中只包含一个子元素

  [注意]:first-child和:last-child只有IE6-浏览器不支持

  n可以是整数(从1开始),也可以是公式,也可以是关键字(even、odd)

p:first-child       代表的并不是<p>的第一个子元素,而是<p>元素是某元素的第一个子元素p > i:first-child    匹配所有<p>元素中的第一个<i>元素p:first-child i     匹配所有作为第一个子元素的<p>元素中的所有<i>元素
li:nth-child(odd){color: red;} li:nth-last-child(3){color: green;}li:first-child{color: blue;}li:last-child{color: yellow;}div:only-child{background-color:lightgrey;}
复制代码
<ul>    <li><div>第一个DIV</div></li>    <li><div>第二个DIV</div></li>    <li><div>第三个DIV</div></li>    <li><div>第四个DIV</div></li>    <li><div>第五个DIV</div></li>    <li><div>第六个DIV</div></li>        </ul>
复制代码

【2】:nth-of-type(n)、:nth-last-of-type(n)、:first-of-type、:last-of-type、:only-of-type

E F:nth-of-type(n)          选择父元素的具有指定类型的第n个子元素E F:nth-last-of-type(n)     选择父元素的具有指定类型的倒数第n个子元素E F:first-of-type           选择父元素中具有指定类型的第1个子元素,与E F:nth-of-type(1)相同E F:last-of-type            选择父元素中具有指定类型的最后1个子元素,与E F:nth-last-of-type(1)相同E F:only-of-type           选择父元素中只包含一个同类型的子元素
.box div:nth-of-type(even){color: red;} .box p:nth-last-of-type(3){color: green;}.box div:first-of-type{color: blue;}.box p:last-of-type{color: yellow;}.box div:only-of-type{color: pink;}
复制代码
<div class="box">    <div>第一个div</div>    <p>第一个p</p>    <div>第二个div</div>    <p>第二个p</p>    <div class="in">第三个div</div>    <p>第三个p</p></div><div class="box">    <div>第四个div</div></div>
复制代码

【3】:root、:not、:empty、:target

:root         选择文档的根元素:not          选择除某个元素之外的所有元素:empty         选择没有子元素的元素,而且该元素也不包含任何文本节点:target       匹配锚点对应的目标元素

  [注意]:not选择器常用于导航之间的竖线处理,如li:not(:last-of-type)

:root{color:red;}div:not{background-color: lightgrey;}p:empty{height:30px;width:30px;background:pink;}:target{color:blue;}
复制代码
<body>    <a href="#test">测试</a>    <div>第一个div</div>    <p>第一个p</p>    <div id="test">第二个div</div>    <p>第二个p</p>    <p></p>    </body>
复制代码

 

0 0
原创粉丝点击