CSS 选择器

来源:互联网 发布:杭州淘宝运营学徒招聘 编辑:程序博客网 时间:2024/06/04 18:44

在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素。

容易混淆的

一子选择器和后代选择器

后代选择器,即加入空格,用于选择指定标签元素下的后辈元素

.first  span{color:red;}

子选择器,即大于符号(>),用于选择指定标签元素的第一代子元素。示例如下:

.food > li{  border:1px solid red; }

总结:>作用于元素的第一代后代,空格作用于元素的所有后代。

二逗号和加号选择器

逗号选择器表示平级

div,p   选择所有 <div> 元素和所有 <p> 元素。

加号选择器即相邻兄弟选择器

h1 + p {margin-top:50px;}

这个选择器读作:“选择紧接在 h1 元素后出现的段落,h1 和 p 元素拥有共同的父元素”。
只会选择一个

三+和~选择器
1. + 选择器
如果需要选择紧接在另一个元素后的元素,而且二者有相同的父元素,可以使用相邻兄弟选择器。
2. ~ 选择器
作用是查找某一个指定元素的后面的所有兄弟结点。

参考:http://blog.csdn.net/u014291497/article/details/50482874

属性选择器

[attribute] [target]    选择带有 target 属性所有元素。 2[attribute=value]   [target=_blank] 选择 target="_blank" 的所有元素。   2[attribute~=value] [title~=flower]    选择 title 属性包含单词 "flower" 的所有元素。 2[attribute|=value]  [lang|=en]  选择lang属性值以 "en" 开头的所有元素。 2[attribute^=value]  a[src^="https"] 选择其 src 属性值以 "https" 开头的每个 <a> 元素。  3[attribute$=value]a[src$=".pdf"] 选择其 src 属性以 ".pdf" 结尾的所有 <a> 元素。    3[attribute*=value]  a[src*="abc"]   选择其 src 属性中包含 "abc" 子串的每个 <a> 元素。   3

关于链接的选择器

:link   a:link  选择所有未被访问的链接。    1:visited    a:visited   选择所有已被访问的链接。    1:active a:active    选择活动链接。 1:hover  a:hover 选择鼠标指针位于其上的链接。1

注意: a标签的各个伪类书写顺序
正确写法

a:link{text-decoration:none ; color:red ;}a:visited {text-decoration:none ; color:black ;}a:hover {text-decoration:underline ; color:yellow ;}a:active {text-decoration:none ; color:pink ;}

记忆口诀:lovehate

:visited要写在:hover和:active之前,否则会覆盖:hover和:active:hover要写在:active之前,否则会覆盖:activeactive是鼠标点击的时候激活,变颜色

有关type的选择器

:first-of-type  p:first-of-type 选择属于其父元素的首个 <p> 元素的每个 <p> 元素。   3:last-of-type   p:last-of-type  选择属于其父元素的最后 <p> 元素的每个 <p> 元素。   3:only-of-type   p:only-of-type  选择属于其父元素唯一的 <p> 元素的每个 <p> 元素。   3:nth-of-type(n) p:nth-of-type(2)    选择属于其父元素第二个 <p> 元素的每个 <p> 元素。   3:nth-last-of-type(n)    p:nth-last-of-type(2)   同上,但是从最后一个子元素开始计数。  3

有关child的选择器

:only-child p:only-child    选择属于其父元素的唯一子元素的每个 <p> 元素。   3:nth-child(n)   p:nth-child(2)  选择属于其父元素的第二个子元素的每个 <p> 元素。  3:nth-last-child(n)  p:nth-last-child(2) 同上,从最后一个子元素开始计数。    3:first-child    p:first-child   选择属于父元素的第一个子元素的每个 <p> 元素。   2:last-child p:last-child    选择属于其父元素最后一个子元素每个 <p> 元素。   3

css3之nth-child和nth-of-type的区别

nth-child

p:nth-child(n){//表示p的父元素下的第n个子元素}

nth-of-type

p:nth-of-type(n){//表示p的父元素下的第n个子元素且这个子元素是p}

例子参考:http://blog.csdn.net/u012657197/article/details/73740745

伪元素选择器

:first-letter   p:first-letter  选择每个 <p> 元素的首字母。    1:first-line p:first-line    选择每个 <p> 元素的首行。 1

注意::first-child和:last-child等属于伪类

伪类和伪元素的区别参考:http://blog.csdn.net/shuidinaozhongyan/article/details/71539152

不常见的选择器

:root   :root   选择文档的根元素。   3:empty  p:empty 选择没有子元素的每个 <p> 元素(包括文本节点)。  3:enabled    input:enabled   选择每个启用的 <input> 元素。 3:disabled   input:disabled  选择每个禁用的 <input> 元素  3:checked    input:checked   选择每个被选中的 <input> 元素。    3:not(selector)  :not(p) 选择非 <p> 元素的每个元素。    3::selection ::selection 选择被用户选取的元素部分。   3

对于disabled和checked在input元素上的效果:disabled是Input按钮不可用;而checked是单选或者多选按钮被选中。

参考:http://blog.csdn.net/u012110719/article/details/41171517

选择器的例子可参考:http://www.w3school.com.cn/cssref/css_selectors.asp

原创粉丝点击