首页 > 开发 > CSS > 正文

CSS position:absolute全面了解

2024-07-11 08:32:37
字体:
来源:转载
供稿:网友

一、绝对定位的特征

绝对定位有着与浮动一样的特性,即包裹性和破坏性。

就破坏性而言,浮动仅仅破坏了元素的高度,保留了元素的宽度;而绝对定位的元素高度和宽度都没有了。

请看下面代码:

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPE html>   <html>  
  2.     <head>           <meta charset="utf-8">  
  3.         <title>绝对定位的特征</title>           <style>  
  4.             .box1,.box2,.box3 {                    background-color: orange;   
  5.                 margin-bottom: 30px;                }   
  6.                .absolute {   
  7.                 position: absolute;                }   
  8.                .wraper {   
  9.                 display:inline-block;                    margin-left: 300px;   
  10.             }      
  11.             .float {                    float: left;   
  12.             }                   
  13.             .box3 {                    position: absolute;   
  14.             }            </style>  
  15.     </head>       <body>  
  16.         <div class="box1">               <img src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg" alt="A picture" style="width:175px;height:100px" />  
  17.             <img src="http://pic1.win4000.com/wallpaper/c/537b28b60619b.jpg" alt="A picture" style="width:240px;height:180px" />               <p>这是普通流中的两幅图像。</p>  
  18.         </div>           <div class="box2">  
  19.             <img class="absolute" src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg" alt="A picture" style="width:175px;height:100px" />               <img src="http://pic1.win4000.com/wallpaper/c/537b28b60619b.jpg" alt="A picture" style="width:240px;height:180px" />  
  20.                <div class="wraper">  
  21.                 <img class="float" src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg" alt="A picture" style="width:175px;height:100px" />                   <img src="http://pic1.win4000.com/wallpaper/c/537b28b60619b.jpg" alt="A picture" style="width:240px;height:180px" />  
  22.             </div>               <p>左图,将第一幅图像绝对定位,其完全脱离文档流,并且覆盖在第二幅图像之上;由此看出,绝对定位的破坏性不仅让元素没有了高度,甚至没有了宽度。</p>  
  23.             <p>右图,将第一幅图像左浮动,其虽然脱离了文档流,但是并没有覆盖在其他元素之上;浮动的破坏性仅仅破坏了元素的高度,而保留了元素的宽度。</p>           </div>  
  24.         <div class="box3">               <img src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg" alt="A picture" style="width:175px;height:100px" />  
  25.             <img src="http://pic1.win4000.com/wallpaper/c/537b28b60619b.jpg" alt="A picture" style="width:240px;height:180px" />               <p>将容器绝对定位,体现其包裹性。</p>  
  26.         </div>       </body>  
  27. </html>  
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表