SS3 选择器——基本选择器
1、通配符(*)
通配符
<ul class="demo">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
.demo * {border:1px solid blue;}
2、元素选择器(Element)
元素选择器
<ul class="demo">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
li { background-color: grey; color: orange; }
元素选择器,是css选择器中最常见而且最基本的选择器。元素选择器其实就是文档的元素,如html,body,p,div等等,比如这个demo:中元素包括了div,ul,li等。
3、类选择器(.className)
类选择器
<ul class="demo">
<li>1</li>
<li class="li-2">2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
.li-2 {font-weight: bold; color: yellow;}
上面代码表示是给有 "li-2" 类名的元素加上一个“字体为粗体,颜色为黄色”的样式。
4、id选择器(#ID)
id选择器
<ul class="demo">
<li id="first">1</li>
<li class="li-2">2</li>
<li>3</li>
<li>4</li>
<li id="last">5</li>
</ul>
#first {background: lime;color: #000;}
#last {background: #000;color: lime;}
上面的代码选择了id为"first"和"last"的li。
5、后代选择器(E F)
后代选择器
<ul class="demo">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
ul li { background-color: red; color: #fff; }
元素的子元素或者是孙元素或者是更深层次的关系,都将被选中,换句话说,不论li在ul中有多少层关系,都将被选中。注意他们之间需要一个空格隔开
6、子元素选择器(E>F)
子元素选择器
<ul class="demo">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
ul > li {background: green;color: yellow;}
子元素选择器只能选择某元素的第一层子元素,其中ul为父元素,而li为子元素,其中ul>li所表示的是:选择了ul元素下的所有第一层子元素li。简单的说就是只选择当前第一层级的子元素
7、相邻兄弟元素选择器(E + F)
相邻兄弟元素选择器
<ul class="demo">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
li + li {background: green;color: yellow; border: 1px solid #ccc;}
相邻兄弟选择器可以选择紧接在另一个元素后面的元素。
上面的 li+li,其中第二个li是第一个li的相邻元素,第三个又是第二个相邻元素,因此第三个也被选择,依此类推,所以后面4个li都被选中了。
8、通用兄弟选择器(E 〜 F)
通用兄弟选择器
<ul class="demo">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
.active ~ li {background: green;color: yellow; border: 1px solid #ccc;}
选择某元素后面的所有兄弟元素(选择相邻的所有兄弟元素),和相邻选择器类似(相邻选择器只选择相邻的一个元素)
选择中了li.active 元素后面的所有兄弟元素li
9、群组选择器
群组选择器
<ul class="demo">
<li class="first">1</li>
<li>2</li>
<li class="li-3">3</li>
<li>4</li>
<li class="last">5</li>
</ul>
.first,
.last,
.li-3 {background: green;color: yellow; border: 1px solid #ccc;}
群组选择器是将具有相同样式的元素分组在一起,每个选择器之间使用逗号“,”隔开
li.first和li.last和.li-3具有相同的样式效果,所以我们把他们写到一个组里
CSS3 选择器——属性选择器
1、E[attr]:只使用属性名,但没有确定任何属性值
属性名选择器
<ul class="demo">
<a href="http://www.w3cplus.com" target="_blank" class="links item first" id="first" title="w3cplus">1</a>
<a href="" class="links active item" title="test website" target="_blank" lang="zh">2</a>
<a href="sites/file/test.html" class="links item" title="this is a link" lang="zh-cn">3</a>
<a href="sites/file/test.png" class="links item" target="_balnk" lang="zh-tw">4</a>
<a href="sites/file/image.jpg" class="links item" title="zh-cn">5</a>
<a href="mailto:w3cplus@hotmail" class="links item" title="website link" lang="zh">6</a>
<a href="" class="links item" title="open the website" lang="cn">7</a>
<a href="" class="links item" title="close the website" lang="en-zh">8</a>
<a href="" class="links item" title="http://www.sina.com">9</a>
<a href="" class="links item last" id="last">10</a>
</ul>
.demo a[id] { background: blue; color:yellow; font-weight:bold; }
选择了div.demo下所有带有id属性的a元素,并在这个元素上使用背景色为蓝色,字体加粗并为黄色的样式,
2、E[attr="value"]选择指定的属性对象
选择指定的属性对象
<ul class="demo">
<a href="http://www.w3cplus.com" target="_blank" class="links item first" id="first" title="w3cplus">1</a>
<a href="" class="links active item" title="test website" target="_blank" lang="zh">2</a>
<a href="sites/file/test.html" class="links item" title="this is a link" lang="zh-cn">3</a>
<a href="sites/file/test.png" class="links item" target="_balnk" lang="zh-tw">4</a>
<a href="sites/file/image.jpg" class="links item" title="zh-cn">5</a>
<a href="mailto:w3cplus@hotmail" class="links item" title="website link" lang="zh">6</a>
<a href="" class="links item" title="open the website" lang="cn">7</a>
<a href="" class="links item" title="close the website" lang="en-zh">8</a>
<a href="" class="links item" title="http://www.sina.com">9</a>
<a href="" class="links item last" id="last">10</a>
</ul>
.demo a[id="first"] {background: blue; color:yellow; font-weight:bold; }
选择了.demo a[id="first"] 选择属性id的值为first的对象。
3、E[attr~="value"] 包含属性值
<ul class="demo">
<a href="http://www.w3cplus.com" target="_blank" class="links item first" id="first" title="w3cplus">1</a>
<a href="" class="links active item" title="test website" target="_blank" lang="zh">2</a>
<a href="sites/file/test.html" class="links item" title="this is a link" lang="zh-cn">3</a>
<a href="sites/file/test.png" class="links item" target="_balnk" lang="zh-tw">4</a>
<a href="sites/file/image.jpg" class="links item" title="zh-cn">5</a>
<a href="mailto:w3cplus@hotmail" class="links item" title="website link" lang="zh">6</a>
<a href="" class="links item" title="open the website" lang="cn">7</a>
<a href="" class="links item" title="close the website" lang="en-zh">8</a>
<a href="" class="links item" title="http://www.sina.com">9</a>
<a href="" class="links item last" id="last">10</a>
</ul>
.demo a[title~="website"]{ background:orange; color:green; }
div.demo下的a元素的title属性中,只要其属性值中含有"website"这个词就会被选择
4、E[attr^="value"] 选择attr属性值以“value”开头的所有元素
<ul class="demo">
<a href="http://www.w3cplus.com" target="_blank" class="links item first" id="first" title="w3cplus">1</a>
<a href="" class="links active item" title="test website" target="_blank" lang="zh">2</a>
<a href="sites/file/test.html" class="links item" title="this is a link" lang="zh-cn">3</a>
<a href="sites/file/test.png" class="links item" target="_balnk" lang="zh-tw">4</a>
<a href="sites/file/image.jpg" class="links item" title="zh-cn">5</a>
<a href="mailto:w3cplus@hotmail" class="links item" title="website link" lang="zh">6</a>
<a href="" class="links item" title="open the website" lang="cn">7</a>
<a href="" class="links item" title="close the website" lang="en-zh">8</a>
<a href="" class="links item" title="http://www.sina.com">9</a>
<a href="" class="links item last" id="last">10</a>
</ul>
.demo a[href^="http://"]{ background:orange; color:green; }
.demo a[href^="mailto:"]{ background:green; color:orange; }
选择了以href属性并且以"http://"和"mailto:"开头的所有a元素。
5、E[attr$="value"] 选择attr属性值以"value"结尾的所有元素
<ul class="demo">
<a href="http://www.w3cplus.com" target="_blank" class="links item first" id="first" title="w3cplus">1</a>
<a href="" class="links active item" title="test website" target="_blank" lang="zh">2</a>
<a href="sites/file/test.html" class="links item" title="this is a link" lang="zh-cn">3</a>
<a href="sites/file/test.png" class="links item" target="_balnk" lang="zh-tw">4</a>
<a href="sites/file/image.jpg" class="links item" title="zh-cn">5</a>
<a href="mailto:w3cplus@hotmail" class="links item" title="website link" lang="zh">6</a>
<a href="" class="links item" title="open the website" lang="cn">7</a>
<a href="" class="links item" title="close the website" lang="en-zh">8</a>
<a href="" class="links item" title="http://www.sina.com">9</a>
<a href="" class="links item last" id="last">10</a>
</ul>
.demo a[href$="png"]{ background:orange; color:green; }
选择div.demo中元素有href属性,并以png值结尾的a元素。
6、E[attr*="value"] 选择attr属性值中包含子串"value"的所有元素。
<ul class="demo">
<a href="http://www.w3cplus.com" target="_blank" class="links item first" id="first" title="w3cplus">1</a>
<a href="" class="links active item" title="test website" target="_blank" lang="zh">2</a>
<a href="sites/file/test.html" class="links item" title="this is a link" lang="zh-cn">3</a>
<a href="sites/file/test.png" class="links item" target="_balnk" lang="zh-tw">4</a>
<a href="sites/file/image.jpg" class="links item" title="zh-cn">5</a>
<a href="mailto:w3cplus@hotmail" class="links item" title="website link" lang="zh">6</a>
<a href="" class="links item" title="open the website" lang="cn">7</a>
<a href="" class="links item" title="close the website" lang="en-zh">8</a>
<a href="" class="links item" title="http://www.sina.com">9</a>
<a href="" class="links item last" id="last">10</a>
</ul>
.demo a[title*="site"]{ background:black; color:white; }
选择了div.demo中a元素,而a元素的title属性中只要有"site"就选中。
7、E[attr|="value"] 选择attr属性值等于value或以value-开头的所有元素
<ul class="demo">
<a href="http://www.w3cplus.com" target="_blank" class="links item first" id="first" title="w3cplus">1</a>
<a href="" class="links active item" title="test website" target="_blank" lang="zh">2</a>
<a href="sites/file/test.html" class="links item" title="this is a link" lang="zh-cn">3</a>
<a href="sites/file/test.png" class="links item" target="_balnk" lang="zh-tw">4</a>
<a href="sites/file/image.jpg" class="links item" title="zh-cn">5</a>
<a href="mailto:w3cplus@hotmail" class="links item" title="website link" lang="zh">6</a>
<a href="" class="links item" title="open the website" lang="cn">7</a>
<a href="" class="links item" title="close the website" lang="en-zh">8</a>
<a href="" class="links item" title="http://www.sina.com">9</a>
<a href="" class="links item last" id="last">10</a>
</ul>
.demo a[lang|="zh"]{ background: gray; color: yellow; }
选择了div.demo中lang属性等于zh或以zh-开头的所有a元素。
CSS3 选择器——伪类选择器
1、:first-child 选择某个元素的第一个子元素
<ul class="demo">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
.demo li:first-child { background: red; border: 1px solid yellow; color: #fff; }
选择某个元素的第一个子元素。
2、:last-child 选择某个元素的最后一个子元素
<ul class="demo">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
.demo li:last-child { background: red; border: 1px solid yellow; color: #fff; }
选择某个元素的最后一个子元素。
3、:nth-child() 选择某个的一个或多个特定的子元素
<ul class="demo">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
:nth-child() 可以定义括号的值(值可以是整数,也可以是表达式)
.demo li:nth-child(3) { background: red; border: 1px solid yellow; color: #fff; }
选择 div 元素下的第3个 li 子元素。
[微风][微风]
:nth-child(n) /*参数是n,n从0开始计算*/
.demo li:nth-child(n) { background: red; border: 1px solid yellow; color: #fff; }
等于.demo li {background: lime;}
n是一个简单的表达式,那么"n"取值是从“0”开始计算。
[微风][微风]
:nth-child(n*length)
.demo li:nth-child(2n) { background: red; border: 1px solid yellow; color: #fff; }
等于.demo li:nth-child(even) {}
选择偶数的对象:n是一个简单的表达式,那么"n"取值是从“0”开始计算。
表达示结果,如下:
.demo li:nth-child(2n) = (2*0) = 0
.demo li:nth-child(2n) = (2*1) = 2
.demo li:nth-child(2n) = (2*2) = 4
.demo li:nth-child(2n) = (2*3) = 6
以此类推....
[微风][微风]
.demo li:nth-child(2n-1) { background: red; border: 1px solid yellow; color: #fff; }
等于.demo li:nth-child(odd) {}
选择奇数的对象:n是一个简单的表达式,那么"n"取值是从“0”开始计算。
表达示结果,如下:
.demo li:nth-child(2n-1) = (2*0-1) = -1
.demo li:nth-child(2n-1) = (2*1-1) = 1
.demo li:nth-child(2n-1) = (2*2-1) = 3
.demo li:nth-child(2n-1) = (2*3-1) = 5
以此类推....
[微风][微风]
:nth-child(n+length); /*选择大于length后面的元素*/
nth-child(n+5)从第五个元素开始选择,这里的数字你可以自己定义
.demo li:nth-child(n+5){ background: red; border: 1px solid yellow; color: #fff; }
n是一个简单的表达式,那么"n"取值是从“0”开始计算。
表达示结果,如下:
.demo li:nth-child(0+5) = 5
.demo li:nth-child(1+5) = 6
.demo li:nth-child(2+5) = 7
.demo li:nth-child(3+5) = 8
以此类推....
[微风][微风]
nth-child(-n+5)反向从第五个元素开始选择,这里的数字你可以自己定义
.demo li:nth-child(-n+5){ background: red; border: 1px solid yellow; color: #fff; }
n是一个简单的表达式,那么"n"取值是从“0”开始计算。
表达示结果,如下:
.demo li:nth-child(-0+5) = 5
.demo li:nth-child(-1+5) = 4
.demo li:nth-child(-2+5) = 3
.demo li:nth-child(-3+5) = 2
以此类推....
[微风][微风]
:nth-child(n*length+1); /*表示隔几选一*/
:nth-child(4n+1)间隔选择对象,数字可自定义
.demo li:nth-child(4n+1) { background: red; border: 1px solid yellow; color: #fff; }
n是一个简单的表达式,那么"n"取值是从“0”开始计算。
表达示结果,如下:
.demo li:nth-child(4*0+1) = 1
.demo li:nth-child(4*1+1) = 5
.demo li:nth-child(4*2+1) = 9
.demo li:nth-child(4*3+1) = 13
以此类推....
4、:nth-last-child() 选择指定的元素,从最后一个开始
.demo li:nth-last-child(4) { background: red; border: 1px solid yellow; color: #fff; }
选择倒数第四个元素。
[微风][微风]
可以用表达示,选择奇数
.demo li:nth-last-child(2n) { background: red; border: 1px solid yellow; color: #fff; }
n是一个简单的表达式,那么"n"取值是从“0”开始计算。
表达示结果,如下:
:nth-last-child(2*1) = 2
:nth-last-child(2*2) = 4
:nth-last-child(2*3) = 6
:nth-last-child(2*4) = 8
以此类推....
[微风][微风]
可以用表达示,选择偶数
.demo li:nth-last-child(2n-1) { background: red; border: 1px solid yellow; color: #fff; }
n是一个简单的表达式,那么"n"取值是从“0”开始计算。
表达示结果,如下:
:nth-last-child(2*1-1) = 1
:nth-last-child(2*2-1) = 3
:nth-last-child(2*3-1) = 5
:nth-last-child(2*4-1) = 7
:nth-last-child(2*5-1) = 9
5、:nth-of-type 选择指定的类型元素
.demo li:nth-of-type(8) { background: red; border: 1px solid yellow; color: #fff; }
指定获取 类型为 li 的第8个元素,中间间隔了a元素
[微风][微风]
与 :nth-child区别,:nth-child不指定类型 .demo li:nth-child(8) { background: red; border: 1px solid yellow; color: #fff; }
指定获取元素为li第8个元素,中间间隔了a元素,因没有指定类型,而第8个元素是a,所以无法设置样式
6、:nth-last-of-type() 选择指定类型的元素,从元素的最后一个开始计算
.demo li:nth-last-of-type(2) { background: red; border: 1px solid yellow; color: #fff; }
数字可使用n表达式计算,从最后一个元素开始计算,获取指定类型为 li 的第2个元素,
7、:first-of-type 选择指定类型的第一个元素;
.demo li:first-of-type { background: red; border: 1px solid yellow; color: #fff; }
:first-of-type与:first-child类型,前者区别了类型,后者无区域
获取第一个为 li 的元素,子元素中包含了a、li两种元素
8、:last-of-type 选择指定类型的最后一个元素;
.demo li:last-of-type { background: red; border: 1px solid yellow; color: #fff; }
:last-of-type与:last-child类型,前者区分了类型,后者无区分
获取最后一个为 li 的元素,子元素中包含了a、li两种元素
9、:only-child 选择的元素是它的父元素的唯一一个了元素;
<div>
<p>我是子级,在父级中是唯一一个子元素。</p>
</div>
<div>
<span>我是span标签,在父级中并不是唯一的子元素,因为还有一个p标签。</span>
<p>我是p标签,在父级中并不是唯一的子元素,因为还有一个span标签。</p>
</div>
<p>
我是p标签,而我并没有父级。
</p>
p:only-child { background: #ff0000; }
我是子级,在父级中是唯一一个子元素。
我是span标签,在父级中并不是唯一的子元素,因为还有一个p标签。
我是p标签,在父级中并不是唯一的子元素,因为还有一个span标签。
我是p标签,而我并没有父级。
10、:only-of-type 选择一个元素是它的上级元素的唯一一个相同类型的子元素;
<div>
<p>我是子级p元素,在父级中是唯一的一个p元素。所以会被选择中。</p>
<span>我是子级span元素,在父级中并不是唯一的span元素。</span>
<span>我是子级span元素,在父级中并不是唯一的span元素。</span>
<span>我是子级span元素,在父级中并不是唯一的span元素。</span>
</div>
<div>
<p>我是p标签,在父级中并不是唯一的p元素,因为还有一个和我相同的p元素。所以不会被选中。</p>
<p>我是p标签,在父级中并不是唯一的p元素,因为还有一个和我相同的p元素。所以不会被选中。</p>
</div>
p:only-of-type { background:#ff0000; }
我是子级p元素,在父级中是唯一的一个p元素。所以会被选择中。
我是子级span元素,在父级中并不是唯一的span元素。 我是子级span元素,在父级中并不是唯一的span元素。 我是子级span元素,在父级中并不是唯一的span元素。
我是p标签,在父级中并不是唯一的p元素,因为还有一个和我相同的p元素。所以不会被选中。
我是p标签,在父级中并不是唯一的p元素,因为还有一个和我相同的p元素。所以不会被选中。
11、:empty 选择的元素里面没有任何内容。
<p>我是一个p标签,我<span style="color: red;">下面</span>有个p标签,内容是空的,所以它会被选中。</p>
<p></p>
<p>我是一个p标签,我<span style="color: red;">上面</span>有个p标签,内容是空的,所以它会被选中。</p>
<p>我是一个p标签。</p>
<p>我是一个p标签。</p>
p:empty { background:#ff0000; }
第2行的p标签会被选中,因为它没有内容。
文介绍了如何在网页中使用css及css的常用知识点,今天首先介绍css 的选择器,所谓选择器就是通过一定的规则语法查找html元素节点,只有找到这些元素才能应用样式。
css 选择器从不精确到最精确,可分以下几种:
tag 是html 标签关键字,一般小写。
在html标签上使用 class="" 属性应用css 样式,可以写多个class,空格隔开,如:<p class="cls1 cls2">。
属性值html标签上的各种属性,如 [alt], [type="text"], [lang="en"]。
id 必需是唯一的,不能重复。
语法如下:
* {
/* 通用选择器*/
}
tag {
/* 标签选择器 */
}
tag::before {
/* 标签选择器和伪元素 */
}
.class {
/* 类选择器 */
}
.class:hover {
/* 类选择器和伪类 */
}
[attr] {
/* 属性选择器 */
}
#id {
/* ID 选择器 */
}
伪元素有两个冒号 ( ::, 比如 ::before),伪类有一个冒号 ( :, 比如 :hover)。
后面学习JavaScript 时,会学习到通过js也可以查找html元素,查找规则就是通过css 选择器查找,如下示例:
此查询用于获取与选择器匹配的所有元素:
document.querySelectorAll('article h2')
如上代码获取到一个html 节点集合,每个html节点都有子节点,孙子节点,依次类推.....
在网页中 html 是一个树状的结构,每个html 元素是一个节点,且每个节点下面又有子节点。
如下图:
css 选择器就是在这样的一个树结构里面查找html元素节点,找到后浏览器渲染对应的样式。
选择器除了可以单个使用,也可以组合使用,比如 #id p, #id >.class,p+h3等,按功能可分以下几种:
示例:
<!DOCTYPE html>
<html>
<head>
<style>
div span{
color:red;
}
</style>
</head>
<body>
<div>黑色<span>红色</span></div>
<div>
<p>黑色<span>红色</span><span>红色</span></p>
</div>
<span>不是红色</span>
<span>不是红色</span>
</body>
</html>
显示效果:
示例:
<!DOCTYPE html>
<html>
<head>
<style>
div > span{
color:red;
}
</style>
</head>
<body>
<div>黑色<span>红色</span></div>
<div>
黑色<span>红色</span><span>红色</span>
<p><span>不是红色</span></p>
</div>
<span>不是红色</span>
<span>不是红色</span>
</body>
</html>
显示效果:
示例:
<!DOCTYPE html>
<html>
<head>
<style>
p + span{
color:red;
}
</style>
</head>
<body>
<p>黑色</p>
<span>红色</span>
<span>不是红色</span>
<span>不是红色</span>
</body>
</html>
显示效果:
这里一定注意span必须紧跟在p标签后面,它们2个之间有任何标签都不会起作用了。
示例:
<!DOCTYPE html>
<html>
<head>
<style>
p~span{
color: red;
}
</style>
</head>
<body>
<p>黑色</p>
<span>红色</span><br>
<span>红色</span><br>
<h1>标题</h1>
<span>红色</span>
</body>
</html>
显示效果:
如下示例:
h1,h2,h3{
background-color: red;
}
整个页面中的h1、h2、h3的背景色都是红色,无论在哪个层次的节点中都会起作用。
不同的选择器如果作用于同一个html元素,浏览器会根据优先级规则渲染样式。
优先级高的选择器会覆盖优先级低的样式,如下表从上到下优先级依次变高:
选择器 | 例子 | 级别 |
标签选择器 | h1 | 1 |
类,属性选择器 | .class, [type="text"] | 2 |
ID选择器 | #contact | 3 |
内联样式 | <div style="background: purple"> | 4 |
!important关键词 | div { color: green !important } | 覆盖所有 |
请谨慎地使用!important,它变得非常难以维护。!important 仅在绝对必要时才应使用,例如为某些您无法控制的第三方设置样式,使用内联样式,以及在少数情况下使用 JavaScript 切换显示。
css 选择器是一种查询html元素节点的语言,当css选择器互相组合时,会变得很复杂,所以平时尽量减少使用组合选择器。
关于优先级,总而言之,从标签选择器到!important的每层级别都比前一个级别强一个数量级。
学习css,切记死记硬背,理解它很重要,结合之前学习的html 知识多练习,感谢关注。
上篇:前端入门——css 样式表简介
SS 选择器是用来指定该组 CSS 样式会对什么元素生效的,是连接 HTML 结构和 CSS 样式的桥梁。这一篇将给大家介绍一下 CSS 里基础选择器的用法,同时会对使用上给出一些建议。
基础选择器包括 ID 选择器、类选择器、标签选择器、通配符选择器和属性选择器这几种。
ID 选择器
ID 选择器是用 “#” 号加 ID 名称 xxx 来表示,用来选择 HTML 中 id="xxx" 的 DOM 元素。我们以选择下面这个 ID 为 content 的元素为例:
<div id="content">我是id选择器需要选中的元素</div>
当我们想把样式应用到这个元素的时候,就可以用下面的 ID 选择器:
#content{ color: #fff; background: #000; }
这样 ID 为 content 的元素就会有一个黑底白字的效果了。在 ID 选择器中,有几点要注意:
1、ID 选择器只能对一个元素生效,同一个页面里不允许出现两个 ID 相同的元素。2、理论上 ID 选择器是效率最高的选择器。但是由于它只能选一个元素,特异性太高,在 实际开发中也很少在 CSS 里使用 ID 选择器。3、也正是因为 ID 选择器特异性高,所以在 JS 里使用 ID 选择器的比较常见。
类选择器
类选择器是用 “.” 加上 class 名称来表示,用来选择 HTML 中 class="xxx" 的 DOM 元素。我们以选择下面 class 名称为 list-item 的元素为例:
<ul> <li class="list-item"></li> <li class="list-item"></li> <li class="list-item"></li> <li class="list-item"></li> </ul>
当我们想把样式应用到列表里每一条元素的时候,就可以用类选择器:
.list-item{ border-bottom: 1px solid #ccc; }
这样列表里所有的项就都有一个宽 1px 灰色的下边框了。
在类选择器中,要注意以下几点:
1、类选择器的效率也是不错的,和 ID 选择器并不会有太大的差异。所以在写 CSS 的时候,比较推荐用类选择器。2、类选择器会选择到所有类名相同的 DOM 元素,没有数量限制。3、类选择器应该是样式开发中应用最多的选择器。
通配选择器
通配选择器使用星号来选择到页面里所有元素。用法如下:
*{ margin: 0; padding: 0; }
上面这个样式就是把所有元素的内外边距都归零。由于通配选择器要把样式覆盖到所有的元素上,可想而知它的效率并不会高,所以在实际开发中一般不建议使用通配选择器。
标签选择器
标签选择器的作用是选中 HTML 中某一种类的标签,它直接使用 HTML 中的标签名作为选择器的名称。比如我们需要把页面里所有大标题的字号都调成 20px,就可以用标签选择器来实现:
h1{ font-size: 20px; }
Tips:标签选择器通常用来重置某些标签的样式,标签选择器的效率也不是很高,但要好过通配选择器。
属性选择器
属性选择器比较好理解,就是通过 DOM 的属性来选择该 DOM 节点。属性选择器是用中括号 “[]” 包裹,比如选择所有带有 href 属性的标签,就可以使用这样的选择器:
a[href]{ color: red; }
这条选择器就可以让所有带 href 属性的 a 标签字体都变成红色。属性选择器有如下几种形式:
[attr],用来选择带有 attr 属性的元素,如刚提到的 a [href]。
<!-- HTML:--> <a href="/">返回主页</a> // 下面的CSS会使所有带href的a标签字体变红色: a[href]{ color: red; }
[attr=xxx],用来选择有 attr 属性且属性值等于 xxx 的元素,如选择所有文本类型的输入框,可以用 input [type=text]。
<!-- HTML:--> <input type="text" value="大花碗里扣个大花活蛤蟆"/> // CSS: input[type=text]{ color: red; }
这个选择器里面要注意,xxx 和 HTML 中的属性值必须完全相等才会生效。
<!-- HTML:--> <input class="input text" type="text" value="大花碗里扣个大花活蛤蟆"/> // CSS: input[class=input]{ color: red; }
上面例子中 input [class=input] 的选择器并不能选中 class=“input text” 的元素,如果非要用这种选择器,必须使用 input [class=“input text”] 才可以。
[attr~=xxx],这个选择器中间用了~=,选择属性值中包含 xxx 的元素,但一定是逗号分隔的多个值中有一个能和 xxx 相等才行。
<!-- HTML:--> <input class="input text" type="text" value="大花碗里扣个大花活蛤蟆"/> // CSS: input[class~=input]{ color: red; }
在上面的例子中,使用 input [class~=input] 就可以选中 class=“input text” 的元素了。
[attr|=xxx],这个选择器是用来选择属性值为 xxx 或 xxx- 开头的元素,比较常用的场景是选择某一类的属性。
<!-- HTML:--> <div class="article">我是article</div> <div class="article-title">我是article-title</div> <div class="article-content">我是article-content</div> <div class="article_footer">我是article_footer,不是以artical-开头的</div> // CSS: div[class|=article]{ color: red; }
上面的选择器就可以对所有 article 开头的元素生效,包括 class=“article” 的元素。上面的例子中,选择器会对前三条都生效,但不会对第四条生效。
[attr^=xxx],这个选择器会匹配以 xxx 开头的元素,实际上就是用正则去匹配属性值,只要是以 xxx 开头都可以。
<!-- HTML:--> <div class="article">我是article</div> <div class="article-title">我是article-title</div> <div class="article-content">我是article-content</div> <div class="article_footer">我是article_footer,不是以artical-开头的</div> // CSS: div[class^=article]{ color: red; }
还是用刚才的例子,如果把选择器换成 div [class^=article],那么上面四个 HTML 元素都会被选中了。
[attr$=xxx],这个选择器和上一个相似,它是用正则匹配的方式来选择属性值以 xxx 结尾的元素。
<!-- HTML:--> <button class="btn btn-disabled">禁用的按钮</button> <select class="select select-disabled city-select"></select> <input class="input input-disabled" value="禁用的输入框"/> // CSS: [class$=disabled]{ display: none; }
上面的例子中,我们想把页面里有禁用标识的所有元素都隐藏掉,就可以使用 [class$=disabled] 来选择所有 class 里以 disabled 结尾的元素。这么用的前提是提前约定好,disabled 相关的类要放在最后,否则就像上面的 select 一样不会生效。
[attr*=xxx],最后一个,这个是用正则匹配的方式来选择属性值中包含 xxx 字符的所有元素。这个选择器的规则算是最宽泛的,只要 xxx 是元素属性值的子字符串,这个选择器就会生效。
<!-- HTML:--> <button class="btn btn-disabled">禁用的按钮</button> <select class="select select-disabled city-select"></select> <input class="input input-disabled" value="禁用的输入框"/> // CSS: [class*=disabled]{ display: none; }
还是用刚才 disable 的例子,如果我们用 [class*=disabled] 选择器来选择 disabled 元素,就可以不考虑 -disable 属性所在的位置了,它对所有带这个单词的属性都会生效,哪怕是 class=“i-am-a-disabled-element” 的元素都可以。
Tips:1. 属性选择器要做文本的匹配,所以效率也不会高。2. 在使用属性选择器时,尽量要给它设置上生效的范围,如果只用了个 [href] 相当于要在所有元素里找带 href 的元素,效率会很低。如果用 a [href] 会好的多,如果用 .link [href] 就更好了。这种组合方式我们在下一节讲解。3. 属性选择器很灵活,如果能使用 CSS 代替 JS 解决一些需求,可以不用太纠结性能的问题,用 JS 实现也一样要耗费资源的。
总结
这一篇我们讲了 CSS 里几种基础的选择器,包括 ID 选择器、类选择器、标签选择器、通配符选择器和属性选择器。这几个选择器里面最常用的就是类选择器,最灵活的是属性选择器,而 ID 选择器、标签选择器和通配选择器的应用场景都不多。
*请认真填写需求信息,我们会在24小时内与您取得联系。