CSS选择器
2.选择器
作用:选择页面上的某一个或者某一类元素
- 基本选择器
- 标签选择器: 选择一类标签 标签{}
- 类选择器: class:选择所有class属性一致的标签,跨标签 .类名{}
- Id选择器:全局唯一! #id名{}
标签选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*
标签选择器,会选择到页面上所有的这个标签的元素
*/
h1{
color: #561a2f;
background: aqua;
border-radius: 24px;
}
p{
font-size: 80px;
}
</style>
</head>
<body>
<h1>学习</h1>
<h1>学习</h1>
<p>Java</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>类选择器</title>
<style>
/* 类选择器的格式 .class的名称{}
好处,可以多个标签归类,是同一个class,可以复用
*/
.xiao{
color: beige;
}
.hua{
color: crimson;
}
</style>
</head>
<body>
<h1 class="xiao">标题1</h1>
<h1 class="hua">标题2</h1>
<h1 class="xiao">标题3</h1>
<p class="xiao">p标签</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>id选择器</title>
<style>
/*id选择器 :id必须保证全局唯一!
#id名称{}
不遵循就近原则,固定的
id选择器>class 选择器>标签选择器
*/
#xiao{
color: beige;
}
.style1{
color: crimson;
}
h1{
color: #de3848;
}
</style>
</head>
<body>
<h1 id="xiao" class="style1">标题1</h1>
<h1 class="style1">标题2</h1>
<h1 class="style1">标题3</h1>
<h1>标题4</h1>
<h1>标题5</h1>
</body>
</html>
优先级:id>class>标签
2.2层次选择器
-
后代选择器:在某个元素的后面 祖爷爷 爷爷 爸爸 你
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>层次选择器</title> <style> /*p{*/ /* color: aquamarine;*/ /*}*/ /*后代选择器*/ body p{ background: #561a2f; } </style> </head> <body> <p>p1</p> <p>p2</p> <p>p3</p> <ul> <li> <p>p4</p> </li> <li> <p>p5</p> </li> <li> <p>p6</p> </li> </ul> </body> </html> -
子选择器: 一代,儿子
-
body>p{ background: aqua; } -
相邻兄弟选择器 同辈
-
/*相邻兄弟选择器:只有一个相邻(向下)对下不对上*/ .active + p{ background: darkslategrey; } -
通用选择器
-
/*通用兄弟选择器,当前选中元素的向下的所有兄弟元素*/ .active~p{ background: blue; } -
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>层次选择器</title> <style> /*p{*/ /* color: aquamarine;*/ /*}*/ /*后代选择器*/ /*body p{*/ /* background: #561a2f;*/ /*}*/ /*子选择器*/ /*body>p{*/ /* background: aqua;*/ /*}*/ /*相邻兄弟选择器:只有一个相邻(向下)对下不对上*/ /*.active + p{*/ /* background: darkslategrey;*/ /*}*/ /*通用兄弟选择器,当前选中元素的向下的所有兄弟元素*/ .active~p{ background: blue; } </style> </head> <body> <p>p0</p> <p class="active">p1</p> <p>p2</p> <p>p3</p> <ul> <li> <p>p4</p> </li> <li> <p>p5</p> </li> <li> <p>p6</p> </li> </ul> <p class="active">p7</p> <p>p8</p> </body> </html>
2.3 结构伪类选择器
伪类:有条件的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>结构伪类选择器</title>
<style>
/*避免使用,class,id选择器*/
/*ul的第一个子元素*/
/*ul的最后一个子元素*/
/*ul的第一个子元素*/
ul li:first-child{
background: green;
}
/*ul的最后一个子元素*/
ul li:last-child{
background: aquamarine;
}
/* 选中p1:定位到父元素,选择当前的第一个元素
选择当前p元素的父级元素,选中父级元素的第一个,并且是当前元素才生效:按顺序选
*/
p:nth-child(1){
background: blue;
}
/*选中父元素,下的p元素的第二个,按类型选*/
p:nth-of-type(2){
background: azure;
}
</style>
</head>
<body>
<!--<h1>h1</h1>-->
<p>p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul>
</body>
</html>
2.4属性选择器(常用)
id+class结合
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>属性选择器</title>
<style>
.demo a{
float: left;
display: block;
height: 50px;
width: 50px;
border-radius: 10px;
background: aquamarine;
text-align: center;
color: green;
text-decoration: none;
margin-right: 5px;
font: bold 20px/50px Arial;
}
/* 属性名,属性名=属性值(正则表达式)*/
/* =是绝对等于*/
/* *=是包含这个等于*/
/* ^=以这个开头*/
/* $=以这个结尾*/
/* 存在id属性的元素 a[]{}*/
/* a[id]{*/
/* background: black;*/
/* }*/
/* id=first的元素*/
/* a[id=first]{*/
/* background: green;*/
/* }*/
/* class中有links的元素*/
/* a[class*="links"]{*/
/* background: lemonchiffon;*/
/* }*/
/* 选中herf中以http开关的元素*/
/* a[href^=http]{*/
/* background: aqua;*/
/* }*/
a[href$=pdf]{
background: blue;
}
</style>
</head>
<body>
<p class="demo">
<a href="http://www.baidu.com" class="links iten first" id="first">1</a>
<a href=""class="links item active" target="_blank" title="test">2</a>
<a href="images/122.html" class="links item active">3</a>
<a href="images/123.png" class="links item">4</a>
<a href="images/124.png" class="links item">5</a>
<a href="images/125.png" class="links item">6</a>
<a href="images/126.pdf" class="links item">7</a>
<a href="images/127.png" class="links item">8</a>
<a href="images/128.png" class="links item">9</a>
<a href="images/129.png" class="links item">10</a>
</p>
</body>
</html>
=
*=
^=
$=

浙公网安备 33010602011771号