Css3学习笔记(一):选择器篇

CSS3学习笔记:选择器

1. 属性选择器

使用[att=val]、[att*=val]、[att^=val]、[att$=val]来分别代表选择属性的相等、包含、以……开头、以……结尾的元素。
如:
[id="section1-2"]{background:red;}
[name="tool1"]{background:pink;}
[id*="section2"]{background:blue;}
[id$="1"]{background:pink;}

2. 结构性伪类选择器

所谓伪元素选择器,并不是指真正的元素使用的选择器,而是针对CSS中已经定义好的伪元素使用的选择器。如:
选择器:伪元素{属性:值} 
或者:
选择器 类名:伪元素{属性:值}
在CSS中,主要有如下4种伪元素选择器:
a. first-line
p:first-line{color:#0000FF}
b. first-letter
p:first-letter{font-size:14px;color:red;}
c. before
li:before{content:*}
d. after
li:after{
content:”仅用于测试,切勿用于商业用途。”;
	font-size:12px;
	color:red;
}
e. Root
:root{
	background-color:yellow;
}
f. Not
li:not(h1){
	font-size:18px;
	color:gold;
}
g. Empty
:empty{
	background-color:yellow;
}
h. Target
:target{
	background-color:blue;
}
i. First-child、last-child
li:first-child{
	background-color:yellow;
}
li:last-child{
	background-color:skyblue;
}
j. Nth-child、nth-last-child
li:nth-child(2){
	color:green;
}
li:nth-last-child(2){
	color:blue;
}
li:nth-child(odd){
	color:green;
}
li:nth-last-child(even){
	color:green;
}
k. Nth-of-type、nth-last-type
h2:nth-of-type(even){
	background-color:skyblue;
}
l. 循环使用样式
li:nth-child(3n+1){
	background-color:yellow;
}
li:nth-child(3n+2){
	background-color:blue;
}
li:nth-child(3n+3){
	background-color:green;
}
m. Only-child
li:only-child{
	background-color:yellow;
}

3. UI元素状态伪类选择器

在CSS3中,共有11种UI元素状态伪类选择器,分别是E:hover、E:active、E:focus、E:enabled、E:disabled、E:read-only、E:read-write、E:checked、E:default、E:indeterminate、E::selection.
input[type="text"]:hover{
	background-color:gray;
}
input[type="text"]:active{
	background-color:yellow;
}
input[type="text"]:focus{
	background-color:pink;
}
p::selection{
	background-color:red;
	color:#FFF;
}
input[type="text"]:read-only{
	background-color:gray;
}
input[type="text"]:read-write{
	background-color:yellow;
}

4. 通用兄弟元素选择器

div~p{
	background-color:#00FF00;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值