一、absolute和float有相同的特性,包裹性和破坏性
1、absolute和float的相似(看下面的demo,如果图片在左上角,那么用float和absolute都一样)
1 2 | 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset= "utf-8" > 5 <meta name= "viewport" content= "width=device-width" > 6 <title>absolute和float高尔基</title> 7 <style> 8 body { 9 background-color: #B0DEF5; 10 font-size: 14px; font-family: arial; 11 color: #444; 12 } 13 a { 14 color: #0078b6; 15 text-decoration: none; 16 } 17 a:hover { 18 text-decoration: underline; 19 } 20 em { 21 font-style: normal; 22 color: #aeaeae; 23 margin: 0 5px; 24 } 25 svg { 26 position: absolute; 27 } 28 .container { 29 width: 560px; height: -webkit-calc(100vh - 16px); height: calc(100vh - 16px); 30 margin-left: auto; margin-right: auto; 31 background-color: #fff; 32 } 33 .list { 34 padding: 20px; 35 position: relative; 36 } 37 .space { 38 padding-left: 60px; 39 line-height: 22px; 40 } 41 .space img { 42 vertical-align: text-bottom; 43 } 44 .info { 45 font-size: 12px; 46 overflow: hidden; 47 color: #808080; 48 } 49 .from { 50 float: left; 51 } 52 .from a { 53 color: #9abbc8; 54 } 55 p { 56 margin: 6px 0; 57 } 58 .operate { 59 float: right; 60 } 61 .operate img { 62 vertical-align: text-bottom; 63 margin-right: 3px; 64 } 65 .test { 66 position: relative; 67 font-size: 18px; font-family: 'microsoft yahei' ; 68 } 69 .test p { 70 width: 200px; 71 margin-left: auto; margin-right: auto; 72 padding: 5px; 73 background-color: #f0f0f0; 74 } 75 .test input { 76 width: 20px; height: 20px; 77 vertical-align: -4px; 78 } 79 </style> 80 </head> 81 82 <body> 83 <p class = "container" > 84 <svg xmlns= "http://www.w3.org/2000/svg" width= "520" height= "360" > 85 <path d= "M45 80C45 280,45 320,165 300" stroke-width= "1" stroke= "#444" fill= "none" ></path> 86 </svg> 87 <p class = "list" > 88 <a href= "#" id= "avatar" class = "avatar" style= "float: left;" ><img src= "http://img.mukewang.com/54447ac70001f5cc00500050.jpg" ></a> 89 <p class = "space" > 90 <a href= "//weibo.com/mukewang" target= "_blank" title= "慕课网" >慕课网</a> 91 <img src= "http://img.mukewang.com/54447b1a0001750000160013.png" > 92 <a href= "http://huati.weibo.com/k/%E6%85%95%E8%AF%BE%E7%BD%91%E6%B4%BB%E5%8A%A8?from=501" >#慕课网活动#</a>【全民晒课ing,火速赢取IT图书】无论你是慕课网的“资深粉丝”,还是刚加入的新同学,在慕课网活动帖:<a href= "http://www.imooc.com/wenda/detail/30394" >http://www.php.cn/</a> 写下你在慕课网所学习的任意一门课程,并附上课程的学习心得,就有机会赢得精品IT好书。<img src= "http://img.mukewang.com/54447cc700010dbf00220022.gif" >这事很急哒,仅有2天呦。在<a href= "http://huati.weibo.com/k/%E7%A8%8B%E5%BA%8F%E5%91%98%E8%8A%82?from=501" >#程序员节#</a>送给自己一份礼物吧!<img src= "http://img.mukewang.com/54447af90001ab1c00010001.gif" > 93 <p><img src= "http://img.mukewang.com/54447aea0001f43301200084.jpg" ></p> 94 <p class = "info" > 95 <span class = "from" > 96 <a href= "#" >9月13日 10:27</a> 97 <em>来自</em><a href= "#" >微博 weibo.com</a> 98 </span> 99 <span class = "operate" >100 <a href= "javascript:" title= "赞" ><img src= "http://img.mukewang.com/54447c350001055b00120013.png" >(4)</a><em>|</em>101 <a href= "javascript:" >转发(5)</a><em>|</em>102 <a href= "javascript:" >收藏</a><em>|</em>103 <a href= "javascript:" >评论(2)</a>104 </span>105 </p>106 </p>107 </p>108 <p class = "test" >109 <p><input type= "radio" name= "layout" id= "float" checked><label for = "float" >float: left;</label></p>110 <p><input type= "radio" name= "layout" id= "absolute" ><label for = "absolute" >position: absolute;</label></p>111 </p>112 </p>113 <script>114 var eleAvatar = document.getElementById( "avatar" ),115 eleFloat = document.getElementById( "float" ),116 eleAbsolute = document.getElementById( "absolute" );117 118 // SVG路径以及相关动画119 var elePath = document.querySelector( "path" ), timerPath = null;
120 var funPathMove = function ( end ) {121 clearTimeout(timerPath);122 var d = elePath.getAttribute( "d" ), arrD = d.split( " " );123
|
Salin selepas log masuk
View Code
2、破坏性
1 | 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset= "utf-8" > 5 <meta name= "viewport" content= "width=device-width" > 6 <title>absolute的破坏性</title> 7 <style> 8 .box { 9 padding: 10px;10 background-color: #f0f0f0; 11 }12 input {13 position: absolute; top: 234px; 14 width: 160px; height: 32px;15 font-size: 100%;16 }17 </style>18 </head>19 20 <body>21 <p class = "box" ><img id= "image" src= "http://img.mukewang.com/54447b06000171a002560191.jpg" width= "256" height= "191" ></p>22 <input id= "button" type= "button" value= "图片absolute化" >23 <script>24 var eleImg = document.getElementById( "image" ), eleBtn = document.getElementById( "button" );25 if (eleImg != null && eleBtn != null) {26 eleBtn.onclick = function () {27 if (this.absolute) {28 eleImg.style.position = "" ;29 this.value = "图片absolute化" ;30 this.absolute = false;31 } else {32 eleImg.style.position = "absolute" ;33 this.value = "图片去absolute" ;34 this.absolute = true;35 }36 };37 }38 </script>39 </body>40 </html>
|
Salin selepas log masuk
View Code
3、包裹性
1 | 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset= "utf-8" > 5 <meta name= "viewport" content= "width=device-width" > 6 <title>absolute的包裹性</title> 7 <style> 8 .box { 9 padding: 10px;10 background-color: #f0f0f0; 11 }12 input {13 position: absolute; top: 234px; 14 width: 160px; height: 32px;15 font-size: 100%;16 }17 </style>18 </head>19 20 <body>21 <p id= "box" class = "box" ><img src= "http://img.mukewang.com/54447b06000171a002560191.jpg" width= "256" height= "191" ></p>22 <input id= "button" type= "button" value= "容器absolute化" >23 <script>24 var eleBox = document.getElementById( "box" ), eleBtn = document.getElementById( "button" );25 if (eleBox != null && eleBtn != null) {26 eleBtn.onclick = function () {27 if (this.absolute) {28 eleBox.style.position = "" ;29 this.value = "容器absolute化" ;30 this.absolute = false;31 } else {32 eleBox.style.position = "absolute" ;33 this.value = "容器去absolute" ;34 this.absolute = true;35 }36 };37 }38 </script>39 </body>40 </html>
|
Salin selepas log masuk
View Code
二、absolute的独立性
absolute使用的时候,不一定非要和relative在一起,可以独立使用,而且它越独立,能力越大

三、absolute特性
1、去浮动
1 | 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset= "utf-8" > 5 <title>折翼天使表现特性一:去浮动</title> 6 <style> 7 input[type=button] { 8 height: 32px; 9 font-size: 100%; 10 }11 </style>12 </head>13 14 <body>15 <img src= "http://img.mukewang.com/54447b06000171a002560191.jpg" >16 <img src= "http://img.mukewang.com/54447f4a0001eb7d01910256.jpg" >17 <img src= "http://img.mukewang.com/54447f550001ccb002560191.jpg" >18 <p><input type= "button" id= "float" value= "点击第2张图片应用float:left" ></p>19 <p><input type= "button" id= "button" value= "点击第2张图片应用position:absolute" ></p>20 <script>21 var flbtn = document.getElementById( "float" ),22 button = document.getElementById( "button" ),23 image2 = document.getElementsByTagName( "img" )[1];24 if (flbtn && button && image2) {25 var value_init = button.value;26 button.onclick = function () {27 if (this.value == value_init) {28 image2.style.position = "absolute" ;29 this.value = "撤销" ;30 } else {31 image2.style.position = "" ;32 this.value = value_init;33 }34 };35 36 flbtn.onclick = function () {37 image2.style[ "cssFloat" in this.style? "cssFloat" : "styleFloat" ] = "left" ;38 };39 }40 </script>41 </body>42 </html>
|
Salin selepas log masuk
View Code
2、位置跟随
1 2 | 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset= "utf-8" > 5 <title>折翼天使表现特性二:位置跟随</title> 6 <style> 7 input[type=button] { 8 height: 32px; 9 font-size: 100%;10 }11 p { margin-left: 260px; }12 img + p { margin-top: 60px; }13 </style>14 </head>15 16 <body>17 <img src= "http://img.mukewang.com/54447b06000171a002560191.jpg" >18 <p><img src= "http://img.mukewang.com/54447f4a0001eb7d01910256.jpg" ></p>19 <img src= "http://img.mukewang.com/54447f550001ccb002560191.jpg" >20 <p><input type= "button" id= "block" value= "点击第2张图片应用display:block" ></p>21 <p><input type= "button" id= "button" value= "点击第2张图片应用position:absolute变天使" ></p>22 <script>23 var block = document.getElementById( "block" ),24 button = document.getElementById( "button" ),25 image2 = document.getElementsByTagName( "img" )[1];26 if (block && button && image2) {27 var value_init_button = button.value;28 button.onclick = function () {29 if (this.value == value_init_button) {30 image2.style.position = "absolute" ;31 this.value = "撤销" ;32 } else {33 image2.style.position = "" ;34 this.value = value_init_button;35 }36 };37 38 var value_init_block = block.value;39 block.onclick = function () {40 if (this.value == value_init_block) {41 image2.style.display = "block" ;42 this.value = "撤销" ;43 } else {44 image2.style.display = "" ;45 this.value = value_init_block;46 }
47 };48 }49 </script>50 </body>51 </html>
|
Salin selepas log masuk
View Code
3.超越overflow
1 | 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset= "utf-8" > 5 <title>超越overflow</title> 6 <style> 7 body { 8 background-color: #bbb; 9 }10 .scroll {11 width: 500px; height: 300px;12 margin: 200px auto 0;13 margin-top: -webkit-calc(50vh - 150px);14 margin-top: calc(50vh - 150px);15 border: 1px solid #ccc;16 border-radius: 3px;17 box-shadow: 0 0 3px rgba(0,0,0,.35);18 background-color: #fff;19 overflow: auto;20 }21 .close {22 position: absolute;23 width: 34px; height: 34px; 24 margin: -17px 0 0 483px;25 background: url(http:
|
Salin selepas log masuk
View Code
滑动滚动条的时候,关闭按钮不移动,因为设置了absolute
四、absolute实际使用
1、图片图标绝对定位覆盖

上面图片里面的Hot、推荐、vip都是用absolute实现的,而且没有用relative
1 | 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset= "utf-8" > 5 <title>图标定位二三事</title> 6 <style> 7 body { font: 14px/1.4 "Microsoft YaHei" ; background-color: #EDEFF0; } 8 body, h3, h5 { margin: 0; } 9 img { border: 0 none; vertical-align: bottom; }10 .l { float: left; }.r { float: right; }11 .constr { width: 1200px; margin-left: auto; margin-right: auto; }12 .header { background-color: #2A2C2E; }13 .nav { height: 60px; }14 .nav-list { float: left; font-size: 14px; font-weight: 400; }15 .nav-a { display: inline-block; line-height: 20px; padding: 20px 35px; color: #B5BDC0; text-decoration: none; }16 .nav-a:hover { color: #fff; }17 18 .course { padding-top: 10px; }19 .course-list { float: left; width: 280px; height: 240px; margin: 5px 10px 15px; border-radius: 0 0 1px 1px; background-color: #F7FAF9; background-color: rgba(255,255,255,1); box-shadow: 0 1px 2px #c5c5c5; text-decoration: none; }20 .course-list-img { background-color: #6396F1; }21 .course-list-h { line-height: 50px; font-size: 14px; font-weight: 400; color: #363d40; text-align: center; }22 .course-list-tips { margin: 0 14px; font-size: 12px; color: #b4bbbf; overflow: hidden; }23 24 .icon-hot { position: absolute; width: 28px; height: 11px; margin: -6px 0 0 2px; background: url(http:
|
Salin selepas log masuk
View Code
2、下拉框定位

用的是无依赖绝对定位,就是只用了absolute,没有用relative
1 | 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset= "utf-8" > 5 <title>下拉框定位二三事</title> 6 <style> 7 body { margin: 0; font: 14px/1.4 "Microsoft YaHei" ; background-color: #EDEFF0; } 8 .constr { width: 1200px; max-width: 80%; margin-left: auto; margin-right: auto; padding-bottom: 300px; overflow: hidden; } 9 .course-sidebar { width: 262px; float: left; }10 .course-sidebar > p { border: 1px solid #e6e8e9; box-shadow: 0px 1px 2px #d5d7d8; background-color: #fff; }11 .course-sidebar-type { height: 380px; }12 .course-sidebar-search { margin-top: 20px; overflow: hidden; }13 .course-search-input { width: 200px; line-height: 18px; padding: 10px; margin: 0; border: 0 none; font-size: 12px; font-family: inherit; float: left; }14 .course-sidebar-search.focus { border-color: #2ea7e0; }15 .course-search-input:focus { outline: 0 none; }16 .course-search-input::-ms-clear { display: none; }17 .course-search-btn { width: 38px; height: 38px; float: right; background: url(http:
|
Salin selepas log masuk
View Code
3、居中、边缘定位

实现图片居中和边缘定位的一种思路,可以用无限制的absolute,里面用了 和absolute的跟随性,注意代码中的 ,这个很重要
1 | 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset= "utf-8" > 5 <title>居中、边缘定位二三事</title> 6 <style> 7 body { margin: 0; font: 14px/1.4 "Microsoft YaHei" ; background-color: #EDEFF0; } 8 .constr { width: 1200px; max-width: 80%; margin-left: auto; margin-right: auto; } 9 .course-content { float: right; position: relative; width: 920px; min-height: 1200px; background: #fff; }10 .course-list-x { padding: 20px 10px; overflow: hidden; }11 .course-list { float: left; width: 280px; height: 240px; margin: 5px 10px 15px; border-radius: 0 0 1px 1px; background-color: #F7FAF9; background-color: rgba(255,255,255,1); box-shadow: 0 1px 2px #c5c5c5; text-decoration: none; }12 13 .goto_top_diaocha, .goto_top_app, .goto_top_feed { display: block; width: 48px; height: 48px; margin-top: 10px; background: url(http:
|
Salin selepas log masuk
View Code
4、文件图片对其与定位

实际项目中,有的地方有星星,有的没有,想要对其文字比较麻烦,就可以用无依赖的absolute
邮箱前面的图标,以及超出框的文字,都可以用无依赖的absolute
它给页面的布局提供了一种新的思路
1 | 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset= "utf-8" > 5 <title>文本图标对齐与定位二三事</title> 6 <style> 7 body { margin: 0; font: 14px/1.4 "Microsoft YaHei" ; background-color: #EDEFF0; } 8 a { color: #50B6E5; } 9 .constr { width: 1200px; margin-left: auto; margin-right: auto; }10 .regist-head { height: 60px; line-height: 60px; padding-left: 30px; background-color: #be3948; color: #fff; font-size: 18px; }11 .regist-body { min-height: 400px; padding: 100px 0; background-color: #fff; }12 .regist-main { width: 600px; margin-left: auto; margin-right: auto; }13 .regist-group { margin-top: 20px; overflow: hidden; }14 .regist-label { width: 70px; padding-top: 10px; float: left; }15 .regist-cell { display: table-cell; *display: inline-block; }16 .regist-input { height: 18px; line-height: 18px; width: 260px; padding: 10px 5px; margin: 0 10px 0 0; border: 1px solid #d0d6d9; vertical-align: top; }17 .regist-code-input { width: 130px; }18 .regist-btn { display: inline-block; width: 160px; line-height: 40px; background-color: #39b94e; color: #fff; text-align: center; text-decoration: none; }19 .regist-btn:hover { background-color: #33a646; }20 .icon-warn { display: inline-block; width: 20px; height: 21px; background: url(http:
|
Salin selepas log masuk
View Code
四、absolute的脱离文档流
动画尽量作用在absolute上,因为absolute是脱离文档流的,这样动画的时候就不会影响其他元素
1 2 3 4 5 | z-index无依赖
1.如果只有一个绝对定位元素,自然不需要z-index,自动覆盖普通元素;
2.如果两个绝对定位,控制DOM流的前后顺序达到需要的覆盖效果,依然无z-index;
3.如果多个绝对定位交错,非常非常少见,z-index:1控制;
4.如果非弹窗类的绝对定位元素z-index>2,必定z-index冗余,请优化!
|
Salin selepas log masuk
更多css深入理解之absolute 相关文章请关注PHP中文网!