绝对定位和相对定位

一、绝对定位

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		div {
			width: 200px;
			height: 200px;
		}

		.father {
			position: absolute;  /* 绝对定位,不占位置 */
			background-color: red;

		}

		.son {
			background-color: pink;
			width: 220px;
		}
	</style>
</head>
<body>
	<div class="father"></div>
	<div class="son"></div>
</body>
</html>

二、相对定位

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		div {
			width: 300px;
			height: 300px;
			/* border: 1px solid red; */
			/* background-color: red; */
		}

		.top {
			background-color: red;
			position: relative; /* 相对定位: 1、占用原来的位置; 2、以原位置的左上角为基准点进行偏移。 */
			left: 150px;
			top: 150px;
		}

		.bottom {
			background-color: purple;
		}

	</style>
</head>
<body>
	<div class="top"></div>
	<div class="bottom"></div>
</body>
</html>

三、固定定位

固定定位,以浏览器为基准

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		
		.father {
			width: 500px;
			height: 500px;
			background-color: purple;

		}

		.son {
			width: 200px;
			height: 200px;
			background-color: pink;
			position: fixed; /* 固定定位,以浏览器为基准 */
			margin-top: 100px;
			margin-left: 100px;
		}
	</style>
</head>
<body>
	<div class="father">
		<div class="son"></div>
	</div>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
	<p>123</p>
</body>
</html>

四、定位的相对性

4.1 当父元素有定位时,子元素以最邻近的有定位的父元素左上角为基准

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		.grandfather {
			width: 800px;
			height: 800px;
			background-color: blue;
			/* position: absolute; */
		}

		.father {
			width: 500px;
			height: 500px;
			background-color: pink;
			margin: 100px;
			position: absolute;
		}

		.son {
			width: 100px;
			height: 100px;
			background-color: purple;
			position: absolute;
			left: 50px;
			top: 50px; /* 当父元素有定位时,以最近的且有定位的父元素的左上角为基准 */
		}

	</style>	
</head>
<body>
	<div class="grandfather">
		<div class="father">
			<div class="son"></div>
		</div>
	</div>
</body>
</html>

4.2 当父元素没有定位时,子元素以当前屏幕或浏览器的左上角为基准

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		.father {
			width: 200px;
			height: 200px;
			background-color: pink;
		}

		.son {
			width: 100px;
			height: 100px;
			background-color: purple;
			position: absolute;
			left: 500px;
			top: 500px; /* 当父元素没有定位时,以当前浏览器屏幕为基准对齐 */
		}
		
	</style>
</head>
<body>
	<div class="father">
		<div class="son"></div>
	</div>
</body>
</html>

4.3 定位的盒子垂直居中

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		div {
			width: 200px;
			height: 200px;
			background-color: pink;

			/**
			* 此时水平居中对齐
			*/
			position: absolute; /* 利用绝对定位 */
			left: 50%; /* 向右偏移父容器的宽度的一半距离 */
			margin-left: -100px; /* 再向左偏移盒子宽度的一半 */
			/**
			* 设置垂直居中对齐
			*/
			top: 50%;
			margin-top: -100px;

		}
	</style>	
</head>
<body>
	<div></div>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值