css 绝对定位

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>相对定位</title>
		<style>
				
			.father{
				
				height: 500px;
				background-color: bisque;
			}
			.son1{
			
				height: 100px;
				background-color: #00ff7f;
				
				
			}
			.son2{
				
				height: 100px;
				background-color: #fff701;
				
				
			}
			.son3{
				
				height: 100px;
				background-color: #0026ff;
				
				
			}
			.son4{
				
				height: 100px;
				background-color: #08ffeb;
			}
		</style>
	</head>
	<body>
		<div class="father">
			<div class="son1">son1</div>
			<div class="son2">son2</div>
			<div class="son3">son3</div>
			<div class="son4">son4</div>
		</div>
	</body>
</html>

在这里插入图片描述子绝父相:给父元素添上相对定位,子元素添上绝对定位

<style>
				
			.father{
				height: 500px;
				background-color: bisque;
				/* 父元素相对定位 */
				position: relative;
			}
			.son1{
				height: 100px;
				background-color: #00ff7f;
				/* 绝对定位 */
				/* position: absolute; */
			}
			.son2{
				height: 100px;
				background-color: #fff701;
				/* 绝对定位 */
				/* position: absolute; */
				
			}
			.son3{
				
				height: 100px;
				background-color: #0026ff;
				/* 绝对定位 */
				position: absolute;
				
			}
			.son4{
				
				height: 100px;
				background-color: #08ffeb;
			}
		</style>

只有son3是绝对定位的时候
在这里插入图片描述son2和son3都是绝对定位的时候

.son2{
				height: 100px;
				background-color: #fff701;
				/* 绝对定位 */
				position: absolute;
				
			}

![](https://img-blog.csdnimg.cn/4a41457603a74b579d71c3f0c4163eab.png)

son1,son2和son3都是绝对定位的时候

.son1{
				height: 100px;
				background-color: #00ff7f;
				/* 绝对定位 */
				position: absolute;
			}

在这里插入图片描述
son1,son2,son3和son4都是绝对定位的时候

.son4{
				
				height: 100px;
				background-color: #08ffeb;
				/* 绝对定位 */
				position: absolute;
			}

在这里插入图片描述可以看到,元素在没有定义宽度(或高度)的情况下,宽度由元素里面的内容决定

	<style>
				
			.father{
				height: 500px;
				background-color: bisque;
				/* 父元素相对定位 */
				position: relative;
			}
			.son1{
				
				height: 100px;
				background-color: #00ff7f;
				/* 绝对定位 */
				position: absolute;
				z-index: 1000;
			}
			.son2{
				width: 40px;
				height: 100px;
				background-color: #fff701;
				/* 绝对定位 */
				position: absolute;
				z-index: 100;
			}
			.son3{
				width: 50px;
				height: 100px;
				background-color: #0026ff;
				/* 绝对定位 */
				position: absolute;
				z-index: 10;
			}
			.son4{
				width: 70px;
				height: 100px;
				background-color: #08ffeb;
				/* 绝对定位 */
				position: absolute;
				z-index: 0;
			}
		</style>

给元素块添加宽度,并调节上下顺序
在这里插入图片描述

.son1{
				
				/* height: 100px; */
				background-color: #00ff7f;
				/* 绝对定位 */
				position: absolute;
				z-index: 1000;
				
			}
			.son2{
				width: 40px;
				height: 100px;
				background-color: #fff701;
				/* 绝对定位 */
				position: absolute;
				z-index: 100;
				left: 50px;
				top: 50px;
			}
			.son3{
				width: 50px;
				height: 100px;
				background-color: #0026ff;
				/* 绝对定位 */
				position: absolute;
				z-index: 10;
				left: 100px;
				top: 100px;
			}
			.son4{
				width: 70px;
				height: 100px;
				background-color: #08ffeb;
				/* 绝对定位 */
				position: absolute;
				z-index: 0;
				left: 200px;
				top: 200px;
			}

在这里插入图片描述

.son2{
				width: 40px;
				height: 100px;
				background-color: #fff701;
				/* 绝对定位 */
				position: absolute;
				z-index: 100;
				left: 100px;
				top: 100px;
			}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值