1.css样式表的三种使用方式:
<!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>
/* 选择器, css样式表可以由很多选择器组成,选择器就是用来选择标签并给标签添加样式 */
a {
color:brown;
}
div{
width: 100px;
height: 100px;
background: blue;
}
</style>
<!-- 外链式 -->
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<!-- 行内式 -->
<p style="color: red;">我是一个段落标签</p>
<a href="http://www.baidu.com">百度</a>
</body>
</html>
2.标签选择器
<!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>
p {
color:red;
}
</style>
</head>
<body>
<!-- 标签选择器,就是以标签名开头,根据标签名来选择html文件中的标签,给标签添加样式 -->
<p>hello world</p>
<p>你好</p>
</body>
</html>
3.类选择器
<!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>
/* 类选择器,以点开头,根据类名选择标签,给标签添加样式 */
.myp{
color:red;
}
.mybg{
background: blue;
}
</style>
</head>
<body>
<p>xxxx</p>
<p class="myp">哈哈,我是一个段落标签</p>
<!-- 这里标签使用了两个类选择器 -->
<p class="myp mybg">哈哈,我是一个段落标签</p>
</body>
</html>
4.层级选择器
<!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 div{
color: green;
}
div .box2{
color: aquamarine;
}
div div p{
color: red;
}
</style>
</head>
<body>
<div>
<div>哈哈</div>
<div class="box2">嘻嘻</div>
<div>
<p>hello</p>
</div>
</div>
<div>哈哈</div>
<!-- 层级选择器不一定必须是父子关系,祖孙关系的子标签也可以找到,完成添加样式操作 -->
</body>
</html>
5.id选择器
<!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>
/* id选择器, 以#开头,根据id名选择html中的标签,给标签添加样式 */
#myid1{
color: blue;
}
</style>
</head>
<body>
<p id="myid1">哈哈</p>
<!-- 在html文件中标签的id是惟一的,不能重复,后续js会根据id获取对应的标签对象 -->
</body>
</html>
6.组选择器
<!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>
/* 组选择器就是多个选择器的组合,把功能的代码可以放到组选择器里面 */
/* 这里表达的意思是定义了三个类选择器,他们都有同样的样式规则 */
.box1, .box2, .box3 {
width: 100px;
height: 100px;
}
/* 提示:在css里面出现同样的选择器,后面的不会覆盖前面的,后面的选择器会在前面选择器的基础上追加样式 */
.box1{
background: red;
}
.box2{
background: blue;
}
.box3{
background: green;
}
</style>
</head>
<body>
<!-- div.box1*3:创建三个div并且指定class属性的名字是box1 -->
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>
7.伪类选择器
<!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{
width: 100px;
height: 100px;
background: green;
}
/* 伪类选择器就是给其它选择器添加特殊效果,表现形式为选择器后加冒号再跟上伪类名字 */
div:hover{
width: 200px;
height: 200px;
background: red;
}
</style>
</head>
<body>
<div>哈哈</div>
</body>
</html>
8.常用的布局样式属性
<!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>
.box{
width:100px;
height:100px;
background: green;
/*background: url(../day07+/876740312508119373.jpg);*/
/* 设置标签的四周边框大小 */
/*border:5px solid red;*/
/* 设置标签的最上面边框 */
border-top: 5px solid red;
border-bottom: 5px solid red;
border-left: 5px solid blue;
border-right: 5px solid blueviolet;
/* 设置浮动,只能设置左浮动和右浮动 */
/* float: right; */
}
.box1{
width: 200px;
height: 200px;
background: blue;
}
.box2{
width: 100px;
height: 100px;
background: yellow;
float:left
}
.box3{
width: 50px;
height: 50px;
background: gray;
float:right
}
</style>
</head>
<body>
<!-- 布局常用空间是div -->
<div class="box">哈哈</div>
<br>
<!-- div>div*2 创建一个父div,在父div中创建两个子div -->
<div class="box1">
<div class="box2"></div>
<div class="box3"></div>
</div>
</body>
</html>
9.常用的文本样式属性
<!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>
p{
color:red;
font-size: 30px;
font-weight:bold;
font-family: "Microsoft Yahei";
background: beige;
text-decoration: underline;
line-height: 100px;
text-align: center;
}
span{
color:gold;
}
a{
text-decoration: none;
}
</style>
</head>
<body>
<!-- span标签可以给一段文字中的其中一段数据修改样式 -->
<p>听说下雨天音乐和<span>辣条</span>更配呦~</p>
<a href="http://www.baidu.com">百度</a>
</body>
</html>
10.元素溢出
<!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>
.box1{
width:100px;
height:100px;
background:red;
/* 超出部分隐藏 */
overflow: hidden;
}
.box2{
width:200px;
height:50px;
background:greenyellow;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
11.显示特性
<!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>
.box1{
width:100px;
height:100px;
background: ghostwhite;
}
.box2{
width:100px;
height:100px;
background: rgb(87, 87, 183);
display: inline;
}
</style>
</head>
<body>
<div class="box1">
哈哈
</div>
<p>嘻嘻</p>
<div class="box2">
哈哈
</div>
<a href="https://www.baidu.com">百度</a>
</body>
</html>
12.盒子模型
<!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>
.box1{
width:200px;
height:200px;
background:goldenrod;
border:10px solid blueviolet;
padding: 10px;
}
.box2{
width: 100px;
height:150px;
background:green;
}
.box3{
width: 100px;
height:50px;
background:gray;
margin-top: 10px;
}
</style>
</head>
<body>
<!-- 盒子模型设置四部分内容:1.内容大小(width,height)2.边框大小(border)3.内边距(padding)4.外边距(margin) -->
<div class="box1">
<div class="box2"></div>
</div>
<div class="box3"></div>
</body>
</html>