文章目录
学习目标
- 知道为什么要用定位
- 知道定位的四种分类
- 知道四种定位的特点
- 知道为什么用子绝父相布局
- 写出轮播图布局
- 知道显示隐藏的两种方式和区别
定位
- 使用场景
- 某个元素可以自由的在一个盒子内移动位置,并且需要压住其他盒子
- 滚动窗口,盒子不跟随滚动
- 定义:将盒子定在某一个位置,所以定位也是用来布局,用来摆放盒子的
- 组成:定位模式 + 边偏移
- 定位模式:指定一个元素在文档中的定位方式
- 边偏移:指定该元素的最终位置
边偏移
- 定位的盒子需要移动到的位置
- 属性以及描述
边偏移属性 | 示例 | 描述 |
---|---|---|
top | top: 80px | 顶端偏移量,定义元素相对于其父元素上边线的距离 |
bottom | bottom: 80px | 底部偏移量,定义元素相对于其父元素下边线的距离 |
left | left: 80px | 左侧偏移量,定义元素相对于其父元素左边线的距离 |
right | right: 80px | 右端偏移量,定义元素相对于其父元素右边线的距离 |
定位模式
- position属性 来决定元素的定位方式
- 分类:
值 | 语义 |
---|---|
static | 静态定位 |
relative | 相对定位 |
absolute | 绝对定位 |
fixed | 固定定位 |
静态定位 static
- 元素的默认定位方式,就是没得定位的意思
- 特点
- 静态定位按照标准流的特性摆放位置,没有偏移量
- 很少使用
- 语法
选择器 {
position: static;
}
相对定位 relative
- 元素在移动位置的时候,是相对于其原本的位置而言的
- 特点
- 移动位置的时候是参照其原位置
- 使用后,盒子虽然动了,但是它原本所占的位置还在,其他盒子不能直接占有,即没有脱标(一般给绝对定位当爹)
- 语法
选择器 {
position:relative;
}
- 案例
<!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>相对定位</title>
<style>
.box1 {
position: relative;
top: 100px;
left: 100px;
width: 100px;
height: 100px;
background-color: skyblue;
}
.box2 {
width: 200px;
height: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
- 效果图
绝对定位
-
元素在移动位置的时候,相对于其祖先元素来移动的
-
特点
- 如果没有祖先元素or祖先元素没有定位,则以浏览器界面为准定位
- 如果祖先元素有定位(相对、绝对和固定三种),则以最近一级有定位祖先元素为参考点移动位置(给相对定位当儿)
- 使用绝对定位后不再占有原位置(脱标了)
-
案例1:祖先元素没有定位
<!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>绝对定位:祖先元素没有定位</title>
<style>
.father {
width: 500px;
height: 500px;
background-color: skyblue;
}
.son {
position: absolute;
bottom: 100px;
right: 100px;
width: 200px;
height: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</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>相对定位:祖先元素有定位</title>
<style>
.father {
position: relative;
width: 500px;
height: 500px;
background-color: skyblue;
}
.son {
position: absolute;
bottom: 100px;
right: 100px;
width: 200px;
height: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</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>相对定位:父亲不定位,爷爷定位了</title>
<style>
.grandfather {
position: relative;
width: 1000px;
height: 1000px;
background-color: red;
}
.father {
width: 500px;
height: 500px;
background-color: skyblue;
}
.son {
position: absolute;
bottom: 100px;
right: 100px;
width: 200px;
height: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="grandfather">
<div class="father">
<div class="son"></div>
</div>
</div>
</body>
</html>
- 效果图
子绝父相
-
见名知意:子级是绝对定位的话,父级要用相对定位
-
原因
- 子级使用绝对定位,是脱标的,不占位置,所以可以放在父盒子里面的任何地方,且不影响其他兄弟盒子
- 父盒子为了限制子盒子只能在其体内显示,所以需要加定位
- 因为布局的时候,父盒子是需要占有位置的,所以三种定位中就相对定位最合适
-
总结:父级需要占位子,所以使用相对定位,子盒子不需要占位置但是得在父盒子里面,所以使用绝对定位
-
案例
<!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>子绝父相</title>
<style>
.zzd {
position: relative;
width: 500px;
height: 250px;
margin: 0 auto;
border: 1px solid black;
}
.hot {
position: absolute;
right: -6px;
top: -5px;
}
</style>
</head>
<body>
<div class="zzd">
<img src="../../../PYOH/images/爪子刀.png" alt="">
<div class="hot"><img src="../../../Review/CSS/CSS04/学成在线/images/hot.png" alt=""></div>
</div>
</body>
</html>
- 效果图
固定定位 fixed
- 将盒子固定在浏览器可视区域中
- 特点
- 将浏览器的可视窗口为参照点移动盒子
- 和父元素没有关系
- 不随滚动条滚动
- 脱标
- 将浏览器的可视窗口为参照点移动盒子
- 案例
<!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>固定定位</title>
<style>
body {
height: 2000px;
}
.zzd {
position: fixed;
top: 100px;
right: 40px;
}
</style>
</head>
<body>
<div class="zzd"><img src="../../../PYOH/images/爪子刀.png" alt=""></div>
</body>
</html>
- 效果图
- 常用情况:固定定位到版心右侧
- 固定步骤
- 让固定定位的盒子 left: 50% 移动到浏览器可视区(or版心)的一半位置
- 再让固定定位的盒子 margin-left: 浏览器(or版心)宽度距离的一半,就可以使固定定位的盒子贴着版心右侧对齐了
- 案例
<!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>固定定位到版心右侧</title>
<style>
.w {
width: 800px;
height: 3000px;
background-color: skyblue;
margin: 0 auto;
}
.fixed {
position: fixed;
left: 50%;
margin-left: 400px;
width: 50px;
height: 100px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="fixed"></div>
<div class="w">版心盒子</div>
</body>
</html>
- 效果图
粘性定位 sticky
- 类似于相对定位和固定定位的混合体
- 特点
- 以浏览器的可视窗口为参照点移动元素(固定定位特点)
- 粘性定位不脱标
- 必须添加top、left、right、bottom中一个属性才有效
- 兼容性较差
- 案例
<!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>粘性定位</title>
<style>
body {
height: 3000px;
}
.nav {
position: sticky;
top: 0;
width: 800px;
height: 50px;
background-color: skyblue;
margin: 100px auto;
}
</style>
</head>
<body>
<div class="nav">导航栏</div>
</body>
</html>
- 效果图
总结
定位模式 | 是否脱标 | 移动位置 | 是否常用 |
---|---|---|---|
static 静态定位 | 否 | 不能用边偏移 | 很少 |
relative 相对定位 | 是(占有位置) | 相对于自身位置移动 | 常用 |
absolute 绝对定位 | 是(不占有位置) | 带有定位的父级or浏览器 | 常用 |
fixed 固定定位 | 是(不占有位置) | 浏览器可视区 | 常用 |
sticky 粘性定位 | 否(占有位置) | 浏览器可视区 | 常用 |
- 要记住是否脱标和以谁为参照物
- 子绝父相很重要
定位叠放次序 z-index
- 使用背景:在使用定位布局的时候,可能会出现盒子重叠的情况,这种时候就可以使用z-index 来控制盒子的前后顺序(z轴)
- 语法
选择器 {
z-index:1;
}
-
注意事项
- 数值可以是正整数、负数或0,默认是auto,数值越大,盒子越靠上
- 如果属性值相同,则按照书写顺序,后来居上
- 数字后面是没有单位的
- 只有定位了的盒子才有z-index属性
-
案例
<!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>定位叠放次序:z-index</title>
<style>
.box {
position: absolute;
top: 0;
left: 0;
width: 200px;
height: 200px;
}
.box1 {
background-color: skyblue;
z-index: 1;
}
.box2 {
background-color: yellow;
left: 50px;
top: 50px;
z-index: 2;
}
.box3 {
background-color: green;
left: 100px;
top: 100px;
}
</style>
</head>
<body>
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
</body>
</html>
- 效果图
定位拓展
绝对定位的盒子居中
-
使用背景:加了绝对定位的盒子不能通过margin:0 auto 进行水平居中
-
解决方法及其步骤
- left:50%;:让盒子左侧移动到父级元素的水平中心位置
- margin-left:-自身宽度一半;:让盒子向左移动自身宽度的一半
-
案例
<!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>绝对定位的盒子居中</title>
<style>
.box {
position: absolute;
left: 50%;
/* 盒子宽度的一半 */
margin-left: -100px;
top: 50%;
/* 盒子高度的一半 */
margin-top: -50px;
width: 200px;
height: 100px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
- 效果图
定位的特殊特性
- 绝对定位和固定定位也有某些浮动的特性
- 行内元素添加绝对or固定定位,可以直接设置高度和宽度
- 块级元素添加绝对or固定定位,如果没有给宽度或者高度,默认大小就是内容的大小
- 案例
<!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>定位特殊特性</title>
<style>
span {
position: absolute;
margin-top: 300px;
width: 200px;
height: 200px;
background-color: skyblue;
}
div {
position: absolute;
background-color: yellow;
}
</style>
</head>
<body>
<span>123</span>
<div>abc</div>
</body>
</html>
- 效果图
脱标的盒子没有外边距塌陷
- 浮动元素、绝对定位(固定定位)元素都不会触发外边距合并的问题
- 因为脱标了
- 案例
<!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>脱标的盒子没有外边距塌陷</title>
<style>
.box {
width: 200px;
height: 200px;
}
.box1 {
position: absolute;
margin-bottom: 100px;
background-color: skyblue;
}
.box2 {
position: absolute;
margin-top: 50px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="box box1"></div>
<div class="box box2"></div>
</body>
</html>
- 效果图
绝对(固定)定位会完全压住盒子
- 浮动元素会压住其下面标准流的盒子,但是不会压住下面标准流盒子内的文字or图片
- 绝对(固定)定位会压住其下面标准流所有的内容
- 案例
<!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>绝对(固定)定位会完全压住盒子</title>
<style>
.box {
float: left;
/* position: absolute; */
width: 200px;
height: 200px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="box"></div>
<p>说好带你流浪</p>
<p>而我却半路返航</p>
<p>坠落自责的海洋</p>
<p>发现离不开你</p>
<p>我开始决定回去</p>
<p>你已不在原地</p>
</body>
</html>
- 效果图
网页布局总结
- 盒子模型:大部分HTML标签是一个盒子
- 浮动、定位:将盒子排列成网页
- 标准流:可以将盒子上下or左右排列,垂直的块级盒子显示就使用标准流布局
- 浮动:可以将多个块级元素一行显示or左右对齐盒子,多个块级盒子水平显示就使用浮动布局
- 定位:定位最大的特点是有层叠的概念,即可以让多个盒子前后层叠显示,某个元素需要自动在另外一个盒子内移动就使用定位
元素的隐藏与显示
- display:显示隐藏元素,但不保留位置
- visibility:显示隐藏元素,但保留原位置
- overflow:溢出显示隐藏,但是仅对溢出部分进行处理
display 属性
- display:none;:隐藏对象
- display:block;:转换为块级元素and显示元素
- 使用display隐藏后,不在占有原位置
- 案例
<!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>display属性</title>
<style>
.box1,
.box2 {
width: 200px;
height: 200px;
}
.box1 {
/* display: none; */
display: block;
background-color: skyblue;
}
.box2 {
background-color: yellow;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
- 效果图
visibility属性
- visibility属性用于指定一个元素应可见or隐藏
- visibility:visible;:元素可见
- visibility:hidden;:元素隐藏
- visibility 隐藏元素后,将会继续占有原位置
- 案例
<!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>visibility属性</title>
<style>
.box {
width: 200px;
height: 200px;
}
.box1 {
visibility: hidden;
/* 隐藏元素后不想要原来的位置,添加display:none属性 */
/* display: none; */
background-color: skyblue;
}
.box2 {
background-color: yellow;
}
</style>
</head>
<body>
<div class="box box1"></div>
<div class="box box2"></div>
</body>
</html>
- 效果图
overflow属性
- overflow属性指定了内容溢出一个盒子的时候,会发生什么
- 属性及其相关描述
属性值 | 描述 |
---|---|
visible | 不剪切内容也不添加滚动条 |
hidden | 不显示超过盒子的内容,超出部分直接隐藏 |
scroll | 不管内容是否超出,总是显示滚动条 |
auto | 超出部分自动显示滚动条,不超出则不显示滚动条 |
- 案例
<!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>overflow属性</title>
<style>
.word1 {
float: left;
overflow: visible;
width: 200px;
height: 200px;
border: 2px solid skyblue;
margin: 100px 50px;
}
.word2 {
float: left;
overflow: hidden;
width: 200px;
height: 200px;
border: 2px solid skyblue;
margin: 100px 50px;
}
.word3 {
float: left;
overflow: scroll;
width: 200px;
height: 200px;
border: 2px solid skyblue;
margin: 100px 50px;
}
.word4 {
float: left;
overflow: auto;
width: 200px;
height: 200px;
border: 2px solid skyblue;
margin: 100px 50px;
}
.word5 {
float: left;
overflow: auto;
width: 200px;
height: 200px;
border: 2px solid skyblue;
margin: 100px 50px;
}
</style>
</head>
<body>
<div class="word1">
<p>
说好带你流浪
而我却半路返航
坠落自责的海洋
发现离不开你
我开始决定回去
你已不在原地
我可以接受你的所有
所有小脾气
我可以带你去吃很多
很多好东西
我可以偶尔给你带来
带来小甜蜜
就像前几年那样
每天都会给你制造很多惊喜
你的心 伤我能治愈
我的快乐也只有你能给予
我们就别再分离
可不可以 和你在一起
我们之间有太多回忆
</p>
</div>
<div class="word2">
<p>
说好带你流浪
而我却半路返航
坠落自责的海洋
发现离不开你
我开始决定回去
你已不在原地
我可以接受你的所有
所有小脾气
我可以带你去吃很多
很多好东西
我可以偶尔给你带来
带来小甜蜜
就像前几年那样
每天都会给你制造很多惊喜
你的心 伤我能治愈
我的快乐也只有你能给予
我们就别再分离
可不可以 和你在一起
我们之间有太多回忆
</p>
</div>
<div class="word3">
<p>
说好带你流浪
而我却半路返航
坠落自责的海洋
发现离不开你
我开始决定回去
你已不在原地
我可以接受你的所有
所有小脾气
我可以带你去吃很多
很多好东西
我可以偶尔给你带来
带来小甜蜜
就像前几年那样
每天都会给你制造很多惊喜
你的心 伤我能治愈
我的快乐也只有你能给予
我们就别再分离
可不可以 和你在一起
我们之间有太多回忆
</p>
</div>
<div class="word4">
<p>
说好带你流浪
而我却半路返航
坠落自责的海洋
发现离不开你
</p>
</div>
<div class="word5">
<p>
说好带你流浪
而我却半路返航
坠落自责的海洋
发现离不开你
我开始决定回去
你已不在原地
我可以接受你的所有
所有小脾气
我可以带你去吃很多
很多好东西
我可以偶尔给你带来
带来小甜蜜
就像前几年那样
每天都会给你制造很多惊喜
你的心 伤我能治愈
我的快乐也只有你能给予
我们就别再分离
可不可以 和你在一起
我们之间有太多回忆
</p>
</div>
</body>
</html>
- 效果图