CSS position:absolute全面了解

春季是一个富有生命力季节,也是一个美丽、神奇,充满希望季节。都说春季是花海洋。粉色淡雅,白色端庄,红色热烈,紫色深情,泼泼洒洒,浓浓烈烈。一朵朵花,把春季朝气蓬勃都开出来了。

一、绝对定位的特征

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

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

请看下面代码:

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPEhtml>
  2. <html>
  3. <head>
  4. <metacharset="utf-8">
  5. <title>绝对定位的特征</title>
  6. <style>
  7. .box1,.box2,.box3{
  8. background-color:orange;
  9. margin-bottom:30px;
  10. }
  11. .absolute{
  12. position:absolute;
  13. }
  14. .wraper{
  15. display:inline-block;
  16. margin-left:300px;
  17. }
  18. .float{
  19. float:left;
  20. }
  21. .box3{
  22. position:absolute;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <divclass="box1">
  28. <imgsrc="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg"alt="Apicture"style="width:175px;height:100px"/>
  29. <imgsrc="http://pic1.win4000.com/wallpaper/c/537b28b60619b.jpg"alt="Apicture"style="width:240px;height:180px"/>
  30. <p>这是普通流中的两幅图像。</p>
  31. </div>
  32. <divclass="box2">
  33. <imgclass="absolute"src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg"alt="Apicture"style="width:175px;height:100px"/>
  34. <imgsrc="http://pic1.win4000.com/wallpaper/c/537b28b60619b.jpg"alt="Apicture"style="width:240px;height:180px"/>
  35. <divclass="wraper">
  36. <imgclass="float"src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg"alt="Apicture"style="width:175px;height:100px"/>
  37. <imgsrc="http://pic1.win4000.com/wallpaper/c/537b28b60619b.jpg"alt="Apicture"style="width:240px;height:180px"/>
  38. </div>
  39. <p>左图,将第一幅图像绝对定位,其完全脱离文档流,并且覆盖在第二幅图像之上;由此看出,绝对定位的破坏性不仅让元素没有了高度,甚至没有了宽度。</p>
  40. <p>右图,将第一幅图像左浮动,其虽然脱离了文档流,但是并没有覆盖在其他元素之上;浮动的破坏性仅仅破坏了元素的高度,而保留了元素的宽度。</p>
  41. </div>
  42. <divclass="box3">
  43. <imgsrc="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg"alt="Apicture"style="width:175px;height:100px"/>
  44. <imgsrc="http://pic1.win4000.com/wallpaper/c/537b28b60619b.jpg"alt="Apicture"style="width:240px;height:180px"/>
  45. <p>将容器绝对定位,体现其包裹性。</p>
  46. </div>
  47. </body>
  48. </html>

二、绝对定位的一般规则:

元素绝对定位时,会完全脱离文档流,并相对于其包含块定位。

绝对定位的包含块,是其最近的已定位的祖先元素。

如果这个祖先元素是块级元素,包含块则为该祖先元素的内边距边界,即边框。

如果这个祖先元素是行内元素,包含块则为该祖先元素的内容边界,即内容区。

如果没有已定位的祖先元素,元素的包含块定义为初始包含块。

偏移属性:top、right、bottom、left。

如果绝对定位的元素没有设置偏移属性,虽然脱离文档流,但是它的位置是原地不动的。

偏移属性可以为负值,将元素定位到包含块之外。

代码在这里:

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPEhtml>
  2. <html>
  3. <head>
  4. <metacharset="utf-8">
  5. <title>绝对定位的一般规则</title>
  6. <style>
  7. body{
  8. background-color:#ccc;
  9. }
  10. .container{
  11. width:500px;
  12. background-color:orange;
  13. margin:20pxauto50pxauto;
  14. padding:20px;
  15. border:2pxsolidred;
  16. }
  17. .box2img,.box3img,
  18. .box4img,.box5img{
  19. position:absolute;
  20. }
  21. .box3img,.box4img{
  22. left:0;
  23. bottom:0;
  24. }
  25. .box5img{
  26. left:-30px;
  27. bottom:-50px;
  28. }
  29. .block{
  30. position:relative;
  31. height:200px;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <divclass="container">
  37. <divclass="box1">
  38. <p>元素绝对定位时,会完全脱离文档流,并相对于其包含块定位。绝对定位的包含块,是其最近的已定位的祖先元素。</p>
  39. <imgsrc="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg"alt="Apicture"style="width:175px;height:100px"/>
  40. <ul>
  41. <li>如果这个祖先元素是块级元素,包含块则为该祖先元素的内边距边界,即边框。</li>
  42. <li>如果这个祖先元素是行内元素,包含块则为该祖先元素的内容边界,即内容区。</li>
  43. <li>如果没有已定位的祖先元素,元素的包含块定义为初始包含块(一个视窗大小的矩形)。</li>
  44. </ul>
  45. </div><!--关闭box1-->
  46. </div><!--关闭container-->
  47. <divclass="container">
  48. <divclass="box2">
  49. <p>不管有没有已经定位的祖先元素,只要没有偏移量,绝对定位之后,原地不动,脱离文档流。</p>
  50. <p>下面这个已经绝对定位的图像原地没动,但是已经脱离了文档流。</p>
  51. <imgsrc="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg"alt="Apicture"style="width:175px;height:100px"/>
  52. </div><!--关闭box2-->
  53. </div><!--关闭container-->
  54. <divclass="container">
  55. <divclass="box3">
  56. <p>没有已经定位的祖先元素,有偏移量,绝对定位之后,以初始包含块(一个视窗大小的矩形)为基准进行偏移。</p>
  57. <p>当偏移量为left:0;buttom:0时,图像水平偏移到了初始包含块左下角(打开网页最开始的那一个视窗的左下角)。</p>
  58. <imgsrc="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg"alt="Apicture"style="width:175px;height:100px"/>
  59. </div><!--关闭box3-->
  60. </div><!--关闭container-->
  61. <divclass="containerblock">
  62. <divclass="box4">
  63. <p>有已经定位的祖先元素,有偏移量,绝对定位之后,以已经定位的祖先元素为基准进行偏移。</p>
  64. <p>此处已经定位的祖先元素为这个图像的容器div元素,偏移量为left:0;bottom:0时,图像到了这个容器的左下角(以边框为界)。</p>
  65. <imgsrc="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg"alt="Apicture"style="width:175px;height:100px"/>
  66. </div><!--关闭box4-->
  67. </div><!--关闭container-->
  68. <divclass="containerblock">
  69. <divclass="box5">
  70. <p>有已经定位的祖先元素,有偏移量,绝对定位之后,以已经定位的祖先元素为基准进行偏移。</p>
  71. <p>此处已经定位的祖先元素为这个图像的容器div元素,偏移量为left:-30px;bottom:-50px时,图像到了这个容器之外(以边框为界)。</p>
  72. <imgsrc="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg"alt="Apicture"style="width:175px;height:100px"/>
  73. </div><!--关闭box5-->
  74. </div><!--关闭container-->
  75. </body>
  76. </html>

三、用margin调整绝对定位元素的位置

绝对定位的元素,除了可以使用top、right、bottom、left进行偏移之外,还能够通过margin值进行精确定位,而且自适应性更好。

示例:

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPEhtml>
  2. <html>
  3. <head>
  4. <metacharset="utf-8">
  5. <title>用margin调整绝对定位元素的位置</title>
  6. <style>
  7. .box1,.box2,.box3{
  8. display:inline-block;
  9. margin-right:150px;
  10. border:1pxsolidblue;
  11. }
  12. span{
  13. background-color:red;
  14. }
  15. .box2span,.box3span{
  16. position:absolute;
  17. }
  18. .meng{
  19. margin-left:-3em;
  20. }
  21. .box4{
  22. border:1pxsolidred;
  23. width:500px;
  24. margin:50pxauto0auto;
  25. padding:20px;
  26. }
  27. li{
  28. margin-bottom:20px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <divclass="box1">
  34. <span>卡哇伊</span>
  35. <imgsrc="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"style="width:200px;height:300px"/>
  36. <span>萌萌哒</span>
  37. </div>
  38. <divclass="box2">
  39. <span>卡哇伊</span>
  40. <imgsrc="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"style="width:200px;height:300px"/>
  41. <span>萌萌哒</span>
  42. </div>
  43. <divclass="box3">
  44. <span>卡哇伊</span>
  45. <imgsrc="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"style="width:200px;height:300px"/>
  46. <spanclass="meng">萌萌哒</span>
  47. </div>
  48. <divclass="box4">
  49. <ol>
  50. <li>第一幅图,最开始的样子。</li>
  51. <li>第二幅图,两个标签绝对定位,但是不指定任何偏移量。</li>
  52. <li>第三幅图,用margin负值调整“萌萌哒”的位置,完成。</li>
  53. </ol>
  54. </div>
  55. </body>
  56. </html>

放弃偏移属性而改用margin来调整绝对定位元素,其原理在于:

绝对定位的元素,在不设置偏移量的时候,它虽然完全脱离了文档流,但它还在原来的位置。

利用偏移属性进行精确定位,其具体位置是取决于绝对定位元素的包含块,不同的包含块将会有完全不一样的定位结果。

而利用margin进行精确定位,不依赖于任何其他东西,更加可控。

四、绝对定位与整体布局

如何用绝对定位来对页面进行整体布局?

简单粗暴,不学就浪费啦!!!

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPEhtml>
  2. <html>
  3. <head>
  4. <metacharset="utf-8">
  5. <title>绝对定位与整体页面布局</title>
  6. <style>
  7. *{
  8. margin:0;
  9. }/*重置所有margin为0,这一步极其重要,否则布局必乱。*/
  10. html,body,.page{
  11. width:100%;
  12. height:100%;
  13. overflow:hidden;
  14. }
  15. .page{
  16. position:absolute;
  17. top:0;
  18. right:0;
  19. bottom:0;
  20. left:0;
  21. background-color:#ccc;
  22. }
  23. .header{
  24. position:absolute;
  25. height:50px;
  26. left:0;
  27. right:0;
  28. background-color:purple;
  29. padding:05px;
  30. z-index:1;
  31. }
  32. .header>h1{
  33. line-height:50px;
  34. text-align:center;
  35. }
  36. .aside{
  37. position:absolute;
  38. top:50px;
  39. bottom:50px;
  40. left:0;
  41. width:100px;
  42. background-color:orange;
  43. }
  44. .footer{
  45. position:absolute;
  46. right:0;
  47. bottom:0;
  48. left:0;
  49. height:50px;
  50. background-color:purple;
  51. }
  52. .footer>h1{
  53. text-align:center;
  54. line-height:50px;
  55. }
  56. .content{
  57. position:absolute;
  58. top:50px;
  59. right:0;
  60. bottom:50px;
  61. left:100px;
  62. background-color:cyan;
  63. overflow:auto;
  64. }
  65. .contenth1{
  66. margin-top:100px;
  67. margin-left:50px;
  68. }
  69. .contentli{
  70. margin-left:100px;
  71. margin-top:80px;
  72. font-size:24px;
  73. line-height:1.5;
  74. }
  75. ol{
  76. margin-bottom:80px;
  77. }
  78. </style>
  79. </head>
  80. <body>
  81. <divclass="page">
  82. <divclass="header">
  83. <h1>Header</h1>
  84. </div>
  85. <divclass="aside">
  86. <h1>Aside</h1>
  87. </div>
  88. <divclass="content">
  89. <h1>实现原理</h1>
  90. <ol>
  91. <li>创建一个div.page,绝对定位,铺满全屏。</li>
  92. <li>创建一个div.header,绝对定位,设定高度。</li>
  93. <li>创建一个div.aside,绝对定位,设定宽度。</li>
  94. <li>创建一个div.footer,绝对定位,设定高度。</li>
  95. <li>创建一个div.content,绝对定位,根据周围div的宽高设定它的宽高;<br/>
  96. 以div.content元素取代原body元素,所有的页面内容都放在这里面。</li>
  97. </ol>
  98. </div>
  99. <divclass="footer">
  100. <h1>Footer</h1>
  101. </div>
  102. </div>
  103. </body>
  104. </html>

以上这篇CSS position:absolute全面了解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

原文地址:http://www.cnblogs.com/cc156676/archive/2016/07/19/5685300.html

标签: CSS position