使用CSS样式有三种方式,请看下面这篇文章:https://blog.csdn.net/qq_39429962/article/details/82824537
1、元素选择器
语法:
E{...} /*E代表有效的HTML元素名*/
例如:
<!DOCTYPE html>
<html>
<head>
<title>内部样式测试</title>
<style type="text/css">
div{
background-color:#ccc;
height:300px;
width:50%;
}
</style>
</head>
<body>
<div>第一个div</div>
</body>
</html>
2、属性选择器
E{...} :该css样式对所有的E元素起作用
E[attr]{...}:对所有具有attr属性的E元素起作用
E{attr=value}:对所有具有attr属性,且attr属性值为value的E元素起作用
E{attr |=value}:对所有具有attr属性,且attr属性值为以|区分符,value为第一个的E元素起作用
E{attr^=value}:对所有具有attr属性,且attr属性值为以value开头的E元素起作用
E{attr$=value}:对所有具有attr属性,且attr属性值为以value结尾的E元素起作用
E{attr*=value}:对所有具有attr属性,且attr属性值为包含value的E元素起作用
3、ID选择器
#idValue{...}
实例:
<!DOCTYPE html>
<html>
<head>
<title>内部样式测试</title>
<style type="text/css">
#div{
background-color:#ccc;
height:300px;
width:50%;
}
</style>
</head>
<body>
<div id="div">第一个div</div>
</body>
</html>
4、class选择器
语法:
.calassValue{...}
实例:
<!DOCTYPE html>
<html>
<head>
<title>内部样式测试</title>
<style type="text/css">
.div{
background-color:#ccc;
height:300px;
width:50%;
}
</style>
</head>
<body>
<div class="div">第一个div</div>
</body>
</html>
5、包含选择器
selector1 selector2{...} /*selecctor1 selector2都是有效的选择器*/
实例:
<!DOCTYPE html>
<html>
<head>
<title>内部样式测试</title>
<style type="text/css">
/*对所有的div里且class为a起作用*/
div .a{
background-color:#ccc;
height:300px;
width:50%;
}
</style>
</head>
<body>
<div><section><div class="a">section中的div</div></section></div>
</body>
</html>
6、子选择器
语法:
Selector1>Selector2{...}
实例:
<!DOCTYPE html>
<html>
<head>
<title>内部样式测试</title>
<style type="text/css">
/*对所有的div里且class为a的子元素起作用*/
div>.a{
background-color:#ccc;
height:300px;
width:50%;
}
</style>
</head>
<body>
<div><section><p class="a">section中的div</p></section></div> <!--不起作用-->
<div><p class="a">section中的div</p></div> <!--起作用-->
</body>
</html>
7、组合器选择
语法:
Selector1,Selector2,Selector3{...}
实例:
<!DOCTYPE html>
<html>
<head>
<title>内部样式测试</title>
<style type="text/css">
/*对所有的div里且class为a的子元素起作用*/
div,.a,#p{
background-color:#ccc;
height:300px;
width:50%;
}
</style>
</head>
<body>
<div>section中的div></div> <!--起作用-->
<p class="a">123123123</p>
<a id="p">texttext</a>
</body>
</html>