1.absolute与float的相同的特性表现
  a.包裹性
  b.破坏性:父元素没有设置高或宽,父元素的高或宽取决于这个元素的内容
  c.不能同时存在

2.absolute独立使用,不与relative合用
   * 超越overflow,无论是滚动还是隐藏

案例:

html

<div class="scroll">
<a href="javascript:;" class="close" title="关闭"></a>
<img src="1.jpg">
<img src="2.jpg">
</div>

css

.scroll {
width: 500px; height: 300px;
margin: 200px auto 0;
border: 1px solid #ccc;
border-radius: 3px;
box-shadow: 0 0 3px rgba(0,0,0,.35);
background-color: #fff;
overflow: auto;
}
.close {
position: absolute;
width: 34px; height: 34px;
margin: -17px 0 0 483px;
background: url(images/close.png) no-repeat;
}
img {
display: block;
margin: 10px;
}

* 将元素设置absolute,然后调整margin,如果使用top和left,其父元素还得relative

独立使用absolute的跟性特性,它会脱离文档流,并且跟在前面元素或文本的后面,也就是它原本应该存在的位置

案例1:

html

<div class="header">
<div class="constr">
<div class="nav">
<h3 class="nav-list">
<a href="#" class="nav-a">课程</a>
</h3>
<h3 class="nav-list">
<a href="#" class="nav-a">问答</a>
</h3>
<h3 class="nav-list">
<a href="#" class="nav-a">
求课<i class="icon-hot"></i>
</a>
</h3>
</div>
</div>
</div>

css

.constr { width: 1200px; margin-left: auto; margin-right: auto; }
.header { background-color: #2A2C2E; }
.nav { height: 60px; }
.nav-list { float: left; font-size: 14px; font-weight: 400; }
.nav-a { display: inline-block; line-height: 20px; padding: 20px 35px; color: #B5BDC0; text-decoration: none; }
.nav-a:hover { color: #fff; }
img { border: 0 none; vertical-align: bottom;/*img为行内元素,去底部留白*/ }
/*position单独使用,看效果*/
.icon-hot { position: absolute; width: 28px; height: 11px; margin: -6px 0 0 2px; background: url(images/new.gif); }

案例2:

  推荐与vip这两个小图标怎么布局

常见方式:给外层容器设置relative,然后给这两个图片absolute,然后分别left:0与right:0

而我利用独立使用absolute实现方式是这样的

html

<a href="#" class="course-list">
<div class="course-list-img">
<span class="icon-recom">推荐</span>
<img src="data:images/4.png"><i class="vip">vip</i>
</div>
</a> 或者 <a href="#" class="course-list">
<div class="course-list-img">
<span class="icon-recom">推荐</span>
<img src="data:images/4.png"><!--
--><i class="vip">vip</i>
</div>
</a> 为什么要在img后面增加<!-- -->,因为换行是文本节点,absolute单独使用,为了使她脱离文档流,保持在于原本的应该存在的位置,效果如下
(或者将course-list-img设置为font-size:0;)
  vip已经absolute后,保证vip前面的元素是img

css

<style>
.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; } .course-list-img { background-color: #6396F1; } .icon-recom { position: absolute; line-height: 20px; padding: 0 5px; background-color: #f60; color: #fff; font-size: 12px; } .icon-vip { position: absolute; width: 36px; height: 36px; margin-left: -36px; background: url(images/vip.gif); text-indent: -9em; overflow: hidden; }
</style>

经典应用:高逼对齐思路

初学前端时,实现对齐,各种蒙逼,现在好了,自从有了它,小车开得呜呜呜

3.无依赖
   要想不受relative限制的印象,不要使用top/bottom/left/right任何一个属性,或不使用使用auto作为值
   表现: 脱离文档流、去浮动,跟随性

* IE7下用position:absolute;时,元素会成为inline-block,在外层套div解决这个bug

4.层级关系
后来者居上(别z-index了),弹窗类设置为2就好了,除特殊情况(交叉显示),否则z-inde冗余

5.absolute与width和height
a.可以相互替代(IE6不支持)
*top/bottom 和 left/right对立属性同时存在时,拉伸 - 不设置宽度或高度,就可以自动增高或增宽
例如要实现一个全屏自适应的50%黑色半透明遮罩层
html,body{height:100%;}
* 通常方案: overlay{position:absolute;width:100%;height:100%;left:0;top:0;}
* NB方案:overlay{position:absolute;left:0;top:0;right:0;bottom:0;margin:auto}

b.相互支持性
* 容器拉伸,内部元素支持百分比width/height值
* 通常情况下
元素百分比height要想起作用,需要父级容器的height值不是auto
* 绝对定位拉伸下
即使父级容器的height值是auto,只要容器绝对定位拉伸形成,百分比高度值也是支持的。
例如实现:9宫格
.page{
position:absolute;
left:0;right:0;
top:0;bottom:0;
}
.page .list{
float:left;
height:33.3%;
width:33.3%;
}

c.相互合作性
如果拉伸和width/height同时存在
width/height设置的尺寸 覆盖left/right/top/bottom拉伸的尺寸
例如: position:absolute;left:0;right:0;top:0;bottom:0;width:50%;
最后的尺寸为50%;
* 绝对居中特性:
当尺寸设置、拉伸以及margin:auto,同时存在,就有绝对定位元素的绝对居中效果
* 注意:支持IE8+本
* 分析:
.demo{width:400px;height:400px;background:#ccc;margin:0 auto;}可以实现居中
但是如果.demo是绝对定位的,已经脱离了文档流,margin:0 auto;不好使了
.demo{position:absolute;width:400px;height:400px;background:#ccc;margin:0 auto;}

这时要想居中有三种方式:
①设置其父容器relative,然后left:50%,margin-left:-demo的宽度
②在demo前加一个&nbsp,然后demo设置inline-block,最后给你父元素text-align:center;
③使其左右拉伸,就可以居中了
.demo{position:absolute;left:0;right:0;width:400px;height:400px;background:#ccc;margin:0 auto;}

其中:①适用于所有浏览器
②要想使用于所有浏览器,需要补丁*display:inline;*zoom:1;
③支持IE8+版本

深入了解absolute的更多相关文章

  1. 5-5 可视化库Seaborn-多变量分析绘图

      Parameters:¶ 参数 解释 变量 x,y,hue 数据集变量 变量名 date 数据集 数据集名 row,col 更多分类变量进行平铺显示 变量名 col_wrap 每行的最高平铺数 整 ...

  2. “fixed+relative==absolute”——对BFC的再次思考

    好久没写博客了,刚好今天跨年夜没约到什么妹子,在家宅着不如写点东西好了. 需求 昨天晚上,给公司年会做一个移动端的投票页面,遇到一个UI优化的问题: · 正文内容少于一屏时,投票提交按钮固定显示在页面 ...

  3. Position属性四个值:static、fixed、relative、absolute的区别和用法

    1.static(静态定位):默认值.没有定位,元素出现在正常的文档流中(如果设置 top, bottom, left, right, z-index这些属性就不起做作了). 2.relative(相 ...

  4. [LeetCode] Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  5. css使absolute相对于父容器进行定位而不是以body(为什么绝对定位(absolute)的父级元素必须是相对定位(relative))

    借知乎的回答如下解释: 首先,我想告诉你的是,如果父级元素是绝对定位(absolute)或者没有设置,里面的绝对定位(absolute)自动以body定位.这句话是错的.正确的是:只要父级元素设了po ...

  6. Position属性四个值:static、fixed、absolute和relative的区别和用法

    Position属性四个值:static.fixed.absolute和relative的区别和用法 在用CSS+DIV进行布局的时候,一直对position的四个属性值relative,absolu ...

  7. 解决绝对定位div position: absolute 后面的<a> Link不能点击

    今天布局的时候,遇到一个bug,当DIV设置为绝对定位时,这个div后面的相对定位的层里面的<a>Link标签无法点击. 网上的解决方案是在绝对定位层里面添加:pointer-events ...

  8. ie a absolute bug

    给a设置position:absolute时,在IE下,尽管display:block;width:100%;height:100%,依然无法点击.但是加一个背景颜色就可以了.如果不需要背景,再把背景 ...

  9. 辨析relative与absolute

    谈起它们,想必大家都不陌生.relative,相对定位嘛:absolute,绝对定位嘛.但是它们到底是个啥东东呢? 看看w3c的定义,见下表 定位 含义 relative 元素框偏移某个距离.元素仍保 ...

  10. Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

随机推荐

  1. NSAutoreleasePool' is unavailable: not avail

    NSAutoreleasePool' is unavailable: not available in automatic reference counting mode 这我就纠结了,对着书敲都出问 ...

  2. SecureCRT 终端仿真程序 v7.0.0.326 中文绿色便携破解版

    http://wd.jb51.net:81/201205/tools/SecureCRT_jb51.rar Secure CRT是一款支持 SSH2.SSH1.Telnet.Telnet/SSH.Re ...

  3. 【转载】ShowWindow函数

    ShowWindow的API函数是显示窗体,但它在第一次调用和以后的调用是有差别的.第一次调用时,它的输入參数nCmdShow是须要输入WinMain函数里传入来的nCmdShow參数,而不能是其他參 ...

  4. 判断2D平面内某点是否在某个旋转的矩形(OBB)内的算法

    http://stackoverflow.com/questions/7328424/point-in-obb-oriented-bounding-box-algorithm Given a cent ...

  5. listview图片错位

    借鉴这两篇文章,绝对解决,亲测有效! 国外coder的(需要FQ才能看) http://negativeprobability.blogspot.com/2011/08/lazy-loading-of ...

  6. 【转】cocos2d-x获取系统时间——2013-08-25 10

    欢迎转载,本帖地址:http://blog.csdn.net/jinjian2009/article/details/9449585 之前使用过cocos2d-x获取系统时间,毫秒级的 long ge ...

  7. Linux--------------安装mysql(2)

    在 CentOS7 上安装 MySQL5.7 1 通过 SecureCRT 连接到阿里云 CentOS7 服务器: 2 进入到目录 /usr/local/ 中:cd /usr/local/ 3 创建目 ...

  8. Java并发——ReentrantLock类源码阅读

    ReentrantLock内部由Sync类实例实现. Sync类定义于ReentrantLock内部. Sync继承于AbstractQueuedSynchronizer. AbstractQueue ...

  9. poj 1007 DNA Sorting

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95437   Accepted: 38399 Des ...

  10. iframe父子窗口取值

    父窗口中操作iframe:window.frames["iframeChild"].document //假如iframe的id为iframeChild 在子窗口中操作父窗口:wi ...