博客列表 >浮动布局与定位布局

浮动布局与定位布局

这位同学问得好的博客
这位同学问得好的博客原创
2019年09月05日 17:36:46609浏览

一、浮动布局

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			.left{float: left; width: 200px; height: 200px; background: paleturquoise;}
			.right{float: right; width: 200px; height: 200px; background: palegoldenrod;}
		</style>
	</head>
	<body>

			<div class="left">float:left  左浮动</div>
			<div class="right">float:right  右浮动</div>

	</body>
</html>

    

浮动对父级元素的影响:造成父级元素高度折叠为0

实例

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			.box{ background: #DDDDDD;}
			.left{float: left; width: 200px; height: 200px; background: paleturquoise;}
			.right{float: right; width: 200px; height: 200px; background: palegoldenrod;}
		</style>
	</head>
	<body>
		<div class="box">	
			<div class="left">float:left  左浮动</div>
			<div class="right">float:right  右浮动</div>
		</div>
	</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


解决方法:

实例

!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			.box{ height:200px; background: #DDDDDD; margin-top: 20px;}
			.left{float: left; width: 200px; height: 200px; background: paleturquoise;}
			.right{float: right; width: 200px; height: 200px; background: palegoldenrod;}
			.box2{overflow: hidden; background: palevioletred; margin-top: 20px;}
			.clearfix{clear: both;}
			.box3{background: greenyellow; margin-top: 20px;}
			.box4{float: left; width: 100%; background: darkcyan; margin-top: 20px;}
			.box5{background: #009F95; margin-top: 20px;}
			.clearfixs:after{content:"."; display:block; height:0; clear:left; visibility:hidden;}
		</style>
	</head>
	<body>
		<div class="box">	
			1:为父级设置高度
			<div class="left">float:left  左浮动</div>
			<div class="right">float:right  右浮动</div>
		</div>
		
		<div class="box2">	
			2:为父级设置 overflow: hidden; 样式
			<div class="left">float:left  左浮动</div>
			<div class="right">float:right  右浮动</div>
		</div>
		
		<div class="box3">	
			3:为父级中插入样式为 clear:both 的空标签
			<div class="left">float:left  左浮动</div>
			<div class="right">float:right  右浮动</div>
			<div class="clearfix"></div>
		</div>
		<div style="overflow: hidden;">
			<div class="box4">	
				4:为父级 也设置浮动元素
				<div class="left">float:left  左浮动</div>
				<div class="right">float:right  右浮动</div>
			</div>
		</div>
		

		<div class="box5 clearfixs">	
			5:使用CSS的:after伪元素
			<div class="left">float:left  左浮动</div>
			<div class="right">float:right  右浮动</div>
		</div>
	</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


二、三列布局

实例

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			.box{ height:200px; background: #DDDDDD;overflow: hidden;}
			.left{float: left; width: 200px; height: 200px; background: paleturquoise;}
			.right{float: right; width: 200px; height: 200px; background: palegoldenrod;}
			
			.box2{position: relative; height: 200px; background: #DDDDDD; margin-top: 20px; text-align: center;}
			.aLeft{position: absolute; left: 0; top: 0; bottom: 0; width: 200px; background: paleturquoise;}
			.aRight{position: absolute; right: 0; top: 0; bottom: 0; width: 200px; background: palegoldenrod;}
			
			.box3{display:flex; height: 200px; background: #DDDDDD; margin-top: 20px; text-align: center;}
			.fLeft{width: 200px; height: 200px; background: paleturquoise;}
			.fRight{width: 200px; height: 200px; background: palegoldenrod;}
			.fMain{flex: 1;}
		</style>
	</head>
	<body>
		
		<div class="box">	
			1,通过浮动布局
			<div class="left">float:left  左浮动</div>
			<div class="right">float:right  右浮动</div>
		</div>
		
		<div class="box2">	
			2,通过position: relative 定位布局
			<div class="aLeft">position: absolute;绝对定位left:0</div>
			<div class="aRight">position: absolute;绝对定位right:0</div>
		</div>
		
		<div class="box3">	
			<div class="fLeft">左边固定</div>
			<div class="fMain">3,通过display:flex布局</div>
			<div class="fRight">右边固定</div>
		</div>
	</body>
</html>
运行实例 »

点击 "运行实例" 按钮查看在线实例


总结:float 和 position 都会使元素脱离文档流,就是脱离了自身原本的位置,按最近的同样拥有float或position的父级元素重新定位自身的位置。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议