06-CSS定位(position)
第1章 position属性
1-1 position-static
1-2 position-relative
<!DOCTYPE html>
<html>
<head>
<title>3</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
.blue{
width: 100px;
height: 100px;
background: blue;
position: relative;
top: 50px;
left: 50px;
}
.red{
width: 100px;
height: 100px;
background: red;
}
</style>
</head>
<body>
<div class="blue"></div>
<div class="red"></div>
</body>
</html>
1-3 position-absolute(实际工作用的最多,同时也相对比较难)
<!DOCTYPE html>
<html>
<head>
<title>3</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<style type="text/css">
.per{
width: 300px;
height: 300px;
background: red;
margin-left: 200px;
margin-top: 200px;
}
.son{
width: 100px;
height: 100px;
background: blue;
position: relative;
top: 100px;
left: 100px;
}
</style>
</head>
<body>
<div class="per">
<div class="son"></div>
</div>
</body>
</html>
1-4 position-fixed
<!DOCTYPE html>
<html>
<head>
<title>3</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.test{
width: 800px;
height: 100px;
background: red;
position: fixed;
margin-left: -400px;
top: 0;
left: 50%;
}
.out{
width: 100%;
height: 2000px;
background: #abcdef;
}
</style>
</head>
<body>
<div class="out">
<div class="test"></div>
</div>
</body>
</html>
1-5 position-sticky(了解即可)
第2章 实际应用
效果图:
<!DOCTYPE html>
<html>
<head>
<title>3</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.page{
width: 1400px;
height: 800px;
background: url(http://climg.mukewang.com/59c9f7ce0001839219034033.png) no-repeat center top;
}
.left{
width: 210px;
height: 250px;
background: url(http://climg.mukewang.com/5a3383d00001a3dd02240364.png) no-repeat center top;
position: fixed;
left: 0;
top: 50%;
margin-top: -105px;
}
.right{
width: 210px;
height: 250px;
background: url(http://climg.mukewang.com/5a3383c70001f1b702240364.png) no-repeat center top;
position: fixed;
right: 0;
top: 50%;
margin-top: -105px;
}
</style>
</head>
<body>
<div class="page">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>