CSS3详解
CSS3详解
HTML+CSS+JavaScript
结构+表现+交互
1.什么是CSS
CSS选择器(重点+难点)、美化网页(文字、阴影、超链接、列表、渐变....)、盒子模型、浮动、定位、网页动画(特效效果)
1.1什么是CSS
Cascading Style Sheet层叠样式表
CSS:表现(美化网页)
字体,颜色,边距,高度,宽度,背景图片,网页定位,网页浮动
1.2发展史
CSS1.0 加粗字体
CSS2.0 DIV(块)+CSS,HTML与CSS结构分离的思想,网页变得简单>利于SEO
CSS2.1 浮动、定位
CSS3.0 圆角、阴影、动画...浏览器兼容性
1.3快速入门
练习格式:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--规范,<style>可以编写CSS的代码,每一个声明最好以“;”结尾
语法:
选择器{
声明1;
声明2;
声明3;
}
-->
<style>
h1{
color: crimson;
}
</style>
</head>
<body>
<h1>CSS测试</h1>
</body>
</html>
建议使用这种规范

CSS的优势:
1、内容和表现分离;
2、网页结构表现统一,可以实现复用
3、样式十分的丰富
4、建议使用独立于html的css文件
5、利用SEO,容易被搜索引擎收录!
1.4CSS的3种导入方式
行内样式优先级最高;内部样式和外部样式的优先级取决于定义的顺序,先定义的优先级低(覆盖)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>导入方式</title>
<!--内部样式-->
<style>
h1{
color:green;
}
</style>
<!--外部样式-->
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<!--行内样式:在标签元素中编写一个style属性,编写样式即可-->
<h1 style="color:red;">我是标题</h1>
</body>
</html>
扩展:外部样式两种写法
- 链接式
link属于html标签,只能放入html源代码中使用
<!--外部样式-->
<link rel="stylesheet" href="css/style.css">
- 导入式
@import:CSS2.1特有
<style>
@import url("css/style.css");
</style>
2.选择器
作用:选择页面上的某一个或者某一类元素
2.1基本选择器
1.标签选择器:选择一类标签 标签{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>标签选择器</title>
<style>
/*标签选择器:会选择到页面上所有的这个标签元素*/
h1{
color:#1f794d;
}
p{
font-size: 80px;
}
</style>
</head>
<body>
<h1>学习JAVA</h1>
<h1>草莓熊</h1>
<p>我在沈阳东北大学</p>
</body>
</html>
2.类选择器class:选中所有class属性一致的标签,可以跨标签 .类名{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>类选择器</title>
<style>
/*类选择器的格式 .class的名称{}
好处:可以多个标签归类,是同一个class,可以复用
*/
.lin{
color: aqua;
}
.xue{
color: blueviolet;
}
</style>
</head>
<body>
<h1 class="lin">标题1</h1>
<h1 class="xue">标题2</h1>
<h1 class="lin">标题3</h1>
<p class="lin">p标签</p>
</body>
</html>
3.id选择器:全局唯一 #id名{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>id选择器</title>
<style>
/*id选择器 id必须保证全局唯一
#id名称{
}
*/
#hello{
color: hotpink;
}
.style1{
color: aquamarine;
}
h1{
color: gold;
}
</style>
</head>
<body>
<h1 id="hello">标题1</h1>
<h1 class="style1">标题2</h1>
<h1 class="style1">标题3</h1>
<h1>标题4</h1>
<h1>标题5</h1>
</body>
</html>
不遵循就近原则
优先级:id>class>标签
2.2层次选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>层次选择器</title>
<style>
p{
background: green;
}
</style>
</head>
<body>
<p>p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li><p>p4</p></li>
<li><p>p5</p></li>
<li><p>p6</p></li>
</ul>
</body>
</html>
1.后代选择器:在某个元素的后面
/*后代选择器*/
body p{
background: red;
}

2.子选择器:一代,只有儿子
/*子选择器*/
body>p{
background: #3cbda6;
}

3.相邻兄弟选择器,同辈
/*相邻兄弟选择器,只有一个,相邻向下*/
.active+p{
background: #a13d30;
}
<body>
<p>p0</p>
<p class="active">p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li><p>p4</p></li>
<li><p>p5</p></li>
<li><p>p6</p></li>
</ul>
<p class="active">p7</p>
<p>p8</p>
</body>

4.通用选择器
/*通用兄弟选择器,当前选中元素的向下的所有兄弟元素*/
.active~p{
background: chartreuse;
}

2.3结构伪类选择器
伪类:条件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>结构伪类选择器</title>
<!--避免使用class,id选择器-->
<style>
/*ul的第一个子元素*/
ul li:first-child{
background: coral;
}
/*ul的最后一个子元素*/
ul li:last-child{
background: mediumseagreen;
}
/*nth-of-type他是当前元素的兄弟元素的第n个,而nth-child是当前元素的兄弟节点的第n个当前元素。*/
/*选中p1:定位到父元素,选择当前的第一个元素
选择当前p元素的父级元素,选中父级元素的第一个,并且是p元素才生效!
*/
p:nth-child(1){
background: #4fa1bb;
}
/*选择父元素中第2个类型为p的元素*/
p:nth-of-type(2){
background: gold;
}
/*鼠标移到上面会有背景色*/
a:hover{
background: darksalmon;
}
</style>
</head>
<body>
<a href="">点击跳转</a>
<p>p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul>
<p class="active">p7</p>
<p>p8</p>
</body>
</html>

2.4属性选择器(常用)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>属性选择器</title>
<style>
.demo a{
float: left;
display: block;
height: 50px;
width: 50px;
background: dodgerblue;
border-radius: 10px;
margin-right: 5px;
text-decoration: none;
text-align: center;
font:bold 20px/50px Arial;
color:gainsboro;
}
/*
属性名,属性名=属性值(正则)
=表示绝对等于
*=表示包含
^=表示以...开头
$=表示以...结尾
*/
/*存在id属性的元素 a[]{}*/
a[id]{
background: yellow;
}
/*id=first的元素*/
a[id=first]{
background: lightpink;
}
/*class中含有links元素*/
a[class*="links"]{
background: orange;
}
/*选中href中以http开头的元素*/
a[href^="http"]{
background: skyblue;
}
a[href$="doc"]{
background: mediumseagreen;
}
</style>
</head>
<body>
<p class="demo">
<a href="http://www.baidu.com" class="links item first" id="first">1</a>
<a href="" class="links item active" target="_blank" title="test">2</a>
<a href="images/123.html" class="links item">3</a>
<a href="images/123.png" class="links item">4</a>
<a href="images/123.jpg" class="links item">5</a>
<a href="abc" class="links item">6</a>
<a href="a.pdf" class="links item">7</a>
<a href="/abcd.pdf" class="links item">8</a>
<a href="/abc.doc" class="links item">9</a>
<a href="/abcd.doc" class="links item last">10</a>
</p>
</body>
</html>

= 绝对等于
*= 包含这个元素
^= 以这个开头
$= 以这个结尾
3.美化网页元素
3.1为什么要美化网页
- 有效的传递页面信息
- 美化网页,页面漂亮才能吸引客户
- 凸显页面的主题
- 提高用户的体验
span标签:重点要突出的字,使用span标签套起来
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>span</title>
<style>
#title1{
font-size: 50px;
}
</style>
</head>
<body>
欢迎学习<span id="title1">JAVA</span>
</body>
</html>
3.2字体样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--
font-family:字体
font-size:字体大小
font-weight:字体粗细
color:字体颜色
-->
<style>
body{
font-family: "Arial Black",楷体;
color: saddlebrown;
}
h1{
font-size:50px;
}
.p1{
font-weight: bold;
}
</style>
</head>
<body>
<h1>故事介绍</h1>
<p class="p1">这个世界名为元泱境界,脉(本质为振动)是构成万物的基础。每隔333年,会有一个神秘而强大的异常生物重生,它就是魁拔!魁拔的每一次出现,都会给元泱境界带来巨大的灾难!即便是天界的神族,也在劫难逃。在天地两界各种力量的全力打击下,魁拔一次次被消灭,但又总是按333年的周期重新出现。魁拔纪元1664年,天神经过精确测算后,在魁拔苏醒前一刻对其进行毁灭性打击。但谁都没有想到,由于一个差错导致新一代魁拔成功地逃脱了致命一击。很快,天界魁拔司和地界神圣联盟均探测到了魁拔依然生还的迹象。因此,找到魁拔,彻底消灭魁拔,再一次成了各地热血勇士的终极目标。
</p>
<p>
在偏远的兽国窝窝乡,蛮大人和蛮吉每天为取得象征成功和光荣的妖侠纹耀而刻苦修炼,却把他们生活的村庄搅得鸡犬不宁。村民们绞尽脑汁把他们赶走。一天,消灭魁拔的征兵令突然传到窝窝乡,村长趁机怂恿蛮大人和蛮吉从军参战。然而,在这个一切都凭纹耀说话的世界,仅凭蛮大人现有的一块冒牌纹耀,不要说参军,就连住店的资格都没有。受尽歧视的蛮吉和蛮大人决定,混上那艘即将启程去消灭魁拔的巨型战舰,直接挑战魁拔,用热血换取至高的荣誉。
</p>
<p>
Love alters not with his brief hours and weeks.
But bears it out even to the edge of doom.
If this be error and upon me proved.
I never writ, nor no man ever loved.
</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title2</title>
<!--font:字体风格 字体粗细 字体大小/行高 字体-->
<style>
p{
font: oblique bolder 16px/50px "楷体";
}
</style>
</head>
<body>
<p>
在偏远的兽国窝窝乡,蛮大人和蛮吉每天为取得象征成功和光荣的妖侠纹耀而刻苦修炼,却把他们生活的村庄搅得鸡犬不宁。村民们绞尽脑汁把他们赶走。一天,消灭魁拔的征兵令突然传到窝窝乡,村长趁机怂恿蛮大人和蛮吉从军参战。然而,在这个一切都凭纹耀说话的世界,仅凭蛮大人现有的一块冒牌纹耀,不要说参军,就连住店的资格都没有。受尽歧视的蛮吉和蛮大人决定,混上那艘即将启程去消灭魁拔的巨型战舰,直接挑战魁拔,用热血换取至高的荣誉。
</p>
</body>
</html>
3.3文本样式
1.颜色 –> color, rgb(),rgba()
2.文本对齐方式 –> text-align:center
3.首行缩进 –> text-indent:2em
4.行高 –> line-height:300px;单行文字上下居中!line-height = height
5.下划线 –> text-decoration
6.文本图片水平对齐:vertical-align: middle;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文本样式</title>
<!--
颜色:
单词:#FFFFFF
RGB:0~F ,rgb(0,255,255)
RGBA:A(透明度):0~1,rgba(0,255,255,0.9)
text-align:排版(左右),居中、居右等
text-indent: 2em;段落首行缩进
line-height: 150px;
行高 和 块的高度一致,就可以上下居中
-->
<style>
h1{
color: rgba(0,255,255,0.9);
text-align: center;
}
.p1{
text-indent: 2em;
}
.p3{
background: blueviolet;
height: 300px;
line-height: 150px;
}
/*下划线*/
.l1{
text-decoration: underline;
}
/*中划线*/
.l2{
text-decoration: line-through;
}
/*上划线*/
.l3{
text-decoration: overline;
}
/* 水平对齐~ 参照物, a,b */
img,span{
vertical-align: middle;
}
/* a标签去除下划线 */
a{
text-decoration: none;
}
</style>
</head>
<body>
<a href="">a标签去除下划线</a>
<p class="l1">123321</p>
<p class="l2">223321</p>
<p class="l3">323321</p>
<h1>故事介绍</h1>
<p class="p1">这个世界名为元泱境界,脉(本质为振动)是构成万物的基础。每隔333年,会有一个神秘而强大的异常生物重生,它就是魁拔!魁拔的每一次出现,都会给元泱境界带来巨大的灾难!即便是天界的神族,也在劫难逃。在天地两界各种力量的全力打击下,魁拔一次次被消灭,但又总是按333年的周期重新出现。魁拔纪元1664年,天神经过精确测算后,在魁拔苏醒前一刻对其进行毁灭性打击。但谁都没有想到,由于一个差错导致新一代魁拔成功地逃脱了致命一击。很快,天界魁拔司和地界神圣联盟均探测到了魁拔依然生还的迹象。因此,找到魁拔,彻底消灭魁拔,再一次成了各地热血勇士的终极目标。
</p>
<p>
在偏远的兽国窝窝乡,蛮大人和蛮吉每天为取得象征成功和光荣的妖侠纹耀而刻苦修炼,却把他们生活的村庄搅得鸡犬不宁。村民们绞尽脑汁把他们赶走。一天,消灭魁拔的征兵令突然传到窝窝乡,村长趁机怂恿蛮大人和蛮吉从军参战。然而,在这个一切都凭纹耀说话的世界,仅凭蛮大人现有的一块冒牌纹耀,不要说参军,就连住店的资格都没有。受尽歧视的蛮吉和蛮大人决定,混上那艘即将启程去消灭魁拔的巨型战舰,直接挑战魁拔,用热血换取至高的荣誉。
</p>
<p class="p3">
Love alters not with his brief hours and weeks.
But bears it out even to the edge of doom.
If this be error and upon me proved.
I never writ, nor no man ever loved.
</p>
<p>
<img src="images/1.jpg" alt="">
<span>这是我喜欢的头像dsjfhskdjlhlsdkei</span>
</p>
</body>
</html>
3.4超链接伪类
/* 未访问的链接 */
a:link {color: #FF0000}
/* 已访问的链接,点击之后的状态*/
a:visited {color: #00FF00}
/* 鼠标移动到链接上,鼠标悬浮的状态*/
a:hover {color: #FF00FF}
/* 选定的链接,鼠标按住未释放的状态*/
a:active {color: #0000FF}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
/*默认的颜色*/
a{
text-decoration: none;
color:#000000;
}
/*鼠标悬浮的颜色(只需要记住这个)*/
a:hover{
color: orange;
font-size: 50px;
}
/*鼠标按住未释放的状态*/
a:active{
color: darkgreen;
}
/*已访问的链接*/
a:visited{
color: hotpink;
}
/* text-shadow: 阴影颜色,水平偏移(正数向右),垂直偏移(正数向下),阴影半径*/
#price{
text-shadow: skyblue 10px 10px 2px;
}
</style>
</head>
<body>
<a href="#">
<img src="images/a.png" alt="">
</a>
<p>
<a href="#">码出高效:Java开发手册</a>
</p>
<p>
<a href="#">作者:孤尽老师</a>
</p>
<p id="price">
¥99
</p>
</body>
</html>
3.5文字阴影
/* text-shadow: 阴影颜色,水平偏移(正数向右),垂直偏移(正数向下),阴影半径*/
#price{
text-shadow: skyblue 10px 10px 2px;
}
<p id="price">
¥99
</p>

3.6列表
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>列表样式</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<div id="nav">
<h2 class="title">全部商品分类</h2>
<ul>
<li><a href="#">图书</a> <a href="#">音响</a> <a href="#">数字商品</a></li>
<li><a href="#">家用电器</a> <a href="#">手机</a> <a href="#">数码</a></li>
<li><a href="#">电脑</a> <a href="#">办公</a></li>
<li><a href="#">家居</a> <a href="#">家装</a> <a href="#">厨具</a></li>
<li><a href="#">服饰鞋帽</a> <a href="#">个护化妆</a></li>
<li><a href="#">礼品箱包</a> <a href="#">中标</a> <a href="#">珠宝</a></li>
<li><a href="#">食品饮料</a> <a href="#">保健食品</a></li>
<li><a href="#">彩票</a> <a href="#">旅行</a> <a href="#">充值</a> <a href="#">票务</a></li>
</ul>
</div>
</body>
</html>
#nav{
width: 250px;
background: grey;
}
.title{
font-size: 18px;
font-weight: bold;
text-indent: 4em;
line-height: 35px;
/*颜色 图片 图片位置 平铺方式 */
background: red url("../images/down.jpg") 215px 5px no-repeat;
}
/*ul li
none:去掉圆点
circle:空心圆
decimal:数字
square:正方形
*/
ul{
background: grey;
}
ul li{
height: 30px;
list-style: none;
text-indent: 1em;
background-image: url("../images/right.jpg");
background-repeat: no-repeat;
background-position: 180px -1px;
}
a{
text-decoration: none;
font-size: 16px;
color:#000;
}
a:hover{
color:orange;
text-decoration: underline;
}

3.7背景
- 背景颜色:background
- 背景图片
<style>
div{
width: 1000px;
height: 700px;
border: 1px solid red;
background-image:url("images/desktop.jpg");
/*默认是全部平铺的 repeat*/
}
.div1{
background-repeat: repeat-x;/*水平平铺*/
}
.div2{
background-repeat: repeat-y;/*垂直平铺*/
}
.div3{
background-repeat: no-repeat;/*不平铺*/
}
</style>
3.8渐变
网址:Grabient
background-color: #D9AFD9;
background-image: linear-gradient(315deg, #D9AFD9 0%, #97D9E1 100%);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>渐变</title>
<!--径向渐变,圆形渐变-->
<style>
body{
background-color: #D9AFD9;
background-image: linear-gradient(315deg, #D9AFD9 0%, #97D9E1 100%);
}
</style>
</head>
<body>
哈哈哈哈
</body>
</html>
4.盒子模型
4.1什么是盒子模型

1.margin:外边距
2.padding:内边距
3.border:边框
4.2边框
border:粗细 样式 颜色
- 边框的粗细
- 边框的样式
- 边框的颜色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
<style>
/*body总有一个默认的外边框margin:0,常见操作*/
body{
margin: 0;
padding: 0;
text-decoration: none;
}
/*border:粗细 样式 颜色*/
#box{
width: 300px;
border: 1px solid red;
}
h2{
font-size: 16px;
background-color: mediumaquamarine;
line-height: 30px;
color: #fff;
}
form{
background: mediumaquamarine;
}
form div:nth-of-type(1) input{
border: 3px solid black;
}
form div:nth-of-type(2) input{
border: 3px dashed #4f0ebe;
}
form div:nth-of-type(3) input{
border: 2px dashed #f6a038;
}
</style>
</head>
<body>
<div id="box">
<h2>会员登录</h2>
<form action="#">
<div>
<span>用户名:</span>
<input type="text">
</div>
<div>
<span>密码:</span>
<input type="text">
</div>
<div>
<span>邮箱:</span>
<input type="text">
</div>
</form>
</div>
</body>
</html>
4.3外边距
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>外边距</title>
<style>
/*外边距的妙用:居中元素
margin: 0 auto;左右居中
*/
#box{
width: 300px;
border: 1px solid red;
margin: 0 auto;
}
/*
顺时针旋转
margin: 1px
margin: 1px 2px 3px;
margin: 1px 2px 3px 4px;
*/
h2{
font-size: 16px;
background-color: mediumaquamarine;
line-height: 30px;
color: #fff;
}
form{
background: mediumaquamarine;
}
input{
border: 2px solid black;
}
</style>
</head>
<body>
<div id="box">
<h2>会员登录</h2>
<form action="#">
<div>
<span>用户名:</span>
<input type="text">
</div>
<div>
<span>密码:</span>
<input type="text">
</div>
<div>
<span>邮箱:</span>
<input type="text">
</div>
</form>
</div>
</body>
</html>
margin-left/right/top/bottom–>表示四边,可分别设置,也可以同时设置如下
margin:0 0 0 0/*分别表示上、右、下、左;从上开始顺时针*/
/*左右居中*/
margin:0 auto /*auto表示左右自动*/
margin:4px/*表示上、右、下、左都为4px*/
margin:10px 20px 30px/*表示上为10px,左右为20px,下为30px*/
盒子的计算方式:元素到底多大

margin+border+padding+内容宽度
总结:
body总有一个默认的外边距 margin:0
常见操作:初始化
margin:0;
padding:0;
text-decoration:none;/*超链接去下划线*/
4.4圆角边框 border-radius
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>圆角边框</title>
<style>
/*border-radius: 50px 20px;左上右下 右上左下
border-radius: 50px 20px 10px 5px;左上开始顺时针
圆圈:圆角等于半径
*/
div{
width: 100px;
height: 100px;
border: 10px solid red;
border-radius: 50px 20px 10px 5px;
}
img{
border-radius: 75px 75px 75px 75px;
}
</style>
</head>
<body>
<div></div>
<img src="images/1.jpg" alt="头像">
</body>
</html>
半圆
div{
width: 100px;
height: 50px;
border: 10px solid red;
border-radius: 60px 60px 0px 0px;
}
4.5盒子阴影
box-shadow: 阴影颜色,水平偏移(正数向右),垂直偏移(正数向下),阴影半径
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>盒子阴影</title>
<!--使用margin:0 auto; 居中要求:块元素,块元素有固定宽度-->
<style>
img{
border-radius: 75px 75px 75px 75px;
box-shadow: green 10px 10px 20px;
}
</style>
</head>
<body>
<div style="text-align: center;margin: 0 auto">
<img src="images/1.jpg" alt="">
</div>
</body>
</html>
脚本之家:https://www.jb51.net/codes/
Element:https://element.eleme.cn/#/zh-CN或https://element-plus.org/zh-CN/
vue-element-admin:https://panjiachen.github.io/vue-element-admin-site/zh/
layui:http://layui.org.cn/index.html
5.浮动
5.1标准文档流

块级元素:独占一行 h1-h6、p、div、列表
行内元素:不独占一行span、a、img、strong
注:行内元素可以包在块级元素中,反之则不可以。
5.2diaplay
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>display</title>
<style>
/*
block 块元素
inline 行内元素
inline-block 是块元素,但是可以内联 ,在一行
none:消失
*/
div{
width: 100px;
height: 100px;
border: 1px solid red;
display: inline-block;
}
span{
width: 100px;
height: 100px;
border: 1px dashed red;
display: inline-block;
}
</style>
</head>
<body>
<div>块元素</div>
<span>span行内元素</span>
</body>
</html>
QQ会员页面导航练习

html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>QQ会员</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="wrap">
<!--头部-->
<header class="nav-header">
<div class="head-contain">
<a href="" class="top-logo"><img src="images/logo.png" width="145" height="90" /></a>
<nav class="top-nav">
<ul>
<li><a href="">功能特权</a> </li>
<li><a href="">游戏特权</a> </li>
<li><a href="">生活特权</a> </li>
<li><a href="">会员特权</a> </li>
<li><a href="">成长体系</a> </li>
<li><a href="">年费专区</a> </li>
<li><a href="">超级会员</a> </li>
</ul>
</nav>
<div class="top-right">
<a href="">登录</a>
<a href="">开通超级会员</a>
</div>
</div>
</header>
</div>
</body>
</html>
css文件
*{
padding:0;
margin: 0;
}
a{
text-decoration: none;
}
.nav-header{
height: 90px;
width: 100%;
background: rgba(0,0,0,.6);
}
.head-contain{
width: 1180px;
height: 90px;
margin: 0 auto;
text-align: center;
}
.top-logo,.top-nav,.top-nav li,.top-right{
height: 90px;
display: inline-block;
vertical-align: top;
}
.top-nav{
margin: 0 48px;
}
.top-nav li{
line-height: 90px;
width: 90px;
}
.top-nav li a{
display: block;
text-align: center;
font-size: 16px;
color: #fff;
}
.top-nav li a:hover{
color: blue;
}
.top-right a{
display: inline-block;
font-size: 16px;
text-align: center;
margin-top: 25px;
border-radius: 35px;
}
.top-right a:first-of-type{
width: 93px;
height: 40px;
font-weight: 700;
line-height: 40px;
color: #fad65c;
border: 1px #fad65c solid;
}
.top-right a:first-of-type:hover{
color: #986b0d;
background: #fad65c;
}
.top-right a:last-of-type{
width: 140px;
height: 40px;
font-weight: 700;
line-height: 40px;
background: #fad65c;
color: #986b0d;
}
.top-right a:last-of-type:hover{
background: #fddc6c;
}

5.3float:left/right左右浮动
html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>浮动</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="father">
<div class="layer01"><img src="images/1.jpg" alt=""></div>
<div class="layer02"><img src="images/2.jpg" alt=""></div>
<div class="layer03"><img src="images/3.jpg" alt=""></div>
<div class="layer04">
浮动的盒子可以向左浮动,也可以向右浮动,知道它的外边缘碰到包含或另一个浮动盒子为止
</div>
</div>
</body>
</html>
css文件
div{
margin: 10px;
padding: 5px;
}
#father{
border: 1px #000 solid;
}
.layer01{
border: 1px #F00 dashed;
display: inline-block;
float: left;/*向左浮动*/
clear: both;/*清楚浮动*/
}
.layer02{
border: 1px #00F dashed;
display: inline-block;
float: right;
clear: both;
}
.layer03{
border: 1px #060 dashed;
display: inline-block;
float: left;
clear: both;
}
.layer04{
border: 1px #666 dashed;
font-size: 12px;
line-height: 23px;
float: left;
clear: both;
}
clear:
right:右侧不允许有浮动元素
left:左侧不允许有浮动元素
both:两侧不允许有浮动元素
none:
5.4overflow及父级边框塌陷问题
解决塌陷问题方案:
方案一:增加父级元素的高度
#father{
border: 1px #000 solid;
height: 800px;
}
方案二:增加一个空的div标签,清除浮动


方案三:在父级元素中增加一个overflow:hidden
overflow:hidden /*内容会被修剪,并且其余内容是不可见的*/
overflow:scoll /*内容会被修剪,但是浏览器会显示滚动条以便查看其余的内容*/
overflow:auto /*如果内容被修剪,则浏览器会显示滚动条以便查看其余的内容。*/
#father{
border: 1px #000 solid;
overflow: hidden;
}
overflow示例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>overflow</title>
<style>
#content{
width: 200px;
height: 150px;
overflow: scroll;
}
</style>
</head>
<body>
<div id="content">
<img src="images/1.jpg" alt="">
<p>
我国始终不渝推进全民健康覆盖,大力推进健康扶贫,接续乡村振兴,基层医疗卫生条件得到极大改善,“因病致贫”“因病返贫”得到有效阻断,基本医疗卫生服务均等化程度持续提高。
</p>
</div>
</body>
</html>
方案四:父类添加一个伪类:after
#father:after{
content:'';
display:block;
clear:both;
}
小结:
- 浮动元素增加空div---->简单、代码尽量避免空div
- 设置父元素的高度----->简单,元素假设没有了固定的高度,就会超出
- overflow---->简单,下拉的一些场景避免使用
- 父类添加一个伪类:after(推荐)---->写法稍微复杂,但是没有副作用,推荐使用
5.5dispaly与float对比
- display:方向不可以控制
- float:浮动起来会脱离标准文档流,所以要解决父级边框塌陷的问题。
6.定位
6.1相对定位-relative
相对于原来的位置进行指定的偏移;相对定位仍然在标准文档流中,原来的位置会被保留
position: relative;
top: -20px; /*向上偏移20px*/
left: 20px; /*向右偏移20*/
bottom: -20px; /*向下偏移20px*/
right: 20px; /*向左偏移20px*/
/*top: -20px;= bottom:20px;*/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>相对定位</title>
<!--相对定位
相对于自己原来的位置进行偏移~-->
<style>
div{
margin: 10px;
padding: 5px;
font-size: 16px;
line-height: 25px;
}
#father{
border: 1px solid #666;
padding: 0;
}
#first{
background-color: lightseagreen;
border: 2px dashed orange;
position: relative;/*相对定位:上下左右*/
/*top: -20px;*/
left: 20px;/*相对于原来的位置,正数右移*/
bottom:20px;/*top: -20px;= bottom:20px;*/
}
#second{
background-color: rgba(91,114,187,0.64);
border: 2px dashed hotpink;
}
#third{
background-color: #c0257f;
border: 2px dashed dodgerblue;
position: relative;
bottom: -20px;
right: 20px;
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
</html>
练习题:
- 使用div和a标签布局页面
- 每个超链接宽度和高度都是100px,背景颜色粉色,鼠标指针移上去变为蓝色
- 使用相对定位改变每个超链接的位置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>格子练习</title>
<style>
#box{
width: 300px;
height: 300px;
border: 2px solid red;
padding: 10px;
}
a{
text-decoration: none;
width: 100px;
height: 100px;
background-color: hotpink;
line-height: 100px;
color: white;
display: block;
text-align: center;
}
div a:hover{
background-color: dodgerblue;
}
.a2{
position: relative;
left:200px;
bottom: 100px;
}
.a4{
position: relative;
left:200px;
bottom: 100px;
}
.a5{
position: relative;
top: -300px;
right: -100px;
}
</style>
</head>
<body>
<div id="box">
<a class="a1" href="#">链接1</a>
<a class="a2" href="#">链接2</a>
<a class="a3" href="#">链接3</a>
<a class="a4" href="#">链接4</a>
<a class="a5" href="#">链接5</a>
</div>
</body>
</html>

6.2绝对定位-absolute
定位:基于xxx定位,上下左右~
- 没有父级元素定位的前提下,相对于浏览器定位
- 假设父级元素存在定位,通常相对于父级元素进行偏移
- 在父级元素范围内移动
总结:相对一父级或浏览器的位置进行指定的偏移;绝对定位不在标准文档流中,原来的位置不会被保留
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绝对定位</title>
<style>
div{
margin: 10px;
padding: 5px;
font-size: 16px;
line-height: 25px;
}
#father{
border: 1px solid #666;
padding: 0;
position: relative;
}
#first{
background-color: lightseagreen;
border: 2px dashed orange;
}
#second{
background-color: rgba(91,114,187,0.64);
border: 2px dashed hotpink;
position: absolute;
left: -30px;
top:10px;
}
#third{
background-color: #c0257f;
border: 2px dashed dodgerblue;
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
</html>
6.3固定定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>固定定位</title>
<style>
body{
height:1000px
}
div:nth-of-type(1){/*绝对定位:没有相对的父级元素,所以相对于浏览器*/
width: 100px;
height: 100px;
background-color: red;
position: absolute;
right:0;
bottom:0;
}
div:nth-of-type(2){/*固定定位*/
width: 50px;
height: 50px;
background-color: yellow;
position: fixed;
right:0;
bottom:0;
}
</style>
</head>
<body>
<div>div1</div>
<div>div2</div>
<div>div3</div>
</body>
</html>
6.4z-index

图层~
z-index:默认是0,最高无限~999
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>z-index</title>
<style>
#app{
width: 615px;
padding: 0px;
margin: 0px;
overflow: hidden;
font-size: 18px;
line-height: 30px;
border: 2px solid red;
}
ul,li{
padding: 0px;
margin: 0px;
list-style: none;
}
/*父级元素相对定位*/
#app ul{
position: relative;
}
.tipText,.tipBg{
position: absolute;
width: 630px;
height: 30px;
bottom:70px
}
.tipText{
z-index: 999;
color: white;
}
.tipBg{
background-color: black;
opacity: 0.5;/*背景透明度*/
filter: alpha(opacity=50);/*IE8 以及更早的版本支持替代的 filter 属性*/
}
</style>
</head>
<body>
<div id="app">
<ul>
<li><img src="images/amy.jpg" alt=""></li>
<li class="tipText">学习css,找前端工作</li>
<li class="tipBg"></li>
<li>时间:2023年4月7日19:14:48</li>
<li>地点:信息楼A313</li>
</ul>
</div>
</body>
</html>
7.动画
@keyframes
CSS3动画 | HTML5资源教程 (html5tricks.com)
浙公网安备 33010602011771号