float与absolute脱离文档流的区别

CSS中脱离文档流,也就是将元素从普通的布局排版中拿走,其他盒子在定位的时候,会当做脱离文档流的元素不存在而进行定位。
  需要注意的是,使用float脱离文档流时,其他盒子会无视这个元素,但其他盒子内的文本依然会为这个元素让出位置,环绕在周围。
  而对于使用absolute positioning脱离文档流的元素,其他盒子与其他盒子内的文本都会无视它。
float时:

[html]  view plain  copy
  1. <html>  
  2.     <head>  
  3.         <title></title>  
  4.         <style type="text/css">  
  5.             *{padding: 0;margin: 0}  
  6.             .box1{width:100px;background-color:red;float:left}    
  7.             .box2{width:100px;background-color:gray;float:right}      
  8.             .box3{background-color: aqua;margin: 0 110;}          
  9.         </style>  
  10.     </head>  
  11.     <body>  
  12.         <div class="box1">box1</div>  
  13.         <div class="box2">box2</div>  
  14.         <div class="box3">box3</div>  
  15.     </body>  
  16. </html>  

显示效果如下:可见,box3的margin是box3边框到浏览器的距离,box3的位置并没有收到float的box1 box2的影响。

当把box3样式改为:.box3{background-color: aqua;}时,可以看到box3中间的文本依然为box1 box2让出了位置。 

position:absolute时:

[html]  view plain  copy
  1. <html>  
  2.     <head>  
  3.         <title></title>  
  4.         <style type="text/css">  
  5.             *{padding: 0;margin: 0}  
  6.             .box1{width:100px;background-color:red;position:absolute;left:0;top:0}    
  7.             .box2{width:100px;background-color:gray;position:absolute;right:0;top:0}      
  8.             .box3{background-color: aqua;}        
  9.         </style>  
  10.     </head>  
  11.     <body>  
  12.         <div class="box1">box1</div>  
  13.         <div class="box2">box2</div>  
  14.         <div class="box3">box3</div>  
  15.     </body>  
  16. </html>  

显示效果:

可以看到,box3中的文本不见了,被box1遮盖了,也可以反映出,box3的定位也是不受到绝对定位的box1 box2的影响,而且box3的文字也没有为box1 box2让出位置。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值