css中position的定义

本文通过两个实例详细解析了CSS中absolute和relative定位方式的使用方法。第一个实例展示了如何使用absolute定位使元素相对于其最近的已定位父元素进行偏移。第二个实例则解释了relative定位下元素如何相对于其正常位置进行偏移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.absolute:

<html>
<head>
<title>position</title>
<style>
#father {
	width:200px;
	height:100px;
	position:reletive;
	background-color:blue;
}
#son {
	width:100px;
	height:50px;
	position:absolute;
	background-color:gray;
	top:20px;
	left:40px;
}
</style>
</head>
<body>
	<div id="father">
		<div id="son">son</div>
	</div>
</body>
</html>

上面代码块中,son设置了position为absolute,father设置了position为relative,则会出现son相对father向下20px,向右偏40px;


2.

<html>
<head>
<title>position</title>
<style>
#father {
	width:100%;
	height:100%;
	background-color:blue;

}

#son {
	width:100px;
	height:50px;
	position:relative;
	background-color:gray;
	top:10%;
	left:40px;

}
</style>

</head>
<body>
	<div id="father">
		<div id="son">son</div>
	</div>
</body>
</html>
上面的代码,son的position设置了relative,同事top为10%,则无论页面高度为多少,son始终距离son应该在的位置下方10%,fature没有设置padding,son没有设置magin,则10%应该是距离father最顶端10%,左右也是如此。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值