前端路线--HTML篇(day08)

浮动带来的影响

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		/*子元素浮动过后会造成父元素高度为0*/
		.box1{
			width: 200px;
			height: 200px;
			background-color: #f00;
			float: left;
		}
		.box2{
			width: 200px;
			height: 200px;
			background-color: #00f;
			float: left;
		}
		.pox{
			width: 400px;
			height: 100px;
			background-color: #0f0;
		}
	</style>
</head>
<body>
	<div class="box">
		<div class="box1"></div>
		<div class="box2"></div>
	</div>
	<div class="pox">
		
	</div>
</body>
</html>

清除浮动

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.box1{
			width: 200px;
			height: 200px;
			background-color: #f00;
			float: left;
		}
		.box2{
			width: 200px;
			height: 200px;
			background-color: #00f;
			float: left;
		}
		.pox{
			width: 400px;
			height: 100px;
			background-color: #0f0;
		}
		.box{
			
		}
		.clearfix:after{  
		    content: "."; 
		    display: block; 
		    height: 0; 
		    clear: both; 
		    visibility: hidden;  
		}   
		/*1、给浮动元素的父元素添加宽高*/
		/*2、给被影响的元素添加clear: both;*/
		/*3、给浮动元素的父元素添加overflow: hidden;*/
		/*4、给父级元素添加clearfix类名*/

		/*------reset------*/
		body,dl,dd,ul,ol,h1,h2,h3,h4,h5,h6,p,form{margin:0;}
		/*------common------*/
		.clearfix:after{content:'\20';display:block;height:0;line-height:0;visibility:hidden;clear:both}
		.f12{font-size:12px}
		.f16{font-size:16px}
		.f22{font-size:22px}
		.tc{text-align:center}
		.tr{text-align:right}
	</style>
</head>
<body>
	<div class="box clearfix">
		<div class="box1"></div>
		<div class="box2"></div>
	</div>
	<div class="pox">
		
	</div>
</body>
</html>

固定定位

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.box1{
			width: 800px;
			height: 400px;
			margin: 100px auto;
			background-color: #f00;
		}
		.box2{
			width: 800px;
			height: 400px;
			margin: 100px auto;
			background-color: #f0f;
		}
		.box3{
			width: 800px;
			height: 400px;
			margin: 100px auto;
			background-color: #ff0;
		}

		.box4{
			width: 200px;
			height: 200px;
			background-color: #0ff;

			/*固定定位:脱离文档流,基于浏览器窗口进行定位*/
			/*定位元素必须配合偏移量来使用*/
			position: fixed;
			top: 300px;
			left: 300px;

			z-index: 2;
		}
		.box5{
			width: 200px;
			height: 200px;
			background-color: #00f;

			/*固定定位:脱离文档流,基于浏览器窗口进行定位*/
			position: fixed;
			top: 400px;
			left: 400px;

			/*只能用于定位元素,值越高层级越高*/
			z-index: -3;
		}
	</style>
</head>
<body>
	<div class="box1"></div>
	<div class="box2"></div>
	<div class="box3"></div>

	<div class="box4"></div>
	<div class="box5"></div>
</body>
</html>

相对定位

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.box1{
			width: 200px;
			height: 200px;
			margin: 100px auto 0px;
			background-color: #f00;
		}
		.box2{
			width: 200px;
			height: 200px;
			margin: 0px auto;
			background-color: #f0f;

			/*相对定位:没有脱离文档流,根据自身原来的位置进行定位*/
			position: relative;
			top: 0px;
			left: 200px;
		}
		.box3{
			width: 200px;
			height: 200px;
			margin: 0px auto;
			background-color: #ff0;
		}
	</style>
</head>
<body>
	<div class="box1"></div>
	<div class="box2"></div>
	<div class="box3"></div>
</body>
</html>

绝对定位

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.box1{
			width: 600px;
			height: 600px;
			margin: 100px auto 0px;
			background-color: #f00;

			position: relative;
		}
		.box2{
			width: 400px;
			height: 400px;
			margin: 0px auto;
			background-color: #f0f;
			
		}
		.box3{
			width: 200px;
			height: 200px;
			margin: 0px auto;
			background-color: #ff0;


			/*子绝父相*/
			/*绝对定位:脱离文档流,根据已有定位元素的父元素进行定位*/
			position: absolute;
			top: 0px;
			left: 0px;
		}
	</style>
</head>
<body>
	<div class="box1">
		<div class="box2">
			<div class="box3"></div>
		</div>
	</div>
	
</body>
</html>

利用定位实现盒子快速居中

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.box1{
			width: 200px;
			height: 200px;
			background-color: #f00;

			position: absolute;
			top: 50%;
			left: 0px;
			margin-top: -100px;

			/*通过定位直接添加百分之50、再添加外边距负盒子高度或者宽度的一半*/
		}
	</style>
</head>
<body>
	<div class="box1">
	</div>
	
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值