HTML+CSS+JavaScript
结构 + 表现+ 交互
1. 什么是CSS?
1.1 什么是CSS
css:cascading style sheets
层叠 样式 表
作用:美化网页
字体、颜色、边距、高度、宽度、背景图片、网页定位、网页浮动
比如百度的网页:
百度百科:
层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。
CSS不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。CSS 能够对网页中元素位置的排版进行像素级精确控制,支持几乎所有的字体字号样式,
拥有对网页对象和模型样式编辑的能力。
1.2 CSS的发展史
CSS1.0
CSS2.0 DIV(块)+CSS ,HTML与CSS结构分离的思想,网页变得简单,SEO
CSS2.1 浮动,定位
CSS3.0 圆角,阴影,动画。。。。浏览器兼容性
1.3 CSS怎么用(快速入门)?
基本入门
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--规范 <style>可以编写CSS代码,每个声明最好使用分号结尾
语法:
选择器{
声明1;
声明2;
声明3;
}
-->
<style>
h1 {
color: brown;
}
</style>
</head>
<body>
<h1>我是标题</h1>
</body>
</html>
或者:(建议使用这种规范)
结果:
CSS的优势:
- 内容和表现分离
- 网页结构表现统一,可以实现复用
- 样式十分丰富
- 建议使用独立于html的css文件
- 利用SEO,容易被搜索引擎收录
1.4 CSS的三种导入方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="style.css">
<!--内部样式-->
<style>
h1{
color: blue;
}
</style>
</head>
<!--优先级:就近原则-->
<body>
<!--行内样式:在标签元素中,编写一个style属性,编写样式即可-->
<h1 style="color: brown" >我是标题</h1>
<!--<h1>我是标题</h1>-->
</body>
</html>
拓展: 外部样式两种写法
- 链接式
/*链接式*/
<link rel="stylesheet" href="style.css">
- 导入式
@import是CSS 2.1特有的
<!--导入式-->
<style>
@import url("style.css ");
</style>
2. CSS选择器(重点+难点)*
作用:选择页面上的某一个或者某一类元素
2.1 基本选择器
2.1.1. 标签选择器:选择一类标签 标签{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*标签选择器,会选择到页面上所有的这个标签的元素*/
h1{
color: white;
background: darkgreen;
border-radius: 19px;
}
p{
font-size: 20px;
}
</style>
</head>
<body>
<h1>java1</h1>
<h1>java2</h1>
<p>第一段落</p>
</body>
</html>
结果:
2.1.2. 类选择器 class :选择所有class属性一致的标签,跨标签 .类名{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*类选择器的格式 .class的名称{}
好处:可以多个标签归类,是同一个class,可以复用
*/
.haha {
color: darkgreen;
}
.lala {
color: brown;
}
.nana{
color: burlywood;
}
</style>
</head>
<body>
<h1 class="haha">java1</h1>
<h1 class="lala">java2</h1>
<h1 class="nana">java3</h1>
<p class="lala">第一段落</p>
</body>
</html>
结果:
2.1.3. ID选择器 :全局唯一! #id名{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/* id选择器:id不能复用,必须保证全局唯一!!
#id名称{}
优先级:不遵循就近原则
id选择器>类选择器>标签选择器
*/
.aaa {
color: darkgreen;
}
#x{
color: burlywood;
}
h1{
color: blueviolet;
}
</style>
</head>
<body>
<h1 class="aaa" id="x" >java1</h1>
<h1 class="aaa" id="c">java2</h1>
<h1 id="v">java3</h1>
<h1 id="b">java4</h1>
</body>
</html>
结果:
优先级:id选择器>类选择器>标签选择器
2.2 层次选择器
原始情况:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
p{
background: darkgreen;
}
</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>
结果:
2.2.1 后代选择器
在某个元素的后面
/*后代选择器*/
body p{
background: blueviolet;
}
结果:
2.2.2 子选择器:一代
/*子选择器*/
body>p{
background: burlywood;
}
2.2.3 相邻兄弟选择器:同辈(向下相邻)
/*相邻弟选择器*/
.xx+p{
background: cyan;
}
2.2.4 通用选择器
/*通用兄弟选择器,选中元素的向下的所有兄弟元素*/
.xx~p{
background: darkorange;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*通用兄弟选择器*/
.xx~p{
background: darkorange;
}
</style>
</head>
<body>
<p class="xx">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>
2.3 结构伪类选择器
/*ul的第一个子元素*/
li:first-child {
background: darkorange;
}
/*ul的最后一个子元素*/
li:last-child{
background:green;
}
/*选中p1:定位到父元素,选择当前的第一个元素
选择当前p元素的父级元素,选中父级元素的第一个,并且是当前元素才生效!
*/
p:nth-child(2){
background: gold;
}
/*选中父元素下的p元素的第一个,类型*/
p:nth-child(1){
background: rebeccapurple;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*ul的第一个子元素*/
li:first-child {
background: darkorange;
}
/*ul的最后一个子元素*/
li:last-child{
background:green;
}
/*选中p1:定位到父元素,选择当前的第一个元素
选择当前p元素的父级元素,选中父级元素的第一个,并且是当前元素才生效!
*/
p:nth-child(2){
background: gold;
}
/*选中父元素下的p元素的第一个,类型*/
p:nth-of-type(1){
background: rebeccapurple;
}
</style>
</head>
<body>
<p>p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul>
</body>
</html>
结果:
2.4 属性选择器(常用)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.demo a{
float: left;
display: block;
height: 50px;
width: 50px;
border-radius: 10px;
background: green;
text-align: center ;
color: gold;
text-decoration: none ;
margin-right: 5px;
font: bold 20px/50px Arial ;
}
/*!*id=first的元素*!*/
/*a[id=first]{*/
/* background: blueviolet;*/
/*}*/
/*!*class中有link的元素*!*/
/*a[class*="link" ]{*/
/* background: blue;*/
/*}*/
/*!* ^选中herf中以http开头的元素*!*/
/*a[href^=http ]{*/
/* background: brown ;*/
/*}*/
/* $选中herf中以jpg结尾的元素*/
a[href$=jpg ]{
background: cornflowerblue ;
}
</style>
</head>
<body>
<p class="demo" >
<a href="http://www.baidu.com" class="link item first" id="first">1</a>
<a href="" class="link item first" >2</a>
<a href="image/123.html" class="link item first" >3</a>
<a href="image/123.png" class="link item first" >4</a>
<a href="image/123.jpg" class="link item first" >5</a>
<a href="abc" class="link item first" >6</a>
<a href="/a.pdf" class="link item first" >7</a>
<a href="/abc.pdf" class="link item first" >8</a>
<a href="abc.doc" class="link item first" >9</a>
<a href="d.doc" class="link item first" >10</a>
</p>
</body>
</html>
结果:
正则表达式
= 绝对
*= 含有
^= 开头
$=结尾
3. 美化网页元素(文字、阴影、超链接、列表)
3.1 为什么要美化网页?
- 有效地传递信息
- 美化网页,页面漂亮才能吸引用户
- 凸显页面的主题
- 提高用户的体验
span标签:重点要突出的字,使用span套起来
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#title1 {
font-size: 50px;
}
</style>
</head>
<body>
欢迎学习 <span id="title1" >java</span>
</body>
</html>
结果:
3.2 字体样式
font-family:字体
font-size:字体大小
font-weight:字体的粗细
color:字体颜色
text-indent:文字缩进
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--
font-family:字体
font-size:字体大小
font-weight:字体的粗细
color:字体颜色
text-indent:文字缩进
-->
<style>
body{
font-family:"Times New Roman" ,楷体 ;
color: darkred;
text-indent: 2em;
}
h1{
font-size: 50px;
}
.p1 {
font-weight:bold;
}
</style>
</head>
<body>
<h1> 蜘蛛侠</h1>
<p class="p1">
彼得·帕克(Peter Parker)即蜘蛛侠(Spider-Man),是美国漫威漫画旗下超级英雄,由编剧斯坦·李和画家史蒂夫·迪特科联合创造,初次登场于《惊奇幻想》(Amazing Fantasy)第15期(1962年8月),因为广受欢迎,几个月后,便开始拥有以自己为主角的单行本漫画。
</p>
<p>
蜘蛛侠全名彼得·本杰明·帕克(Peter Benjamin Parker),是美国的一名普通学生,毕业后成为《号角日报》的记者。由于意外被一只受过放射性感染的蜘蛛咬到,因此获得了和蜘蛛一样的超能力、超人般的耐力、反应、敏捷和速度。并发明了蛛网发射器,从此化身蜘蛛侠(Spider-Man)守卫纽约。
</p>
<p >
Give me the strength lightly to bear my joys and sorrows.Give me the strength to make my love fruitful in service.
</p>
<p >
Give me the strength never to disown the poor or bend my knees before insolent might.
</p>
<p >
Give me the strength to raise my mind high above daily trifles.
</p>
<p >
And give me the strength to surrender my strength to thy will with love.
</p>
</body>
</html>
结果:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--字体风格 oblique斜体-->
<style>
p{
font:oblique lighter 30px "Times New Roman" ;
}
</style>
</head>
<body>
<p>
I came out on the chariot of the first gleam of light, and pursued my voyage through the wildernesses of worlds leaving my track on many a star and planet.
</p>
</body>
</html>
结果:
3.3 文本样式
- 颜色:color rgb rgba
- 文本对齐的方式:text-align
- 首行缩进:text-indent
- 行高: line-height
- 装饰:text-decoration
- 文本图片水平对齐:vertical-align:middle
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--颜色:
单词
RGB
RGBA A:0~1表示透明度
text-align:排版,居中
text-indent:2em,首行缩进
height:块高
line-height:行高
和块的高度一致就可以上下居中
text-decoration:划线 :上中下
vertical-align: 和参照物对齐
-->
<style>
h1{
color: rgba(16, 52, 13, 0.9);
text-align: center ;
}
.p1{
text-indent: 2em;
}
.p3{
background: darkred;
color: white;
height: 50px;
line-height:50px;
}
/*上划线*/
.l1 {
text-decoration: overline;
}
/*中划线*/
.l2 {
text-decoration: line-through ;
}
/*下划线*/
.l3 {
text-decoration: underline ;
}
img,span{
vertical-align: middle;
}
</style>
</head>
<body>
<h1> 蜘蛛侠</h1>
<p class="p1">
彼得·帕克(Peter Parker)即蜘蛛侠(Spider-Man),是美国漫威漫画旗下超级英雄,由编剧斯坦·李和画家史蒂夫·迪特科联合创造,初次登场于《惊奇幻想》(Amazing Fantasy)第15期(1962年8月),因为广受欢迎,几个月后,便开始拥有以自己为主角的单行本漫画。
</p>
<p>
蜘蛛侠全名彼得·本杰明·帕克(Peter Benjamin Parker),是美国的一名普通学生,毕业后成为《号角日报》的记者。由于意外被一只受过放射性感染的蜘蛛咬到,因此获得了和蜘蛛一样的超能力、超人般的耐力、反应、敏捷和速度。并发明了蛛网发射器,从此化身蜘蛛侠(Spider-Man)守卫纽约。
</p>
<p class="p3">
Give me the strength lightly to bear my joys and sorrows.Give me the strength to make my love fruitful in service.
</p>
<p class="p3">
Give me the strength never to disown the poor or bend my knees before insolent might.
</p>
<p class="p3">
Give me the strength to raise my mind high above daily trifles.
</p>
<p class="p3">
And give me the strength to surrender my strength to thy will with love.
</p>
<p class="l1" >123</p>
<p class="l2" >123</p>
<p class="l3" >123</p>
<p>
<img src="images/a.jpg" alt="">
<span>give me the strength</span>
</p>
</body>
</html>
结果:
3.4 超链接伪类
正常情况下,主要记住 【a,a:hover】
/* 未访问的链接 */
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</title>
<style>
/*默认的颜色*/
a{
text-decoration: none;
color: black;
}
/*鼠标悬浮的状态*/
a:hover {
color: blue;
font-size: 20px;
}
/*鼠标按住未释放的状态*/
a:active{
color: green;
}
a:link {
color: gold;
}
a:visited{
color: darkturquoise;
}
</style>
</head>
<body>
<a href="#">
<img src="images/a.png" alt="">
</a>
<p>
<a href="#">悲惨的世界</a>
</p>
<p>
<a href="#">维克多·雨果</a>
</p>
<p>
¥99
</p>
</body>
</html>
结果:
3.5 阴影
/*text-shadow:阴影颜色,水平偏移,垂直偏移,阴影半径 */
#price{
text-shadow: green 10px 10px 2px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*默认的颜色*/
a{
text-decoration: none;
color: black;
}
/*text-shadow:阴影颜色,水平偏移,垂直偏移,阴影半径 */
#price{
text-shadow: green 10px 10px 2px;
}
</style>
</head>
<body>
<a href="#">
<img src="images/a.png" alt="">
</a>
<p>
<a href="#">悲惨的世界</a>
</p>
<p>
<a href="#">维克多·雨果</a>
</p>
<p id="price">
¥99
</p>
</body>
</html>
结果:
3.6 列表
//html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>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>
//css
/*list-style:
none 去掉原点
circle 空心圆
decimal 数字
square 正方形
*/
#nav {
background: darkgrey;
width:300px
}
.title{
font-size:18px ;
font-weight: bold;
text-align:center ;
line-height: 35px;
background: brown ;
color: white;
}
li{
height: 30px;
list-style: none ;
tab-index: 1em;
}
a{
text-decoration: none;
font-size: 14px;
color: black;
}
a:hover {
color: chocolate;
text-decoration: underline ;
}
结果:
3.7 背景
背景颜色
背景图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width: 780px;
height: 400px;
border: 1px solid red;
/*默认是全部平铺*/
background-image: url("images/a.jpg") ;
}
.div1{
background-repeat:repeat-x ;
}
.div2{
background-repeat:repeat-y ;
}
.div3{
background-repeat:no-repeat ;
}
</style>
</head>
<body>
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
</body>
</html>
结果:
3.8 渐变
https://www.grabient.com// 网站可以下载渐变色的css代码
background-color: #4158D0;
background-image: linear-gradient(81deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%);
4. 盒子模型
4.1 什么是盒子模型?
margin:外边距
padding:内边距
border:边框
4.2 边框
- 边框的粗细
- 边框的样式
- 边框的颜色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*body总有一个默认的外边框margin:0,常见操作*/
/*body{*/
/* margin: 0;*/
/* padding: 0;*/
/* text-decoration: none;*/
/*}*/
/*border:粗细,样式,颜色*/
#box{
width: 300px;
border: 1px solid black;
}
h2{
font-size: 16px;
background-color: gold;
line-height: 30px;
margin: 0;
}
form{
background: #e73131;
}
div:nth-of-type(1) input{
border: 3px solid blue;
}
div:nth-of-type(2) input{
border: 3px dashed blue;
}
div:nth-of-type(3) input{
border: 2px dashed green;
}
</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</title>
<style>
/*外边距的妙用,居中元素
顺时针旋转
margin:上右下左
padding:上右下左
*/
#box{
width: 300px;
border: 1px solid black;
margin: 0 auto;
}
h2{
font-size: 16px;
background-color: gold;
line-height: 30px;
margin: 0;
}
form{
background: #e73131;
}
input {
border: 1px solid black;
}
form div:nth-of-type(1) input {
padding: 10px;
}
</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+border+padding+内容宽度
4.4 圆角边框
展示一
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
/*border-radius:左上 右上 右下 左下
圆角=半径
*/
width: 100px;
height: 100px;
background-color: darkred;
border-radius:50px 50px 10px 0px;
/*border-radius:100px;*/
}
</style>
</head>
<body>
<div>
</div>
</body>
</html>
结果:
展示二
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width:200px;
margin:0 auto;
}
div img{
border-radius:100px;
}
</style>
</head>
<body>
<div>
<img src="images/a.jpg" alt="">
</div>
</body>
</html>
结果:
4.5 阴影
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
img{
border-radius:100px;
box-shadow: 10px 10px 100px yellow ;
}
</style>
</head>
<body>
<div style="width:200px; margin:0 auto;" >
<img src="images/a.jpg" alt="">
</div>
</body>
</html>
结果:
可以参考制作网页的开源网站:
- https://www.mycodes.net/
- http://www.cssmoban.com/
- https://www.layuiweb.com/
- https://element.eleme.cn/
- https://panjiachen.github.io/vue-element-admin-site/
- https://ice.work/
- http://www.cssmoban.com/
5. 浮动
5.1 标准文档流
块级元素:独占一行
h1~h6 p div 列表........
行内元素:不独占一行
span a img strong........
行内元素可以被包含在块级元素中,反之则不可以
5.2 display
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--block 块元素
inline 行内元素
inline-block 是块元素但是可以内联在一行
none
-->
<style>
div{
width:100px;
height: 100px;
border: 1px solid red;
display: inline;
}
span{
width:100px;
height: 100px;
border: 1px solid red;
display: inline-block;
}
</style>
</head>
<body>
<div>div块元素</div>
<span>span行内元素</span>
</body>
</html>
结果:
display也是一种实现行内元素排列的方式,但是我们很多情况都是使用float
5.3 float
5.3.1. 左右浮动
<!--html-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="father">
<div class="layer01"><img src="images/a.jpg" alt=""></div>
<div class="layer02"><img src="images/b.jpg" alt=""></div>
<div class="layer03"><img src="images/c.jpg" alt=""></div>
<div class="layer04">
科技一般指科学技术。 社会上习惯于把科学和技术连在一起,统称为科学技术,简称科技。
</div>
</div>
</body>
</html>
/*css*/
div{
margin:10px;
padding: 5px;
}
#father {
border: 1px solid red;
}
.layer01 {
border: 1px dashed green;
display: inline-block;
float: right;
}
.layer02 {
border: 1px dashed green;
display: inline-block;
float: right;
}
.layer03 {
border: 1px dashed green;
display: inline-block;
float: right;
}
.layer04 {
border: 1px dashed green;
display: inline-block;
float: right;
clear: both;
}
结果:
5.4 父级边框塌陷的问题
/*
clear:right;右侧不允许有浮动元素
clear:left;左侧不允许有浮动元素
clear:both;都不允许有浮动元素
clear:none;
*/
解决方案:
- 增加父级元素的高度
#father {
border: 1px solid red;
height: 800px;
}
- 增加一个空的div标签
<div class="clear" ></div>
.clear{
clear: both;
margin: 0;
padding: 0;
}
结果:
理解:
最后加的div不允许浮动,因为它是父级div最后的元素,所以把父级框架撑起,从而不会塌陷
- overflow
在父级元素中增加一个 overflow: hidden
- 父类添加一个伪类:after
#father:after{
content: "";
display: block;
clear: both;
}
小结:
1. 浮动元素后面增加空div
简单,但代码中赢尽量避免空div
2. 设置父元素的高度
简单,元素假设有了固定的高度,就会被限制
3. overflow
简单,下拉的一些场景避免使用
4. 父类添加一个伪类:after (推荐)
写法稍微复杂一些,但没有副作用,推荐使用
5.5 display和float对比
- display
方向不可以控制
- float
可以控制方向,但浮动起来的话会脱离标准文档流,所以要解决父级边框塌陷的问题
6.定位
6.1 相对定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
padding: 30px;
}
div{
margin: 10px;
padding: 5px;
font-size: 10px;
line-height: 25px;
border:1px solid darkred ;
}
#father{
border: 5px solid darkred ;
padding: 0;
}
#first{
background: lightsalmon;
border: 2px dashed gold ;
position: relative;
top:-30px
}
#second{
background: lightsalmon;
border: 2px dashed gold ;
position: relative;
right: 30px;
}
#third{
background: lightsalmon;
border: 2px dashed gold ;
position: relative;
bottom:-20px
}
</style>
</head>
<body>
<div id="father ">
<div id="first">第一个盒子</div>
<div id="second">第一个盒子</div>
<div id="third">第一个盒子</div>
</div>
</body>
</html>
结果:
相对定位:position: relative;
相对于原来的位置进行指定的偏移,相对定位的话,它仍然在标准文档流中,原来的位置会被保留
top left bottom right
练习题:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#father{
width: 300px;
height: 300px;
padding: 15px;
border: 1px solid darkred ;
}
a{
display: block;
height: 100px;
width: 100px;
background: pink;
text-align: center ;
text-decoration: none ;
line-height: 100px;
color: black;
}
a:hover {
background-color: dodgerblue;
}
#a1{
}
#a2{
position: relative;
left:200px;
top: -100px;
}
#a3{
position: relative;
left:100px;
top: -100px;
}
#a4{
position: relative;
bottom: 100px;
}
#a5{
position: relative;
bottom: 200px;
left: 200px;
}
</style>
</head>
<body>
<div id="father">
<a id="a1" href="#">链接1</a>
<a id="a2" href="#">链接2</a>
<a id="a3" href="#">链接3</a>
<a id="a4" href="#">链接4</a>
<a id="a5" href="#">链接5</a>
</div>
</body>
</html>
结果:
6.2 绝对定位
绝对定位是元素在移动位置的时候,是相对于它祖先元素来说的。
定位:基于xxx定位,上下左右
absolute:定位是相对于离元素最近的设置了绝对或相对定位的父元素决定的,如果没有父元素设置绝对或相对定位,则元素相对于根元素即html元素定位。
- 如果没有父元素设置绝对或相对定位,则元素相对于根元素即浏览器定位。
- 有父元素,定位则是相对于离元素最近的设置了绝对或相对定位的父元素决定的。
- 设置了absolute的元素脱了了文档流,元素在没有设置宽度的情况下,宽度由元素里面的内容决定。脱离后原来的位置相当于是空的,下面的元素会来占据位置。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
margin: 10px;
padding: 5px;
font-size: 10px;
line-height: 25px;
border:1px solid darkred ;
}
#father{
border: 5px solid darkred ;
padding: 0;
position: relative;
}
#first{
background: lightsalmon;
border: 2px dashed gold ;
}
#second{
background: lightsalmon;
border: 2px dashed gold ;
position: absolute;
right: 30px;
top: 30px;
}
#third{
background: lightsalmon;
border: 2px dashed gold ;
}
</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</title>
<style>
body{
height: 1000px;
}
div:nth-of-type(1){
/*绝对定位:相对于浏览器*/
width: 100px;
height:100px;
background: #e73131;
position: absolute;
right: 0;
bottom: 0;
}
div:nth-of-type(2){
width: 50px;
height:50px;
background: yellow;
position: fixed;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div>div1</div>
<div>div2</div>
</body>
</html>
结果:
6.4 z-index
z-index:默认是0,最高无限
opacity: 背景透明度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="content" >
<ul>
<li><img src="images/a.png" alt=""></li>
<li class="tipText">爱学习,爱生活</li>
<li class="tipBg"></li>
<li>2022年4月13日22:22:10</li>
<li>地点:太平洋</li>
</ul>
</div>
</body>
</html>
#content{
width:336px;
padding: 0px;
margin:0px ;
overflow: hidden;
font-size: 20px;
line-height: 25px;
border: 1px solid #3131b4;
}
ul,li{
padding: 0px;
margin:0px ;
list-style: none ;
}
#content ul{
position:relative;
}
.tipText,.tipBg{
width:336px;
height: 25px;
position:absolute;
top:165px;
}
.tipText{
color: rgb(255, 255, 255);
text-align:center ;
/*z-index: 0;*/
}
.tipBg{
background-color: black;
/*背景透明度*/
opacity: 0.5;
/*filter: alpha(opacity=50) ;*/
}
7.网页动画(特效效果)
参考的网站:https://www.html5tricks.com/tag/html5-canvas