定位的小细节

1.static 静态(默认)
2.relative 相对定位 
相对于自己的父元素 占据自己原来位置

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			.div1{
				width: 300px;
				height: 300px;
				background-color: #0000FF;
				position: relative; 
				/* position: absolute; */
				top: 50px;
				/* margin-top: 200px; */
			}
			.div2{
				width: 200px;
				height: 200px;
				background-color: #008000;
				position: relative;
				top: 50px;
				/* 相对定位会随着父元素的一定移动 */
			}
			
		</style>
	</head>
	<body>
		<div class="div1">
			<div class="div2">
				11
			</div>
			
		</div>
	</body>
</html>


absolute 绝对定位
脱离文档流
相对于最近的定位如果没有就相对于body 
不占据自己原来的位置

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			.div1{
				width: 300px;
				height: 300px;
				background-color: #0000FF;
				position: relative; 
				/* position: absolute; */
				top: 50px;
				/* margin-top: 200px; */
			}
			.div2{
				width: 200px;
				height: 200px;
				background-color: #008000;
				position: relative;
				top: 50px;
				/* 相对定位会随着父元素的一定移动 */
			}
			.div3{
				width: 200px;
				height: 200px;
				background-color: #00FFFF;
				position: absolute;
				top: 100px;
				/* 绝对定位会脱离文档流 如果父元素或者祖元素出现定位 就会以上面的定位做为标准*/
			}
		</style>
	</head>
	<body>
		<div class="div1">
			<div class="div2">
				11
			</div>
			<div class="div3">
				22
			</div>
			<div class="div4">
				33
			</div>
		</div>
	</body>
</html>

fixed 固定定位 脱离文档流

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			.div1{
				width: 300px;
				height: 300px;
				background-color: #0000FF;
				position: relative; 
				top: 50px;
				height: 2000px;
			}	
			.div3{
				width: 200px;
				height: 200px;
				background-color: #00FFFF;
				position: fixed;
				top: 100px;
				/* fixed 固定定位 脱离文档流*/
			}
		</style>
	</head>
	<body>
		<div class="div1">
			
			<div class="div3">
				22
			</div>
		</div>
	</body>
</html>

sticky 滚动位置定位

<!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>Document</title>
  <style>
    .div {
      height: 2000px;
      width: 100%;
    }
	.box1{
		width: 100%;
		height: 50px;
		background: red;
	}
    .box2 {
      width: 200px;
      height: 300px;
      background-color: black;
    }
    .style {
      position: sticky;
      top: 50px; 
	  /* 始终距离网页上面50px */
    }
  </style>
</head>
<body>
  <div class="div">
		<div class="box1"></div>
		<div class="box2 style"></div>
  </div>
</body>
 
</html>

被设置的盒子始终距离上面的距离不变
z-index 重叠元素
 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值