JavaWeb_CSS(13)_定位_相对定位

本系列博客汇总在这里:JavaWeb_CSS 汇总


一、相对定位

  1. 相对定位是一个非常容易理解的概念,如果对一个元素先对定位,可以设置其水平位置和垂直位置,这个元素相对于元素的 起点 (没有定位的左上角原始位置)开始移动。
  2. 注意:在使用相对定位的时候无论是否进行移动,元素仍然会占据原有的空间,因此移动元素可能覆盖其他元素。

示例

<!DOCTYPE html>
<html>
	<head>	
		<meta charset="UTF-8">
		<title> www.weiyuxuan.com </title>
	</head>	
	<style type="text/css">	
		body
		{
			margin: 10px;
			font-size: 12px;
			font-family: Arial;
		}		
		.outside
		{
			width: 500px;
			height: 200px;
			background-color: #a9d6ff;
			border: 1px dashed black;
		}		
		.inside
		{
			padding: 10px;
			background-color: #fffcd3;
			border: 1px dashed black;
			width: 100px;
			position: relative;
			
			left: 20px;
			top: 80px;					
		}		
	</style>	
	<h1> 定位  </h1>	
	<hr>	
	<body>	
		<div class="outside">
			<div class="inside"> 相对原点的定位 </div>
		</div>	
	</body>	
</html>

在这里插入图片描述

二、CSS 中 div 相对定位对文档流的影响

div1 相对定位脱离了文档流,但是后续的 div 还会认为 div1 是在没有定位之前的状态,所有后续的 div 不会填补 div1 的空缺位置,而是继续按着文档流来排序。

示例

<!DOCTYPE html>
<html>
	<head>	
		<meta charset="UTF-8">
		<title> www.weiyuxuan.com </title>
	</head>	
	<style type="text/css">	
		body
		{
			margin: 10px;
			font-size: 12px;
			font-family: Arial;
		}		
		.outside
		{
			width: 500px;
			height: 200px;
			background-color: #a9d6ff;
			border: 1px dashed black;
		}		
		.inside
		{
			margin:10px;
			padding: 10px;
			background-color: #fffcd3;
			border: 1px dashed black;
			position: relative;
			
			left: 100px;
			top: 220px;					
		}		
		.inside1
		{
			margin:10px;
			padding: 10px;
			background-color: #fffcd3;
			border: 1px dashed black;			
		}				
	</style>	
	<h1> 定位  </h1>
	<hr>
	<body>	
		<div class="outside">
			<div class="inside"> 相对原点的定位 div1 </div>
			<div class="inside1"> 相对原点的定位 div2 </div>
		</div>	
	</body>	
</html>

在这里插入图片描述

如有错误,欢迎指正!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值