一.简介
css:Cascading Style Sheets 层叠样式表
- 层叠:多个样式可以作用在同一个html标签上,同时生效
好处:
1. 功能强大
2. 将内容展示和样式控制分离
- 降低网页代码耦合度
- 分工协作,提高开发效率
二.结合方式
1.内联样式:
- 在标签内使用style属性指定css代码
<div style="color:red">hello css</div>
2.内部样式:
- 在head标签内,定义style标签,style标签体中的内容就是css代码
<style>
div{
color:blue;
}
</style>
3.外部样式:
-
定义css文件,在head标签中定义link标签,引入外部资源文件
<link rel="stylesheet" href="a.css" />
注意:
-
1,2,3种方式,css作用范围越来越大
-
1方式不常用,后期常用2,3
-
第3种格式可以写为:
<style> @import "css/a.css"; </style>
-
探究这三种方式的优先级。
--内联样式最高
--内部和外部没有明显的优先级,取决于谁后加载
三种结合方式如下:
a.css代码
p{
width: 200px;height: 100px;background: #fff819;/*黃色*/
}
div{
width: 200px;height: 100px;background: #0dedff;/*青色*/
}
html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>/*第二种内部样式*/
div{
width: 200px;height: 100px;background: #ff1c1c;/*红色*/
}
</style>
<link rel="stylesheet" href="CSS/a.css"><!--第三种外部样式-->
<style>/*第三种外部样式*/
@import "CSS/a.css";
</style>
</head>
<body>
<div style="width: 200px;height: 100px;background: #ff669d"><!--第一种内联样式 粉色-->
布局快
</div>
<!--第三种覆盖第二种,加载器从上向下加载,后面覆盖前面。-->
<div>
布局快
</div>
<!--第三种-->
<p>
段落标签
</p>
</body>
</html>
运行结果(建议使用谷歌浏览器)
三.选择器
语法格式:
选择器{
属性名1:属性值1;
属性名2:属性值2;
...
}
-- 选择器:筛选具有相似特征的元素
注意:每一对属性需要使用分号隔开,最后一对属性可以不加分号。
1.基础选择器
-
id选择器
#id值{} 建议在一个html页面中id值唯一。
-
元素选择器
标签名称{}
-
class选择器
.class属性值{}
-
优先级:
id选择器>class选择器>元素选择器
-
基础选择器相关代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#id{/*id选择器*/
width: 200px;
height: 100px;
background: #ff1c1c;/*红色*/
}
.class{/*class选择器*/
width: 200px;
height: 100px;
background: #0dedff;/*青色*/
}
div{/*元素选择器*/
width: 200px;
height: 100px;
background: #fff819;/*黄色*/
}
</style>
</head>
<body>
<div id="id">盒子1</div>
<div class="class">盒子2</div>
<div>盒子3</div>
</body>
</html>
运行结果:
2.扩展选择器
-
选择所有元素
*{}
-
并集选择器
选择器1,选择器2{}
-
交集选择器
选择器1选择器2{}
-
后代选择器
选择器1 选择器2{}
-
子代选择器
选择器1 > 选择器2{}
相关代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*并集选择器*/
div,p{
width: 200px;
height: 100px;
background: #ff1c1c;/*红色*/
}
/*交集选择器*/
div#box{
width: 200px;
height: 100px;
background: #fff819;/*黄色*/
}
/*后代选择器*/
body pre{
width: 200px;
height: 100px;
background: #0dedff;/*青色*/
}
/*子代选择器*/
p>span{
width: 200px;
height: 100px;
background: burlywood;/*棕色*/
}
</style>
</head>
<body>
<div>盒子1</div>
<div id="box">盒子2</div>
<div><pre>盒子3</pre></div>
<p>段落1</p>
<p><span>段落2</span></p>
</body>
</html>
-
属性选择器
[属性名="属性值"]{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
[name="name"]{
color: red;
font-size: 20px;
}
</style>
</head>
<body>
<from>
<input type="text" id="inp1" name="name" value="tom"><br>
<input type="text" id="inp2" name="name1" value="tom">
</from>
</body>
</html>
-
伪类选择器–状态选择器
元素:状态{} 如: :link{}--链接状态 :visited{}--被访问过的状态 :hover{}--鼠标悬停的状态 :active{}--正在访问的状态
- 案例
- 表格悬停变色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*链接状态*/
:link{
color: gold;/*黄色*/
}
/*被访问过状态*/
:visited{
color: #0dedff;/*青色*/
}
/*正在访问状态*/
a:active{
color: #ff1c1c;/*红色*/
}
table:hover{
color: #ff669d;/*粉色*/
}
</style>
</head>
<body>
<a href="1.三种结合方式.html">超链接</a><br>
<div>盒子</div><br>
<p>段落</p>
<table cellpadding="15" cellspacing="0" width="200" border="1">
<tr><td>100</td><td>200</td><td>300</td></tr>
</table>
</body>
</html>
四.属性
-
字体、文本
- font-size:字体大小
- color:文本颜色
- text-align:对齐方式
- line-height:行高
-
背景
- background
-
边框
- border:设置边框,是个复合属性
-
尺寸
width:宽度
height:高度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#id1{
width: 900px;
height: 500px;
border: 5px dashed red;/*dashed 边距虚线 红色*/
border-top: 20px solid blue;/*设置上边距 solid实线 蓝色*/
background-image: url("imgs/a1.jpg");
/*background-size: 900px 900px;*//*设置图片大小*/
background-repeat: no-repeat;/*不允许重复*/
background-color: #ff669d;/*背景色 粉色*/
}
#id2{
width: 900px;
height: 500px;
background-image: url("imgs/b.jpg");
border: 5px dashed chartreuse;/*绿色*/
font-size: 30px;
color: #ff1c1c;/*红色*/
text-align: center;
line-height: 60px;
}
</style>
</head>
<body>
<div id="id1">
盒子
</div>
<div id="id2">
床前明月光,<br>
疑是地上霜。
</div>
</body>
</html>
-
盒子模型
-
margin:外边距
-
padding:内边距
- 默认情况下内边距会影响整个盒子的大小
- box-sizing:border-box;设置盒子的属性,让width和height就是盒子的最终大小。
-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
border: 1px solid red;/*红色*/
}
.class1{
width: 300px;
height: 300px;
background-color: chartreuse;/*绿色*/
}
.class2{
width: 150px;
height: 150px;
background-color: #0dedff;/*青色*/
margin: 50px;
padding: 30px;
/*保持盒子的尺寸*/
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="class1">
<div class="class2">内盒子</div>
外盒子
</div>
</body>
</html>
-
float:浮动
- left
- right
-
定位
相对定位relative(相对于正常位置进行定位)
position: relative;
left: 200px;
绝对定位absolute(相对于 static 定位以外的第一个父元素进行定位)
固定定位fixed
属性:“left”, “top”, “right” 以及 “bottom”
拓展:
:nth-child(i)属性 获取选中元素中的第i个
定位练习:
- 将4个树立的正方形,变成一个大的正方形,如下图:
相对定位:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width: 200px;
height: 200px;
/*float: left;*//*左浮动*/
}
div:nth-child(1){
background-color: #0dedff;/*青色*/
position: relative;
}
div:nth-child(2){
background-color: #ff669d;/*粉色*/
position: relative;
left: 200px;
top: -200px;
}
div:nth-child(3){
background-color: #ff1c1c;/*红色*/
position: relative;
bottom: 200px;
}
div:nth-child(4){
background-color: #a3ff6f;/*绿色*/
position: relative;
bottom: 400px;
left: 200px;
}
</style>
</head>
<body>
<div>盒子1</div>
<div>盒子2</div>
<div>盒子3</div>
<div>盒子4</div>
</body>
</html>
绝对定位:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width: 200px;
height: 200px;
/*float: left;*//*左浮动*/
}
div:nth-child(1){
background-color: #0dedff;
position: absolute;
}
div:nth-child(2){
background-color: #ff669d;
position: absolute;
left: 200px;
}
div:nth-child(3){
background-color: #ff1c1c;
position: absolute;
top: 200px;
}
div:nth-child(4){
background-color: #a3ff6f;
position: absolute;
left: 200px;
top: 200px;
}
</style>
</head>
<body><div>盒子1</div><div>盒子2</div><div>盒子3</div><div>盒子4</div>
</body>
</html>
固定定位:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width: 200px;
height: 200px;
/*float: left;*//*左浮动*/
}
div:nth-child(1){
background-color: #0dedff;
position: fixed;
}
div:nth-child(2){
background-color: #ff669d;
position: fixed;
left: 200px;
}
div:nth-child(3){
background-color: #ff1c1c;
position: fixed;
top: 200px;
}
div:nth-child(4){
background-color: #a3ff6f;
position: fixed;
left: 200px;
top: 200px;
}
</style>
</head>
<body>
<div>盒子1</div>
<div>盒子2</div>
<div>盒子3</div>
<div>盒子4</div>
</body>
</html>
效果图(三个定位效果都一样)
作业
- 注册案例优化
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
<style>
div#div1{
width: 2000px;
height: 1000px;
background-image: url("../imgs/bg.jpg");
background-size: 2000px 1000px;
position: absolute;
left: 300px;
}
#div2{
width: 1200px;
height: 600px;
border: 5px dashed greenyellow;
/*position: absolute;*/
margin: 150px;
position: absolute;
left: 250px;
}
p{
position: absolute;
left: 950px;
top: -18px;
}
a{
position: absolute;
left: 1030px;
top: -2px;
}
form{
position: absolute;
left: 450px;
top: -60px;
}
img{
position: absolute;
top: 365px;
left: 220px;
}
</style>
</head>
<body >
<div id="div1">
<div id="div2">
新用户注册 <br>
USER REGISTER
<p>已有账号?</p>
<a href="#">立即登录</a>
<form action="../../web-01html/homework/one.html" method="post">
<table cellspacing="20">
<tr>
<td><label for="username">用户名</label></td>
<td><input id="username" type="text" name="username" placeholder="请输入账号"></td>
</tr>
<br>
<tr>
<td><lable for="password"></lable>密码</td>
<td><input id="password" type="password" name="pas" placeholder="请输入密码"></td>
</tr>
<br>
<tr>
<td><lable for="email"></lable>Email</td>
<td><input id="email" type="email" name="pas" placeholder="请输入邮箱"></td>
</tr>
<tr>
<td><label for="name">姓名</label></td>
<td><input id="name" type="text" name="name" placeholder="请输入姓名"></td>
</tr>
<tr>
<td><label for="tel">手机号</label></td>
<td><input id="tel" type="text" name="tel" placeholder="请输入手机号"></td>
</tr>
<tr>
<td><label >性别</label></td>
<td><input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女
</td>
</tr>
<td><label for="birth">出生日期</label></td>
<td><input id="birth" type="date" name="birth"></td>
<tr>
<td><label for="yzm">验证码</label></td>
<td><input id="yzm" type="text" name="yzm" size="10px" > <img src="../imgs/code.jpg" alt="" height="20" width="60"></td>
</tr>
<tr>
<td></td>
<td><input id="zc" type="submit" value="注册"></td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
效果图: