html5-css:关于float,position的定位问题,文档流的解析

自己总结,全作参考,请以权威文档为准;

  • 以下是一段代码,该代码中的四个DIV并没有做任何 float 或 position 的 css 属性设定;

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			div{
				width: 250px;
				padding-top: 50px;
				padding-bottom: 50px;
				text-align: center;
			}
			div:nth-child(1){
				margin-left: 0px;
				background-color: cyan;
			}
			div:nth-child(2){
				margin-left: 200px;
				background-color: chartreuse;
			}
			div:nth-child(3){
				margin-left: 400px;
				background-color: crimson;
			}
			div:nth-child(4){
				margin-left: 600px;
				background-color: orange;
			}
		</style>
	</head>
	
	<body>
		<div>this is an ordinary div</div>
		<div>this is an ordinary div</div>
		<div>this is an ordinary div</div>
		<div>this is an ordinary div</div>
	</body>
</html>

显示结果如下:


  • 接下来,对红色方块设定float属性:float=right;

将上例style替换为:

<style type="text/css">
			div{
				width: 250px;
				padding-top: 50px;
				padding-bottom: 50px;
				text-align: center;
			}
			div:nth-child(1){
				margin-left: 0px;
				background-color: cyan;
			}
			div:nth-child(2){
				margin-left: 200px;
				background-color: chartreuse;
			}
			div:nth-child(3){
				margin-left: 400px;
				float: right;
				background-color: crimson;
			}
			div:nth-child(4){
				margin-left: 600px;
				background-color: orange;
			}
		</style>

显示结果如下:


可见红色方块在向右浮动,且是基于其原本的位置向右浮动。红色方块的div之后的div块(这里的测试中的橙色块)的定位无视它前面的浮动块。但其中的文本却受到红色浮动块的margin-top属性的影响,当我们去掉红色块的margin-top属性后,显示如下:


可见去掉红色块的margin-top的属性后,它后面的橙色块中的文本恢复正常。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值