定位元素position的使用

定位元素position的使用

定位元素position的作用:手动控制元素在包含块中的精确位置。
1、position:static(默认值)
2、position:relative 相对定位
相对于自身的定位,不脱离文档流(即元素发生定位时还会保留原始的位置),一般用于元素的微调。

<!DOCTYPE html><html lang="en"><head>    
<meta charset="UTF-8">    
<meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>Document</title>    
<style>      
*{          
padding: 0px;          
margin: 0px;      
}      
.box{          
background-color: aqua;          
width: 1200px;          
height: 200px;          
float: left;      
}      
.box1{          
width: 400px;          
height: 100px;          
background-color: blanchedalmond;          
float: left;      
}      
.box2{          
width: 400px;         
height: 100px;          
background-color: green;          
float: left;          
position: relative;          
left: 20px;      
}      
.box3{          
width: 400px;          
height: 100px;         
background-color: blue;          
float: left;      
}    
</style>
</head>
<body>  
<div class="box">      
<div class="box1"></div>      
<div class="box2"></div>      
<div class="box3"></div>

</div>
</body>
</html>

相对定位会改变相邻盒子的位置。3、position:absolute 绝对定位
相对于父级元素或祖先元素的定位,脱离文档流,相对定位一般都是配合绝对定位元素使用;

<!DOCTYPE html><html lang="en"><head>    
<meta charset="UTF-8">    
<meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>Document</title>    
<style>      
*{          
padding: 0px;          
margin: 0px;      
}      
.box{          
background-color: aqua;          
width: 1200px;          
height: 200px;          
position: relative;      
}      
.box1{         
 width: 400px;          
 height: 100px;          
 background-color: blanchedalmond;          
 float: left;      
 }      
 .box2{          
 width: 400px;          
 height: 100px;          
 background-color: green;          
 float: left;          
 position: absolute;          
 left: 30px;          
 bottom: 30px;      
 }      
 .box3{          
width: 400px;         
height: 100px;         
background-color: blue;         
float: left;      
   }      
.clearfix::after{          
clear: both;          
content: "";          
display: block;      
}    
</style>
</head>
<body>  
<div class="box clearfix">      
<div class="box1"></div>      
<div class="box2"></div>      
<div class="box3"></div>
  </div>
  </body>
  </html>

在这里插入图片描述
4、position:fixed(固定定位)
脱离文档流,当使用固定定位时元素自动成为块元素,可以改变宽度高度,一般用于固定导航条、边栏导航条等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值