深入了解absolute
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前加一个 ,然后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的更多相关文章
- 5-5 可视化库Seaborn-多变量分析绘图
Parameters:¶ 参数 解释 变量 x,y,hue 数据集变量 变量名 date 数据集 数据集名 row,col 更多分类变量进行平铺显示 变量名 col_wrap 每行的最高平铺数 整 ...
- “fixed+relative==absolute”——对BFC的再次思考
好久没写博客了,刚好今天跨年夜没约到什么妹子,在家宅着不如写点东西好了. 需求 昨天晚上,给公司年会做一个移动端的投票页面,遇到一个UI优化的问题: · 正文内容少于一屏时,投票提交按钮固定显示在页面 ...
- Position属性四个值:static、fixed、relative、absolute的区别和用法
1.static(静态定位):默认值.没有定位,元素出现在正常的文档流中(如果设置 top, bottom, left, right, z-index这些属性就不起做作了). 2.relative(相 ...
- [LeetCode] Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- css使absolute相对于父容器进行定位而不是以body(为什么绝对定位(absolute)的父级元素必须是相对定位(relative))
借知乎的回答如下解释: 首先,我想告诉你的是,如果父级元素是绝对定位(absolute)或者没有设置,里面的绝对定位(absolute)自动以body定位.这句话是错的.正确的是:只要父级元素设了po ...
- Position属性四个值:static、fixed、absolute和relative的区别和用法
Position属性四个值:static.fixed.absolute和relative的区别和用法 在用CSS+DIV进行布局的时候,一直对position的四个属性值relative,absolu ...
- 解决绝对定位div position: absolute 后面的<a> Link不能点击
今天布局的时候,遇到一个bug,当DIV设置为绝对定位时,这个div后面的相对定位的层里面的<a>Link标签无法点击. 网上的解决方案是在绝对定位层里面添加:pointer-events ...
- ie a absolute bug
给a设置position:absolute时,在IE下,尽管display:block;width:100%;height:100%,依然无法点击.但是加一个背景颜色就可以了.如果不需要背景,再把背景 ...
- 辨析relative与absolute
谈起它们,想必大家都不陌生.relative,相对定位嘛:absolute,绝对定位嘛.但是它们到底是个啥东东呢? 看看w3c的定义,见下表 定位 含义 relative 元素框偏移某个距离.元素仍保 ...
- Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
随机推荐
- Robot Framework自动化测试(三)--- 封装系统关键字
之前对robotframework-ride了解的不多,后来知道了引入Selenium2Lirary库后可以做web UI自动化测试,但发现和python没啥关系,今天学习了封装系统关键字算是和pyt ...
- Yii2 富头像上传
前台html: <div class="xz_img2 text-center"> <div class="images_flow" id=& ...
- C#基础知识-对象初始化顺序
本文章转载:http://blog.csdn.net/forever_wind/article/details/7442503 不错的文章:http://www.cnblogs.com/McJerem ...
- 菜鸟学习-C语言函数参数传递详解-结构体与数组 分类: C/C++ Nginx 2015-07-14 10:24 89人阅读 评论(0) 收藏
C语言中结构体作为函数参数,有两种方式:传值和传址. 1.传值时结构体参数会被拷贝一份,在函数体内修改结构体参数成员的值实际上是修改调用参数的一个临时拷贝的成员的值,这不会影响到调用参数.在这种情况下 ...
- 对Jsp提交input标签空格和回车的处理
今天做增加的时候发现一个问题,在js中去掉空格的时候如果这么写 var stage_name = document.getElementById("stage_name").val ...
- javascript默认中文(汉字/标点)长度均为1的解决
javascript默认中文(汉字/标点)长度均为1 与后台(java)不一致, function calculate(str) { //var str="你好,哈哈哈000111lll&q ...
- 关于Bufferedreader的功能扩写
package cn.hncu.pattern.decorator.v1; import java.io.FileReader;import java.io.IOException; public c ...
- merge into sql优化
今天网友说他的merge into sql跑了15分钟了还没有跑出数据,问我能不能优化一下,我让他把sql和sql的执行计划发过来 merge into F_Sal_P_Camp_Samp_Cust_ ...
- 并发与多版本:update重启动问题
以下演示重启动问题,请注意 before触发器和after触发器的行为区别,因为before触发器会触发两次而导致重启动问题,因此使用after触发器更加高效,应该尽量避免在所有触发器中使用自治事务 ...
- c语言,strcspn,在串中查找第一个给定字符集内容的段
函数名: strcspn 功 能: 在串中查找第一个给定字符集内容的段 用 法: int strcspn(char *str1, char *str2); 程序例: #include <stdi ...
vip已经absolute后,保证vip前面的元素是img