小李带你玩转html—08

定位

css布局:盒模型、浮动、定位
辅助的功能属性:文本、a、背景等
定位:相对定位、绝对定位、固定定位
脱标:浮动、绝对定位、固定定位
  • 相对定位

定位:元素位置相对于某一参照物进行的位置偏移
相对定位:元素相对于自身的相对偏移
position:定位
属性值:relative 相对的
偏移的数据量由其他属性来表示

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>相对定位</title>
	</head>
	<style type="text/css">
		*{
			padding: 0;
			margin: 0;
		}
		div{
			width: 100px;
			height: 100px;
			background: pink;
			margin-bottom: 10px;
		}
		.box{
			background: skyblue;
			position: relative;
			left: 100px; //向右移动了100px 一个盒子的宽度
			top: 110px;	//像下移动了110px 一个盒子的高度加margin值
		}
	</style>
	<body>
		<div>1</div>
		<div class="box">2</div>
		<div>3</div>
		<div>4</div>
	</body>
</html>

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>相对定位</title>
	</head>
	<style type="text/css">
		*{
			padding: 0;
			margin: 0;
		}
		div{
			width: 100px;
			height: 100px;
			background: pink;
			margin-bottom: 10px;
		}
		.box{
			background: skyblue;
			position: relative;
			/*left: 100px;
			top: 110px;*/
			right: 20px;
			bottom: 20px;
		}
	</style>
	<body>
		<div>1</div>
		<div class="box">2</div>
		<div>3</div>
		<div>4</div>
	</body>
</html>

在这里插入图片描述
定位改变之后的图片不会被下面的图片顶掉

  • 绝对定位

绝对定位的定位参考元素,绝对定位从左上角算起
属性值:absolute
也有四个方向的偏移值:left right top bottom

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>绝对定位</title>
	</head>
	<style type="text/css">
		*{
			margin:0;
			padding:0;
		}
		div{
			width: 100px;
			height: 100px;
			background: pink;
			margin-bottom: 10px;
		}
		.box{
			background: skyblue;
			position: absolute;
			top: 100px;
			left: 100px;
		}
	</style>
	<body>
		<div>1</div>
		<div class="box">2</div>
		<div>3</div>
		<div>4</div>
	</body>
</html>

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>绝对定位</title>
	</head>
	<style type="text/css">
		*{
			margin:0;
			padding:0;
		}
		div{
			width: 100px;
			height: 100px;
			background: pink;
			margin-bottom: 10px;
		}
		.box{
			background: skyblue;
			position: absolute;
			top: 100px;
			/*left: 100px;*/
			right: 100px;
		}
	</style>
	<body>
		<div>1</div>
		<div class="box">2</div>
		<div>3</div>
		<div>4</div>
	</body>
</html>

在这里插入图片描述

特点:与相对定位比较,元素绝对定位之后,脱离标准流,标准流的位置让给了后面的元素

  • 用绝对定位制作压盖效果
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>压盖效果</title>
	</head>
	<style type="text/css">
		*{
			margin: 0;
			padding: 0;
		}
		.box{
			width: 400px;
			height: 400px;
			border: 1px solid #000;
			padding: 20px;
			margin: 100px auto;
			position: relative;
		}
		.box .demo1{
			width: 400px;
			height: 400px;
			background: skyblue;
		}
		.box .demo2{
			width: 100px;
			height: 30px;
			background:gold;
			position: absolute;
			left: 160px;
			bottom: 40px;
		}
		
	</style>
	<body>
		<div class="box">
			<div class="demo1">
				
			</div>
			<div class="demo2">
				
			</div>
		</div>
	</body>
</html>

在这里插入图片描述

  • 盒子居中
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>盒子居中</title>
	</head>
	<style type="text/css">
		*{
			padding: 0;
			margin: 0;
		}
		.box{
			width: 400px;
			height: 400px;
			border: 1px solid #000;
			padding: 20px;
			margin: 100px auto;
			position: relative;
		}
		.box .demo1{
			width: 400px;
			height: 400px;
			background: skyblue;
		}
		.box .demo2{
			width: 100px;
			height: 100px;
			background:gold;
			position: absolute;
			left: 50%;
			bottom: 50%;
			margin-left: -50px;
			margin-bottom:-50px
		}
		
	</style>
	<body>
		<div class="box">
			<div class="demo1">
				
			</div>
			<div class="demo2">
				
			</div>
		</div>
	</body>
</html>

在这里插入图片描述

  • 固定定位
    参考浏览器的窗口进行定位
    属性值:fixed
    始终保持针对浏览器某一个顶点位置相对不变
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>固定定位</title>
		<style type="text/css">
			*{
				padding: 0;
				margin: 0;	
			}
			.box{
				position: fixed; //针对浏览器固定定位
				
				/*bottom: 300px;*/
				left: 80px;
				width: 80px;
				height: 80px;
				background: #ccc;
				line-height: 80px;
				font-size: 28px;
				color: #333;
				text-decoration: none;
				text-align: center;
				margin-top: 800px;
			}
		</style>
	</head>
	<body>
		<div class="box">
			
		
		<a  href="#">
			Top
		</a>
		</div>
		<p><img src="images/A1 (1).jpeg" alt="" /></p>
		<p><img src="images/A1 (2).jpeg" alt="" /></p>
		<p><img src="images/A1 (3).jpeg" alt="" /></p>
		<p><img src="images/A1 (4).jpeg" alt="" /></p>
		<p><img src="images/A1 (5).jpeg" alt="" /></p>
		<p><img src="images/A1 (6).jpeg" alt="" /></p>
		<p><img src="images/A1 (7).jpeg" alt="" /></p>
		<p><img src="images/A1 (8).jpeg" alt="" /></p>
	</body>
</html>

在这里插入图片描述
在这里插入图片描述

  • 压盖顺序 z-index
    默认压盖顺序:
    ①有定位的元素的压盖没有定位的元素
    ②有定位的元素,不区分定位类型,压盖顺序按书写顺序来写
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>默认压盖顺序</title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			.box1{
				width: 300px;
				height: 300px;
				background: pink;
			}
			.box2{
				width: 200px;
				height: 200px;
				background: skyblue;
				position: relative;
				left: 0;
				bottom: 300px;
			}
			.box3{
				width: 150px;
				height: 150px;
				position: absolute;
				top: 0;
				left: 0;
				background: yellowgreen;
			}
			.box4{
				width: 100px;
				height: 100px;
				position: fixed;
				top: 0;
				left: 0;
				background: gold;
			}
		</style>
	</head>
	<body>
		<div class="box1"></div>
		<div class="box2"></div>
		<div class="box3"></div>
		<div class="box4"></div>
	</body>
</html>

在这里插入图片描述

  • 自定义压盖顺序
    属性:z-index,压盖顺序
    属性值:数字,数字大的压盖数字小的
  • 只能给定位的元素使用,默认的z-index值是1
  • 如果属性值相同,看书写顺序
  • 父子盒模型里,如果父子都有定位,都有自定义z-index值,看父盒子的z-index数值
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>自定义压盖顺序</title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			.box1{
				width: 300px;
				height: 300px;
				background: pink;
			}
			.box2{
				width: 200px;
				height: 200px;
				background: skyblue;
				position: relative;
				left: 0;
				bottom: 300px;
				z-index: 2;
			}
			.box3{
				width: 150px;
				height: 150px;
				position: absolute;
				top: 0;
				left: 0;
				background: yellowgreen;
				z-index: 3;
			}
			.box4{
				width: 100px;
				height: 100px;
				position: fixed;
				top: 0;
				left: 0;
				background: gold;
			}
		</style>
	</head>
	<body>
		<div class="box1"></div>
		<div class="box2"></div>
		<div class="box3"></div>
		<div class="box4"></div>
	</body>
</html>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值