css选择器用于明确当前css样式,尤其是内联样式表和外联样式表考虑当前css样式修饰的是那一个,或者那些HTML标签
1.三个基本选择器
元素选择器:
HTML标签名作为选择器名字,可以用于修饰所有的对应当前签名的HTML标签
ID选择器:
针对HTML标签ID属性进行选择修饰,并且ID属性具有唯一性
class选择器:
针对HTML标签class属性进行选择修饰,class属性可以复用多个HTML标签
以上三个选择器权重排序
id>class>元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>04-三大基本选择器</title>
<style>
/* 标签选择器 */
div {
font-size: 50px;
color: red;
}
/* ID选择器 【要求】#号开头*/
#id1 {
font-size: 30px;
color: brown;
}
#id2 {
font-size: 30px;
color: red;
}
#id3 {
font-size: 30px;
color: blueviolet;
}
#id4 {
font-size: 60px;
color: indianred;
}
/* class选择器 */
.cls1 {
font-size: 30px;
color: chocolate;
}
</style>
</head>
<body>
<!-- 标签选择器 -->
<div>烤羊排</div>
<div>羊肉串</div>
<div>羊肉汤</div>
<!-- ID选择器 【要求】ID不可以重复-->
<span id="id1">胡辣汤</span>
<span id="id2">羊杂汤</span>
<span id="id3">羊肉烩面</span>
<!-- class选择器 class属性可以复用 -->
<div id="id4" class="cls1">擀面皮</div>
<div class="cls1">肉夹馍</div>
<div class="cls1">冰峰</div>
<div class="cls1">油泼面</div>
</body>
</html>
2.属性选择器
根据属性名以及属性值进行选择元素,from==>input标签
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>05-属性选择器</title>
<style>
/* 属性选择器 */
/* 根据属性名和属性值来约束修饰的HTML标签 */
input[type='text'] {
background-color: lightpink;
}
input[type='password'] {
background-color: #7c1823;
}
/* 根据属性名来约束修饰的HTML标签 */
font[size] {
color: greenyellow;
}
font[face] {
color: #7c1823;
font-size: 30px;
}
</style>
</head>
<body>
<font size="7">你好CSS</font> <br>
<font face="黑体">你好CSS</font> <br>
<form action="02-特殊字符.html" method="get">
<span>姓名:</span><input type="text" name="name"><br>
<span>密码:</span><input type="password" name="password"><br>
<span>头像:</span><input type="file" name="icon"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
3.伪类选择器
伪类选择器语法
静止状态:a:link
鼠标悬浮:a:hover
触发状态:a:active
已访问状态:a:visited
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
a {
text-decoration: none;
}
/*
a标签伪类选择器是有严格顺序的
hover必须跟在link和visited的后面
active必须跟在hover的后面
*/
a:link {
color: chocolate;
}
a:visited {
color: purple;
}
a:hover {
color: lightpink;
}
a:active {
color: greenyellow;
}
</style>
</head>
<body>
<h1><a href="http://www.dangdang.com">当当网</a></h1>
</body>
</html>
4.层级选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>07-层级选择器</title>
<style>
/* 层级选择器 */
#id1 .cls1 {
color: red;
font-size: 30px;
}
#id2 .cls1 {
color: greenyellow;
font-size: 50px;
}
</style>
</head>
<body>
<div id="id1">
<div class="cls1">
82的农夫山泉
</div>
</div>
<div id="id2">
<div class="cls1">
20年雪碧
</div>
</div>
</body>
</html>
5.组合选择器
1.CSS样式中多个选择器使用
2.HTML标签class属性可以有多个值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>08-组合选择器</title>
<style>
div, span, p, strong {
color: red;
font-size: 30px;
}
.size_class {
width: 200px;
height: 200px;
}
.font_class {
color: #7c1823;
font-size: 30px;
}
.bg_class {
background-color: greenyellow;
}
</style>
</head>
<body>
<div>外星人</div>
<span>MacBook Pro</span>
<p>ThinkPad P</p>
<strong>神舟笔记本</strong>
<hr>
<!-- class属性有多个,空格隔开 -->
<div class="size_class font_class">烤面筋</div>
<div class="size_class bg_class">炸鸡</div>
<hr>
</body>
</html>