CSS position定位

position定位通过顶部,底部,左侧和右侧属性进行定位

1.static(占文档流)
默认值,没有定位。

2.fixed(不占文档流)
相对于浏览器进行定位,固定在浏览器的某个位置,不会随着滚动条进行滚动

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.div_fixed{
				position: fixed;
				top:50px;
				left:50px;
				height:50px;
				width:100px;
				border:1px solid black
			}
			.div_data{
				height:100px;
				width:100px;
				border:1px solid red
			}
		</style>
	</head>
	<body>
		<div class='div_fixed'>fixed定位</div>
		<div class='div_data'>数据数据</div>
	</body>
</html>

在这里插入图片描述
3.relative(占文档流)
相对定位元素的定位,是相对其文档流中正常位置进行定位

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.div_relative{
				position: relative;
				top:50px;
				left:50px;
				height:50px;
				width:100px;
				border:1px solid black
			}
			.div_data{
				height:100px;
				width:100px;
				border:1px solid red
			}
		</style>
	</head>
	<body>
		<div class='div_relative'>relative定位</div>
		<div class='div_data'>数据数据</div>
	</body>
</html>

在这里插入图片描述
4.absolute(不占文档流)
绝对定位的元素的位置相对于最近的已定位父元素,如果元素没有已定位的父元素则相对于html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.div_absolute{
				position: absolute;
				top:50px;
				left:50px;
				height:50px;
				width:100px;
				border:1px solid black
			}
			.div_data{
				height:100px;
				width:100px;
				border:1px solid red
			}
		</style>
	</head>
	<body>
		<div class='div_absolute'>absolute定位</div>
		<div class='div_data'>数据数据</div>
	</body>
</html>

在这里插入图片描述
5.sticky
粘性定位基于用户的滚动位置来定位,在 position:relative 与 position:fixed 定位之间切换。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<style>
.div_sticky {
  position: sticky;
  top: 0;
  padding: 5px;
  background-color: #cae8ca;
  border: 2px solid #4CAF50;
}
</style>
</head>
<body>

<p>开始滚动啦。</p>
<p>...</p>
<p>...</p>
<p>...</p>
<div class="div_sticky">我是粘性定位!</div>
<div style="padding-bottom:2000px">
  <p>滚动...</p>
  <p>再滚动...</p>
  <p>滚动...</p>
  <p>再滚动...</p>
  <p>滚动...</p>
  <p>再滚动...</p>
</div>

</body>
</html>

在这里插入图片描述
在这里插入图片描述
6.重叠属性(不占文档流)
具有更高堆叠顺序的元素总是在较低的堆叠顺序元素的前面。通过z-index进行设置

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<style>
	body{
		margin: 0;
		padding: 0;
	}
.div_index {
	width:200px;
	height:200px;
	  background-color: #4CAF50;
	  border: 2px solid #4CAF50;
	  position:absolute;
		left:0px;
		top:0px;
		z-index:-1;
}
.div_bottom{
	width:100px;
	height:100px;
	  background-color: red;
	  border: 2px solid red;
}
</style>
</head>
<body>
<div class="div_index"></div>
<div class='div_bottom'></div>

</body>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值