Rumah > hujung hadapan web > tutorial css > 关于CSS3中选择符的实例详解

关于CSS3中选择符的实例详解

Y2J
Lepaskan: 2017-05-23 11:06:49
asal
1684 orang telah melayarinya

英文原文: www.456bereastreet.com/archive/200601/css_3_selectors_explained/ 中文翻译: www.dudo.org/article.asp?id=197 注:本文写于2006年1月,当时IE7、IE8和Firefox3还未发行,文中所有说的浏览器支持均未包括这三个版本的浏览器。在IE8和Fir
:nth-of-type()
:nth-of-type()伪类和nth-child()伪类找使用方法十分相似,只不过他匹配的是规则中指明类型的元素。下面的规则将会匹配每个属于父元素中第三个子元素的p元素(属于第3个子元素的p在一个该当中可能会有很多,只不过他们位于不同的父元素下,dudo注):
p:nth-of-type(3) { background:#ff0; }
当你想确定是否已经指向了第三个p元素,这种方法会很有用。开始你可能觉得这和使用nth-child效果是一样的,但是nth-child(3)会把所有的子元素都计算进来,因此结果可能就会不一样,除非p所有的所有兄弟元素也都是p元素。
:nth-of-type伪类目前还没有浏览器支持。
:nth-last-of-type伪类
:nth-last-of-type伪类指向的元素在其后还有若干同类型的元素。和:nth-last-child伪类一样,它是从最后一个子元素向回数的。下面的规则将会匹配到倒数第二个兄弟元素p中(注意:是兄弟、同级别的节点):
p:nth-last-of-type(2) { background:#ff0; }
:nth-last-of-type()目前还没有浏览器支持。
:last-child伪类
:last伪类指向的是父元素中最后一个子元素。它和:nth-last-child(1)效果是一样的。下面的规则匹配所有属于父元素中最后一个子元素的p:
p:last-child { background:#ff0; }
:last-childe伪类可以在基于Mozilla的浏览器中使用。Opera不支持:last-childe伪类,在Safri中存在bug(上面的规则会匹配到所有的p元素)。令人稀奇是的它可以在OmniWeb(测试版本5.1.1)中正常使用,尽管这个浏览器是基于Safari的。这可能是因为Apple WebKit最新版本的回归,因为OmniWeb使用的WebKit通常要比Safari版本稍微低一点。
:first-of-type伪类
:first-of-type伪类指向同类型元素中的第一个元素。它和:nth-of-type(1)是一样的。
p:first-of-type { background:#ff0; }
:first-of-type伪类目前还没有浏览器支持。
:last-of-type伪类
:last-of-type伪类指向同类型元素中的最后一个元素。它和nth-last-of-type(1)是一样的。
p:last-of-type { background:#ff0; }
:last-of-type伪类目前还没有浏览器支持。
:only-child伪类
:only-childe伪类指向的是那些父元素中再没有其他任何子元素的元素。它和:first-child:last-child或者:nth-child(1):nth-last-child(1)是一样的(后者不够专业、简明)。
p:only-child { backgound:#ff0; }
:only-childe伪类目前可以在基于Mozilla的浏览器中使用。在Safari似乎被解释成了:first-child(上面的规则会被匹配到文档中所有第一个子元素p中)。
:only-of-type伪类
:only-of-type伪类指向的是那些父元素中再也没有与之类型相同的子元素。这和:first-of-type:last-of-type或者:nth-of-type(1):nth-last-of-type(1)相同(后者专业性不够)。
p:only-of-type { background:#ff0; }
:only-of-type伪类目前还没有浏览器支持。

【相关推荐】

1. CSS3免费视频教程

2. 整合20个CSS/CSS3常用属性

3. h5和css3制作带提示文字的输入框

4. 分享几个常用的最新的css3属性

5. 用CSS制作聊天框小尖角、气泡效果

Atas ialah kandungan terperinci 关于CSS3中选择符的实例详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan