css3 选择器 odd,常用css3选择器

1

2

3

4

5

p{

width:40px;

margin:8px auto;

line-height:40px;

border:1px solid gray;

text-align:center;

font-weight: 700;

}

X + Y 相邻选择器

X + Y : 相邻兄弟选择器 选择匹配Y的元素,且该元素为所匹配X元素后面相邻的位置。

.test1+p{

background-color:green;

color:#fff

}

bVPC3Z?w=173&h=252

X > Y 子选择器

X > Y:子包含选择器 选择匹配Y的元素,且该元素为所匹配X元素的子元素。

.wrapper>p{

background-color:#f5524b;

color:#fff;

border:none

}

bVPC4J?w=169&h=254

X ~ Y 相邻选择器

X ~ Y: 选择所有后面的兄弟节点们

.test2~p{

background-color:#0eabdf;

color:#fff;

border:none

}

bVPC6n?w=250&h=256

X[title] 属性选择器

a {

display: block;

width:300px;

text-align: center;

margin: 10px auto;

color: #000;

text-decoration: none;

}

a[title] {

background: green;

color: #fff;

}

bVPEo3?w=388&h=198

X[title=””] 另一种属性选择器

a[title="标题"] {

background: #ff9900;

color: #fff;

}

bVPEpp?w=354&h=210

属性选择器,上述代码不只匹配带有title属性,更匹配title属性等于”标题”的链接元素。[]内不只可用title属性,还可以使用其他属性。

X[title*=””] 模糊匹配属性选择器

a[title*="标题"]{

background: #3a8aee;

color: #fff;

}

bVPEpw?w=368&h=180

选择器匹配属性值以标题指定值开头的每个元素。

  • item1
  • item2
  • item3
  • item4
  • item5
  • item6
  • item7
  • item8

ul{

list-style: none;

}

.list li{

line-height:24px

}

bVPEvF?w=381&h=211

:first-child

.list li:first-child{

background-color:yellow;

}

bVPEvv?w=534&h=229

:last-child

:last-child选择器用来匹配父元素中最后一个子元素。

bVPECp?w=368&h=212

:nth-child()

nth-child(n)选择器匹配父元素中的第n个子元素。n可以是一个数字,一个关键字,或者一个公式。

.list li:nth-child(2){

background-color:#09ac24;

}

bVPEwF?w=438&h=217

Odd 和 even

Odd 和 even 是可用于匹配下标是奇数或偶数的子元素的关键词

.list li:nth-child(odd)

{

background:#e73a3a;

}

.list li:nth-child(even)

{

background:#f5a72c;

}

bVPEzl?w=474&h=216

:nth-last-child(n)

:nth-last-of-type(n)选择器匹配同类型中的倒数第n个同级兄弟元素。n可以是一个数字,一个关键字,或者一个公式

.list li:nth-last-child(6) {

background-color:#15d6af;

}

bVPExn?w=376&h=209

选择前几个元素

.list li:nth-child(-n+6) {

background: #F05442;

color: #fff;

}

bVboBjx?w=333&h=221

从第几个开始选择

.list li:nth-child(n+4){

background: #F05442;

color: #fff;

}

bVboBjW?w=326&h=208

限制选择某一个范围

:nth-child(-n+6):nth-child(n+3){

background: #F05442;

color: #fff;

}

bVboBk9?w=338&h=204

:nth-of-type(n)

:nth-of-type(n)选择器匹配同类型中的第n个同级兄弟元素。

.list li:nth-of-type(3) {

background: #635f5c;

}

bVPEAg?w=318&h=203

:only-child

:only-child选择器匹配属于父元素中唯一子元素的元素。

span:only-child{

background: #f26f0f;

}

bVPEBd?w=205&h=206

:not

:not(selector) 选择器匹配每个元素是不是指定的元素/选择器。

.list li:not(:last-child){

background: #0fcff2;

}

bVPEDI?w=397&h=244

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值