CSS定位介绍

定位(position)

  1. 边偏移
    • top 顶端偏移量 定义元素相对于其父元素上边线的距离
    • left 定义元素相对于其父元素左边线的距离
    • right 定义元素相对于其父元素右边线的距离
    • bottom 定义元素相对于其父元素下边线的距离
  2. 静态定位 (浏览器默认为静态定位)
    • position: static
    • 元素默认的定位方式,假如元素position设置为static,则元素定位在静态位置(即文档流中默认的位置)
  3. 相对定位
    • position: relative
    • 相对自己做偏移,相对定位作用 1 做一些位置微调 2 配合绝对定位使用
    • 注意 相对定位的元素 不脱标,之前的位置仍然占有
  4. 绝对定位
    • position: absolute
    • 绝对定位的盒子脱标
    • 工程上 常常使用子绝父相,父亲相对定位 占有位置不脱标,儿子采用绝对定位 不占有位置 完全脱标
  5. 固定定位(position: fixed)
    • 参考点 浏览器左上角
    • 固定定位的元素脱标不占有位置
    • 兼容性 ie6低版本不支持固定定位

静态定位

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
	   div  {
	   	  width: 200px;
	   	  height: 200px;
	   	  background-color: red;
	   	 /*  position: static;
	   	 left: 200px;
	   	 top: 200px; */
	   }
	</style>
</head>
<body>
	<div></div>
</body>
</html>

返回顶部


相对定位

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
	   div {
	   	   width: 100px;
	   	   height: 100px;
	   	   background-color: red;
	   	   margin-bottom: 20px;
	   }

	   div:nth-child(2) {
	   	  position: relative;
	   	  left: 150px;
	   	  top: 150px;
	   }


	</style>
</head>
<body>
	<div></div>
	<div></div>
	<div></div>
</body>
</html>

返回顶部


绝对定位

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
	   div {
	   	   width: 100px;
	   	   height: 100px;
	   	   background-color: red;
	   	   margin-bottom: 20px;
	   }

	   div:nth-child(1) {
	   	  /*绝对定位盒子脱标 不占位置*/
	   	  position: absolute;
	   	  left: 150px;
	   	  top: 150px;
	   	  background-color: yellow;
	   }


	</style>
</head>
<body>
	<div></div>
	<div></div>
	<div></div>
</body>
</html>

绝对定位的参考点

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

	   .fa .son {
	   	   width: 100px;
	   	   height: 100px;
	   	   background-color: green;
	   	   /* 假如父亲没有定位 儿子以浏览器为基准点对齐*/
	   	   position: absolute;
	   	   left: 20px;
	   	   top: 20px;
	   }
	</style>
</head>
<body>
	<div class="fa">
		<div class="son"></div>
	</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
	   .fa {
	   	   width: 200px;
	   	   height: 200px;
	   	   background-color: red;
	   	   margin: 200px;
	   	   position: relative;
	   	   top: 100px;
	   	   left: 100px;
	   }

	   .fa .son {
	   	   width: 100px;
	   	   height: 100px;
	   	   background-color: green;
	   	   /* 假如父亲有定位(相对或者绝对) 儿子以父亲基准点对齐*/
	   	   position: absolute;
	   	   left: 20px;
	   	   top: 20px;
	   }
	</style>
</head>
<body>
	<div class="fa">
		<div class="son"></div>
	</div>
</body>
</html>

绝对定位的应用

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
	    .fa {
	    	width: 600px;
	    	height: 300px;
	    	margin: 60px auto;
	    	background-color: skyblue;
	    	position: relative;
	    }
           
        /*让儿子盒子水平居中*/   
	    .son {
	    	width: 200px;
	    	height: 100px;
	    	background-color: pink;
	    	/* margin: 0 auto; 对绝对定位的盒子无效*/
	    	position: absolute;
	    	/*做法一(推荐):*/
	    	left: 50%; /*百分比参考父亲的宽度*/
	    	margin-left: -100px; /*自己的宽一半*/
	    	top: 50%;
	    	margin-top: -50px;
	    	/*做法二:*/
	    	/* left: 300px;
	    	top: 150px; */
	    }
	</style>
</head>
<body>
	<div class="fa">
		<div class="son"></div>
	</div>
</body>
</html>

返回顶部


固定定位

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
	    * {
	    	padding: 0;
	    	margin: 0;
	    } 

	    body {
	    	height: 2000px;
	    }

	    .fa {
	    	width: 600px;
	    	height: 300px;
	    	margin: 60px auto;
	    	background-color: skyblue;
	    	position: relative;
	    }

	    span {
	    	width: 300px;
	    	height: 100px;
	    	background-color: green;
	    	position: fixed;
	    	top: 60px;
	    	left: 50px;
	    }
	</style>
</head>
<body>
	<div class="fa">
		<span class="son">我是一段文字</span>
	</div>
</body>
</html>

返回顶部


返回定位目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值