HTML标记定义
格式
其实上节课已经使用过。
<p>…</p>
p{属性:属性值;属性1:属性值1;}
p可以叫做选择器,定义那个标记中的内容执行其中的样式
一个选择器可以控制若干个样式属性,他们之间需要用英语的“;”隔开,左后一个可以不加“;”。
代码
<!doctype html>
<html>
<head>
<title>CSS自定义样式</title>
<meta charset="utf-8">
<style type="text/css">
p{
color:red;
}
</style>
</head>
<body>
<p>我是一段话</p>
</body>
</html>
显示结果
Class定义
格式
<p class=”a” >…<p>
class定义是以“.”开始
.a{属性:属性值;属性1:属性值1;}
class可以重复使用,也就是可以作用于其他任何标签
代码
<!doctype html>
<html>
<head>
<title>CSS自定义样式</title>
<meta charset="utf-8">
<style type="text/css">
.a{color:green;font-size:30px;}
/*.a p{color:red;font-size:30px;}*/
.b{color:blue;font-size:30px;}
</style>
</head>
<body>
<p class="a">我是一段话</p>
<div class="a">我是第二段话</p>
<!--如果要单独定义div下的p标记
1..a p{color:red;font-size:30px;}
2.class="b"
.b{color:blue;font-size:30px;}
-->
<div class="a">
<p class="b">我是第四段话</p>
我是第三段话
</p>
</body>
</html>
显示结果
ID定义
格式
<p id=”a” >…</p>
ID是“#”开始的
#a{属性:属性值;属性1:属性值1;}
与class是一样的
值得注意的是ID选择器是唯一的,通常情况下一个页面智能出现一次这个选择器。
代码
<!doctype html>
<html>
<head>
<title>CSS自定义样式</title>
<meta charset="utf-8">
<style type="text/css">
#a{color:red;font-size:30px;}
</style>
</head>
<body>
<p id="a">我是一段话</p>
</body>
</html>
显示结果
省略
优先级问题
ID>Class>HTML
同级选择离元素最近的一个
代码
<!doctype html>
<html>
<head>
<title>CSS自定义样式</title>
<meta charset="utf-8">
<style type="text/css">
#a{color:red;font-size:30px;}
.b{color:green;font-size:30px;}
p{color:yellow;font-size:30px;}
div{color:yellow;font-size:30px;}
div{color:red;font-size:30px;}
</style>
</head>
<body>
<p id="a" class="b">我是一段话</p><!--red-->
<p id="a" >我是第二段话</p><!--red-->
<p class="b" >我是第三段话</p><!--green-->
<p >我是第四段话</p><!--yellow-->
<div>我是第四段话</div><!--red-->
</body>
</html>
显示结果
省略
组合选择器
同步控制多个元素
h1,h2,h3{属性:属性值;属性1:属性值1;}
选择组合器可以使用“,”隔开
代码
<!doctype html>
<html>
<head>
<title>CSS自定义样式</title>
<meta charset="utf-8">
<style type="text/css">
h1,h2,h3,h4,.b,#a{color:red;}
</style>
</head>
<body>
<h1>大家一起学习</h1>
<h2>大家一起学习</h2>
<h3>大家一起学习</h3>
<h4>大家一起学习</h4>
<p id="a">大家一起学习</p>
<p class="b">大家一起学习</p>
</body>
</html>
显示结果
伪元素选择器
- a:link 正常连接的样式
- a:hover 鼠标放上去的样式
- a:active 选择链接时的样式
- a:visited 已经访问过得链接样式
代码
<!doctype html>
<html>
<head>
<title>CSS自定义样式</title>
<meta charset="utf-8">
<style type="text/css">
a:link{color:red;}
a:hover{color:green;}
a:active{color:yellow;}
a:visited{color:blue;}
</style>
</head>
<body>
<a href="http://www.baidsu.com" target="_blank">百du</a>
</html>
显示结果
省略……