你想知道的CSS3选择器,全在这里

2022-12-17 0 734

CSS3 示例——基本上示例

1、纯虚(*)

你想知道的CSS3选择器,全在这里

纯虚

<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)

你想知道的CSS3选择器,全在这里

原素示例

<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)

你想知道的CSS3选择器,全在这里

类示例

<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)

你想知道的CSS3选择器,全在这里

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)

你想知道的CSS3选择器,全在这里

后代示例

<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)

你想知道的CSS3选择器,全在这里

子原素示例

<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)

你想知道的CSS3选择器,全在这里

相邻兄弟原素示例

<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)

你想知道的CSS3选择器,全在这里

通用兄弟示例

<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、群组示例

你想知道的CSS3选择器,全在这里

群组示例

<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]:只使用属性名,但没有确定任何属性值

你想知道的CSS3选择器,全在这里

属性名示例

<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”]选择指定的属性对象

你想知道的CSS3选择器,全在这里

选择指定的属性对象

<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”] 包含属性值

你想知道的CSS3选择器,全在这里
<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”开头的所有原素

你想知道的CSS3选择器,全在这里
<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”结尾的所有原素

你想知道的CSS3选择器,全在这里
<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”的所有原素。

你想知道的CSS3选择器,全在这里
<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-开头的所有原素

你想知道的CSS3选择器,全在这里
<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 选择某个原素的第一个子原素

你想知道的CSS3选择器,全在这里
<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 选择某个原素的最后一个子原素

你想知道的CSS3选择器,全在这里
<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() 选择某个的一个或多个特定的子原素

:nth-child(number);/*参数是具体数字*/:nth-child(n);/*参数是n,n从0开始计算*/:nth-child(n*length)/*n的倍数选择,n从0开始算*/:nth-child(n+length);/*选择大于length后面的原素*/:nth-child(-n+length)/*选择小于length前面的原素*/:nth-child(n*length+1);/*表示隔几选一*/<ulclass=“demo”> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul>

:nth-child() 可以定义括号的值(值可以是整数,也可以是表达式)

你想知道的CSS3选择器,全在这里

.demo li:nth-child(3) { background: red; border: 1px solid yellow; color: #fff; }

选择 div 原素下的第3个 li 子原素。

:nth-child(n)/*参数是n,n从0开始计算*/

你想知道的CSS3选择器,全在这里

.demo li:nth-child(n) { background: red; border: 1px solid yellow; color: #fff; }

等于.demo li {background: lime;}

n是一个简单的表达式,那么”n”取值是从“0”开始计算。

:nth-child(n*length)

你想知道的CSS3选择器,全在这里

.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

以此类推….

你想知道的CSS3选择器,全在这里

.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后面的原素*/

你想知道的CSS3选择器,全在这里

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

以此类推….

你想知道的CSS3选择器,全在这里

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);/*表示隔几选一*/

你想知道的CSS3选择器,全在这里

: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() 选择指定的原素,从最后一个开始

你想知道的CSS3选择器,全在这里

.demo li:nth-last-child(4) { background: red; border: 1px solid yellow; color: #fff; }

选择倒数第四个原素。

你想知道的CSS3选择器,全在这里

可以用表达示,选择奇数

.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

以此类推….

你想知道的CSS3选择器,全在这里

可以用表达示,选择偶数

.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 选择指定的类型原素

你想知道的CSS3选择器,全在这里

.demo li:nth-of-type(8) { background: red; border: 1px solid yellow; color: #fff; }

中间间隔了a原素

你想知道的CSS3选择器,全在这里

与 :nth-child区别,:nth-child不指定类型 .demo li:nth-child(8) { background: red; border: 1px solid yellow; color: #fff; }

中间间隔了a原素,因没有指定类型,而第8个原素是a,所以无法设置样式

6、:nth-last-of-type() 选择指定类型的原素,从原素的最后一个开始计算

你想知道的CSS3选择器,全在这里

.demo li:nth-last-of-type(2) { background: red; border: 1px solid yellow; color: #fff; }

7、:first-of-type 选择指定类型的第一个原素;

你想知道的CSS3选择器,全在这里

.demo li:first-of-type { background: red; border: 1px solid yellow; color: #fff; }

:first-of-type与:first-child类型,前者区别了类型,后者无区域

,子原素中包含了a、li两种原素

8、:last-of-type 选择指定类型的最后一个原素;

你想知道的CSS3选择器,全在这里

.demo li:last-of-type { background: red; border: 1px solid yellow; color: #fff; }

:last-of-type与:last-child类型,前者区分了类型,后者无区分

子原素中包含了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标签会被选中,因为它没有内容。

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务