深入了解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 ...
随机推荐
- dedecms 常用标签
都是常用的一些标签,大家可以用ctrl+F实现搜索. 网站名称:{dede:global.cfg_webname/} 网站根网址:{dede:global.cfg_basehost/} 网站根目录:{ ...
- 删除已分配IP的静态IP地址池
如果静态IP地址池已经分配了IP,则无法直接将其静态IP地址池删除,会提示出错:“已经有IP被分配,需要先将其回收,再删除” 如下: 查看IP地址池: Get-SCStaticIPAddressPoo ...
- logback使用
须要的jar包: slf4j-api-1.7.7.jar logback-classic-1.1.2.jar logback-core-1.1.2.jar logback.xml配置文件,放在proj ...
- wechat客户端修改
1. src/ui/adapter/FriendCardAdapter.java @Override public View getView(int position, View conver ...
- 分享功能使用的UIPopoverController在iOS9 过期,替换为popoverPresentationController
记录一下 以备以后用到的时候拿出来看看.以前使用的: 1 if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom ...
- Java开发之I/O读取文件实例详解
在java开发或者android开发中,读取文件是不可避免的,以下对java开发中读取文件做了归纳和详解: 1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 pa ...
- Java基础知识强化之IO流笔记44:IO流练习之 复制图片的 4 种方式案例
1. 复制图片的 4 种方式案例: 分析: 复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流. 通过该原理,我们知道我们应该采用字节流. 而字节流有4种方式,所以做这个题目我们有 ...
- ModelSim之命令行仿真入门
下面是我们的Tcl仿真步骤:启动ModelSim SE, 首先看到在在ModelSim SE右边的窗口有ModelSim> 这样的提示符.在提示符后,顺序运行以下命令: vlib work ...
- Css3炫酷总结使用
先从CSS3的选择器说起: E F:所有的子孙元素: E>F: E中的子元素: E+F:E元素之后的最近的选择器: E~F:E中所以后面兄弟元素(CSS3 不包括自己本身,前面也不包括) att ...
- Unity3D 之UGUI 按钮
新建一个按钮 按钮对应的属性 按钮下面只有一个文本的 游戏对象,是Button对应的显示文字 Interactable -->是否可以交互 Transition -->变换,对应各种关于按 ...
vip已经absolute后,保证vip前面的元素是img