定位position
定位分为以下几种:静态定位(static),是默认的定位;相对定位(relative),一般用在父级;绝对定位(absolute);固定定位(fixed),参考物是浏览器窗口。
相对定位relative
相对定位的参考物:定位前的位置,也就是它原来的位置。
相对定位的特点:不影响元素本身特性;元素不脱离文档流(就是使用定位移动后,原来的位置不会被占);相对于原位置进行偏移。
绝对定位absolute
绝对定位的参考物:最近的使用了定位的父级,如果没有找body。
绝对定位的特点:元素脱离文档流;行元素支持所有的css样式;块元素内容撑开高度;清除子级浮动。
固定定位fixed
固定定位的参考物:浏览器窗口。
固定定位的特点:脱离文档流;清除子级浮动。
定位的作用
1、可以使用偏移描述(left、right、top、bottom)
2、可以使用z-index提升层级。z-index大的元素会叠加在z-index较小的元素上。
练习:用定位实现QQ图标
<!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>qq</title>
<style>
*{
margin: 0;
padding: 0;
}
.qq{
width: 400px;
height: 400px;
background-color: pink;
position: relative;
}
.qq div{
position: absolute;
}
.body{
width: 200px;
height: 320px;
background-color: #111;
border-radius: 100px;
left: 100px;
top: 40px;
overflow: hidden;
z-index: 1;
}
.eye{
width: 30px;
height: 50px;
background-color: white;
border-radius: 50%;
top: 40px;
left: 60px;
}
.eye1 div,.eye2 div{
width: 16px;
height: 20px;
background-color: #111;
border-radius: 50%;
left: 10px;
top: 20px;
}
.eye2{
left: 110px;
}
.eye2 div{
left: 4px;
top: 24px;
border-top: 4px solid #111;
background-color: transparent;
}
.zui{
width: 120px;
height: 24px;
background-color: goldenrod;
border-radius: 50%;
left: 40px;
top: 98px;
}
.wb{
width: 220px;
height: 150px;
background-color: transparent;
border: 40px solid tomato;
border-radius: 50%;
top: -50px;
left: -50px;
z-index: 1;
}
.wb2{
width: 40px;
height: 60px;
background-color: tomato;
left: 30px;
top: 160px;
z-index: 1;
}
.dp{
width: 160px;
height: 160px;
background-color: #fff;
border-radius: 80px;
left: 20px;
top: 150px;
}
.arm{
width: 140px;
height: 40px;
background-color: #111;
border-radius: 50%;
left: 20px;
top: 218px;
}
.arm1{
transform: rotate(-60deg);
}
.arm2{
left: 240px;
transform: rotate(60deg);
}
.foot{
width: 100px;
height: 30px;
background-color: goldenrod;
left: 90px;
top: 330px;
border-radius: 50%;
}
.foot1{
transform: rotate(-10deg);
}
.foot2{
left: 210px;
transform: rotate(10deg);
}
</style>
</head>
<body>
<div class="qq">
<div class="body">
<div class="eye eye1">
<div></div>
</div>
<div class="eye eye2">
<div></div>
</div>
<div class="zui"></div>
<div class="wb"></div>
<div class="wb2"></div>
<div class="dp"></div>
</div>
<div class="arm arm1"></div>
<div class="arm arm2"></div>
<div class="foot foot1"></div>
<div class="foot foot2"></div>
</div>
</body>
</html>