position的常用值
position:absolute
会相对已经定位的父元素(祖先元素)进行定位,会脱离正常的文档流,不会占据任何位置。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.d1{
width: 200px;
height: 200px;
background-color: skyblue;
position: absolute;
left: 200px;
top: 200px;
}
.d2{
width: 200px;
height: 200px;
background-color: palevioletred;
}
.parent{
width: 600px;
height: 600px;
background-color: darkslategrey;
margin: 0 auto;
position: absolute;
left: 500px;
height: 500px;
}
</style>
</head>
<body>
<div class="parent">
<div class="d1"></div>
<div class="d2"></div>
</div>
</body>
</html>
position:relative
相对于自己原本的位置进行定位,不脱离正常的文档流,改变以后依然占据着原本的位置。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.d1{
width: 200px;
height: 200px;
background-color: skyblue;
position: relative;
left: 100px;
top: 100px;
}
.d2{
width: 200px;
height: 200px;
background-color: palevioletred;
display: inline-block;
}
.d3{
width: 200px;
height: 200px;
background-color: greenyellow;
display: inline-block;
}
.parent{
width: 600px;
height: 600px;
background-color: darkslategrey;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="parent">
<div class="d1"></div>
<div class="d2"></div>
<div class="d3"></div>
</div>
</body>
</html>
position:fixed
fixed是以窗口来进行元素定位。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
a{
width: 150px;
height: 100px;
background-color: paleturquoise;
display: inline-block;
position: fixed;
left: calc(50% - 75px);
top: calc(50% - 50px);
}
</style>
</head>
<body>
<a href="">xiao可爱在线发牌</a>
.d1{内容$}*100
</body>
</html>