css2属性选择器
-
ele[attr]{ } 指定了属性名为attr的ele元素
-
ele[attr=value]{ } 指定了属性名为attr且属性值为value的ele元素
-
ele[attr~=value]{ } 指定了属性名称attr,并且属性值词列表中包含value的ele元素
-
注意:要有词列表的时候,属性名=属性值是选择不上的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div[class] {
background-color: antiquewhite;
}
div[id] {
background-color: aquamarine;
}
p[class] {
background-color: blue;
}
h1[class]{
background-color: blueviolet;
}
div[class=box1]{
background-color: cornflowerblue;
width: 100px;
height: 100px;
}
/*
词列表:具有多个类名
有词列表的元素,用属性名等于属性值选不上 */
p[class=dd]{
background-color: aqua;
}
/* 选择词列表 */
p[class~=dd]{
background-color: blueviolet;
}
/*
选择表单
*/
input[type=text]{
background-color: crimson;
border-color: blanchedalmond;
}
</style>
</head>
<body>
<div class="box">box</div>
<div class="div">div</div>
<div class="box1">box1</div>
<p class="p">p</p>
<h1 class="h1">h1</h1>
<div id="dd">dd</div>
<p class="ss dd">少时诵诗书所</p>
<input type="text">
<input type="password">
</body>
</html>
css3新增属性选择器
-
ele[attr^=value]{ } 指定了属性名attr,且属性值为value开头的ele元素
-
ele[attr$=value]{ } 指定了属性名attr,且属性值为value结尾的ele元素
-
ele[attr*=value]{ } 指定了属性名attr,且属性值包含value的ele元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 开头 */
div[class^=d]{
background-color: aqua;
}
/* 结尾 */
div[id$=d]{
background-color: blueviolet;
}
/* 中间 */
div[class*=o]{
background-color: chartreuse;
}
</style>
</head>
<body>
<div class="box">box</div>
<div class="div">div</div>
<div class="box1">box1</div>
<p class="p">p</p>
<h1 class="h1">h1</h1>
<div id="dd">dd</div>
<p class="ss dd">少时诵诗书所</p>
</body>
</html
结构伪类选择器
-
:root{ } 匹配文档根元素
-
ele:first-child{ } 选择一组相同元素中的第一个元素
-
ele:last-child{ } 选择一组相同元素中的最后一个元素
-
ele:nth-child(n){ } 选择一组相同元素中的第n个元素,n可以是数值、关键词、表达式
-
数值
-
关键词:odd(奇数)|even(偶数)
-
表达式:2n(偶数)|2n+1(奇数)
-
-
ele:nth-last-child(n){ } 选择一组相同元素中的倒数第n个元素,n表示数值、关键词、表达式
备注: 以上元素类型必须一样
-
ele:first-of-type 选择一组元素中特定类型的第一个子元素
-
ele:last-of-type 选择一组元素中特定类型的最后一个子元素
-
ele:nth-of-type(n){ } 选择一组元素中特定类型的第n个ele元素
-
ele:nth-last-of-type(n){ } 选择一组元素中特定类型的倒数第n个ele元素
备注 : 以上元素类型可以不一样
child一组与of-type一组的区别
-
nth-child强调结构 关系,查找子元素中的第几个
-
nth-of-type强调类型,查找类型中的第几个
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.wrap div:first-child{
background-color: chocolate;
}
.wrap div:last-child{
background-color: blueviolet;
}
.wrap div:nth-child(3){
background-color: cadetblue;
}
</style>
</head>
<body>
<div class="wrap">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</div>
</body>
</html>
状态伪类选择器
-
ele:disabled{ } 选择界面上处于禁用状态的元素
-
ele:enabled{ } 选择界面上处于可用状态的元素
-
ele:checked{ } 选择界面上处于被选中状态的元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.wrap div:first-of-type{
background-color: aqua;
}
.wrap p:first-of-type{
background-color: brown;
}
.wrap p:last-of-type{
background-color: blueviolet;
}
.wrap div:last-of-type{
background-color: cadetblue;
}
.wrap div:nth-of-type(3){
background-color: coral;
}
.wrap div:nth-last-of-type(2){
background-color: crimson;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="wrap">
<div>1</div>
<p>p1</p>
<div>2</div>
<h1>wwww</h1>
<div>3</div>
<span>span</span>
<div>4</div>
<div>5</div>
<div>6</div>
<p>p2</p>
<div>7</div>
<div>8</div>
</div>
</body>
</html>