定位
css布局:盒模型、浮动、定位
辅助的功能属性:文本、a、背景等
定位:相对定位、绝对定位、固定定位
脱标:浮动、绝对定位、固定定位
- 相对定位
定位:元素位置相对于某一参照物进行的位置偏移
相对定位:元素相对于自身的相对偏移
position:定位
属性值:relative 相对的
偏移的数据量由其他属性来表示
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>相对定位</title>
</head>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
div{
width: 100px;
height: 100px;
background: pink;
margin-bottom: 10px;
}
.box{
background: skyblue;
position: relative;
left: 100px; //向右移动了100px 一个盒子的宽度
top: 110px; //像下移动了110px 一个盒子的高度加margin值
}
</style>
<body>
<div>1</div>
<div class="box">2</div>
<div>3</div>
<div>4</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>相对定位</title>
</head>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
div{
width: 100px;
height: 100px;
background: pink;
margin-bottom: 10px;
}
.box{
background: skyblue;
position: relative;
/*left: 100px;
top: 110px;*/
right: 20px;
bottom: 20px;
}
</style>
<body>
<div>1</div>
<div class="box">2</div>
<div>3</div>
<div>4</div>
</body>
</html>
定位改变之后的图片不会被下面的图片顶掉
- 绝对定位
绝对定位的定位参考元素,绝对定位从左上角算起
属性值:absolute
也有四个方向的偏移值:left right top bottom
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>绝对定位</title>
</head>
<style type="text/css">
*{
margin:0;
padding:0;
}
div{
width: 100px;
height: 100px;
background: pink;
margin-bottom: 10px;
}
.box{
background: skyblue;
position: absolute;
top: 100px;
left: 100px;
}
</style>
<body>
<div>1</div>
<div class="box">2</div>
<div>3</div>
<div>4</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>绝对定位</title>
</head>
<style type="text/css">
*{
margin:0;
padding:0;
}
div{
width: 100px;
height: 100px;
background: pink;
margin-bottom: 10px;
}
.box{
background: skyblue;
position: absolute;
top: 100px;
/*left: 100px;*/
right: 100px;
}
</style>
<body>
<div>1</div>
<div class="box">2</div>
<div>3</div>
<div>4</div>
</body>
</html>
特点:与相对定位比较,元素绝对定位之后,脱离标准流,标准流的位置让给了后面的元素
- 用绝对定位制作压盖效果
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>压盖效果</title>
</head>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.box{
width: 400px;
height: 400px;
border: 1px solid #000;
padding: 20px;
margin: 100px auto;
position: relative;
}
.box .demo1{
width: 400px;
height: 400px;
background: skyblue;
}
.box .demo2{
width: 100px;
height: 30px;
background:gold;
position: absolute;
left: 160px;
bottom: 40px;
}
</style>
<body>
<div class="box">
<div class="demo1">
</div>
<div class="demo2">
</div>
</div>
</body>
</html>
- 盒子居中
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>盒子居中</title>
</head>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
.box{
width: 400px;
height: 400px;
border: 1px solid #000;
padding: 20px;
margin: 100px auto;
position: relative;
}
.box .demo1{
width: 400px;
height: 400px;
background: skyblue;
}
.box .demo2{
width: 100px;
height: 100px;
background:gold;
position: absolute;
left: 50%;
bottom: 50%;
margin-left: -50px;
margin-bottom:-50px
}
</style>
<body>
<div class="box">
<div class="demo1">
</div>
<div class="demo2">
</div>
</div>
</body>
</html>
- 固定定位
参考浏览器的窗口进行定位
属性值:fixed
始终保持针对浏览器某一个顶点位置相对不变
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>固定定位</title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
.box{
position: fixed; //针对浏览器固定定位
/*bottom: 300px;*/
left: 80px;
width: 80px;
height: 80px;
background: #ccc;
line-height: 80px;
font-size: 28px;
color: #333;
text-decoration: none;
text-align: center;
margin-top: 800px;
}
</style>
</head>
<body>
<div class="box">
<a href="#">
Top
</a>
</div>
<p><img src="images/A1 (1).jpeg" alt="" /></p>
<p><img src="images/A1 (2).jpeg" alt="" /></p>
<p><img src="images/A1 (3).jpeg" alt="" /></p>
<p><img src="images/A1 (4).jpeg" alt="" /></p>
<p><img src="images/A1 (5).jpeg" alt="" /></p>
<p><img src="images/A1 (6).jpeg" alt="" /></p>
<p><img src="images/A1 (7).jpeg" alt="" /></p>
<p><img src="images/A1 (8).jpeg" alt="" /></p>
</body>
</html>
- 压盖顺序 z-index
默认压盖顺序:
①有定位的元素的压盖没有定位的元素
②有定位的元素,不区分定位类型,压盖顺序按书写顺序来写
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>默认压盖顺序</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.box1{
width: 300px;
height: 300px;
background: pink;
}
.box2{
width: 200px;
height: 200px;
background: skyblue;
position: relative;
left: 0;
bottom: 300px;
}
.box3{
width: 150px;
height: 150px;
position: absolute;
top: 0;
left: 0;
background: yellowgreen;
}
.box4{
width: 100px;
height: 100px;
position: fixed;
top: 0;
left: 0;
background: gold;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</body>
</html>
- 自定义压盖顺序
属性:z-index,压盖顺序
属性值:数字,数字大的压盖数字小的 - 只能给定位的元素使用,默认的z-index值是1
- 如果属性值相同,看书写顺序
- 父子盒模型里,如果父子都有定位,都有自定义z-index值,看父盒子的z-index数值
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>自定义压盖顺序</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.box1{
width: 300px;
height: 300px;
background: pink;
}
.box2{
width: 200px;
height: 200px;
background: skyblue;
position: relative;
left: 0;
bottom: 300px;
z-index: 2;
}
.box3{
width: 150px;
height: 150px;
position: absolute;
top: 0;
left: 0;
background: yellowgreen;
z-index: 3;
}
.box4{
width: 100px;
height: 100px;
position: fixed;
top: 0;
left: 0;
background: gold;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</body>
</html>