相对定位
使用
position:relative;
来设定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box1{
width:200px;
height:200px;
background-color:red;
/*如果当前盒子只设置相对定位,那么与标准流下的盒子没有什么区别*/
position:relative;
/*设置相对定位后,我们就可以使用四个方向的属性:left right top bottom
相对定位,是相对于原来的本身定位。top:20px;盒子相对于原来的位置向下移动20px,也就是说与原来盒子的顶部距离为20px;相对定位
仅仅微调我们的元素。
*/
left:20px;
top:20px;
}
</style>
</head>
<body>
<div class="box1">
</div>
</body>
</html>
样式:
特性
不脱标
不脱离标准文档流。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width:200px;
height:200px;
}
.box1{
background-color:red;
}
.box2{
background-color:green;
position:relative;
left:100px;
top:50px;
}
.box3{
background-color:blue;
}
</style>
</head>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
<div class="box3">
</div>
</body>
</html>
样式:
形影分离
盒子本身在老家,影子在到处飘。
老家留坑
盒子已经移到其他地方,原来的地方的空间还留着。
用途
相对定位在页面中没有什么大的作用,影响我们页面的布局,我们不要使用相对定位来做压盖效果。
有两个作用:
- 微调元素位置信息
- 做绝对定位的参考(父相子绝)
绝对定位
使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width:200px;
height:200px;
}
.box1{
background-color:red;
/*绝对定位的盒子会脱离标准流。做遮盖效果,提升了层级*/
position:absolute;
}
.box2{
background-color:green;
}
.box3{
background-color:blue;
}
span{
/*设置绝对定位之后,不区分行内、块级元素,都可设置宽高*/
width:100px;
height:100px;
background-color:pink;
position:absolute;
}
</style>
</head>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
<div class="box3">
</div>
<span>内容</span>
</body>
</html>
样式:
绝对定位参考点:
当使用top来描述的时候,是以页面的左上角(跟浏览器的左上角区分)为参考点调整位置。
当使用bottom属性描述的时候,是以首屏右下角为参考点。
特性
绝对定位的盒子会脱离标准流。
做遮盖效果。
设置绝对定位之后,不区分行内元素和块级元素,都能设置宽高。
绝对定位无视父辈的padding
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>父相子绝</title>
<style>
*{
padding:0;
margin:0;
}
.box{
width:300px;
height:300px;
border: 5px solid red;
margin: 50px 100px;
position:relative;
padding:50px;
}
.box p{
width:100px;
height:100px;
background-color:pink;
position:absolute;
left:0;
top:0;
}
</style>
</head>
<body>
<div class="box">
<p></p>
</div>
</body>
</html>
样式:
父相子绝
父辈元素设置的相对定位,子元素设置绝对定位。会以设置相对定位的父辈元素的左上角为参考点。这个父辈元素不一定是父亲,也可以是爷爷。如果爷爷和父亲同时设置了相对定位,以父亲为参考点,也就是说以最近的设置了相对定位的父辈元素为参考点。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>父相子绝</title>
<style>
*{
padding:0;
margin:0;
}
.box{
width:300px;
height:300px;
border: 5px solid red;
margin: 50px 100px;
position:relative;
padding:50px;
}
.box p{
width:100px;
height:100px;
background-color:pink;
position:absolute;
left:20px;
top:100px;
}
.box2{
width:200px;
height:200px;
background-color:green;
}
</style>
</head>
<body>
<div class="box">
<div class="box2">
<p></p>
</div>
</div>
</body>
</html>
样式:
父绝子绝
父辈元素设置了绝对定位,以父辈元素为参考点。无实战意义。不会使用父绝子绝,因为绝对定位脱离标准流,影响页面布局。相反父相子绝在页面布局中使用很多,因为父元素设置相对定位不脱离标准文档流,子元素设置绝对定位,仅仅是在当前父辈元素内调整位置信息。
父固子绝
父辈元素设置了固定定位,以父辈元素为参考点
绝对定位盒子居中
设置绝对定位之后,margin:0 auto;
不起任何作用。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>父相子绝</title>
<style>
*{
padding:0;
margin:0;
}
.box{
width:100%;
height:69px;
background:#000;
}
.box2{
width:960px;
height:69px;
position:absolute;
background-color:pink;
left:50%;
margin-left:-480px;
/*当作公式记下来:设置子元素绝对定位,left:50%,margin-left等于元素宽度的一半,实现绝对定位盒子剧中*/
}
</style>
</head>
<body>
<div class="box">
<div class="box2"></div>
</div>
</body>
</html>
样式:
固定定位
固定定位:固定当前的元素不会随着页面滚动而滚动。
参考点:设置固定定位,用top描述是以浏览器的左上角为参考点。用bottom描述是以浏览器的左下角为参考点。
作用:可以做返回顶部栏,固定导航栏,小广告。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>固定定位</title>
<style>
*{
margin:0;
padding:0;
}
div{
width:100%;
height:2000px;
background-color:pink;
background-image:url(./banner.png);
background-repeat:no-repeat;
background-position: center top;
}
p{
width:100px;
height:100px;
background-color:green;
position:fixed;
left:20px;
top:50%;
}
</style>
</head>
<body>
<div>
<p></p>
</div>
</body>
</html>
特性
- 脱标
- 提升层级
- 固定不变,页面滚动,其位置相对浏览器不改变位置。
固定导航栏
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>固定导航栏</title>
<style>
*{
margin:0;
padding:0;
}
ul{
list-style:none;
}
a{
text-decoration:none;
}
.wrap{
width:100%;
height:49px;
background-color:#000;
position:fixed;
top:0;
left:0;
/*设置固定定位之后一定要设置top和left属性*/
}
body{
/*给body设置导航栏的高度,来显示下方图片的整个内容*/
padding-top:49px;
}
.wrap .nav{
width:960px;
height:49px;
margin:0 auto;
}
.wrap .nav ul li{
float:left;
width:160px;
height:49px;
background-color:green;
}
.wrap .nav ul li a{
line-height:49px;
font-size:20px;
display:block;
width:160px;
height:49px;
text-align:center;
}
.wrap .nav ul li a:hover{
background-color:red;
font-size:22px;
}
</style>
</head>
<body>
<div class="wrap">
<div class="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>
</ul>
</div>
</div>
<div class="container">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
<img src="./dingdang.jpg" alt="">
</div>
</body>
</html>
样式:
z-index属性
- z-index值表示谁压着谁
- 只有定位了的元素,才能有z-index,也就是睡哦,不管相对定位,绝对定位,固定定位都可以使用z-index,而浮动元素不能使用
- z-index值没有单位,就是一个正整数,默认的z-index值为0
- 如果大家都没有z-index值,或者z-index值一样,那么谁卸载HTML后面,谁在上面压着谁,定位了的元素永远压着没有定位的元素
- 从父现象:父亲怂了,儿子再牛逼也没有用