博主信息
博文 12
粉丝 0
评论 0
访问量 12174
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
浮动布局与定位布局
这位同学问得好的博客
原创
783人浏览过

一、浮动布局

<!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举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学