CSS position:absolute浅析
一、绝对定位的特征
绝对定位有着与浮动一样的特性,即包裹性和破坏性。
就破坏性而言,浮动仅仅破坏了元素的高度,保留了元素的宽度;而绝对定位的元素高度和宽度都没有了。
请看下面代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>绝对定位的特征</title>
<style>
.box1,.box2,.box3 {
background-color: orange;
margin-bottom: 30px;
} .absolute {
position: absolute;
} .wraper {
display:inline-block;
margin-left: 300px;
} .float {
float: left;
} .box3 {
position: absolute;
}
</style>
</head>
<body>
<div class="box1">
<img src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg" alt="A picture" style="width:175px;height:100px" />
<img src="http://pic1.win4000.com/wallpaper/c/537b28b60619b.jpg" alt="A picture" style="width:240px;height:180px" />
<p>这是普通流中的两幅图像。</p>
</div>
<div class="box2">
<img class="absolute" src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg" alt="A picture" style="width:175px;height:100px" />
<img src="http://pic1.win4000.com/wallpaper/c/537b28b60619b.jpg" alt="A picture" style="width:240px;height:180px" /> <div class="wraper">
<img class="float" src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg" alt="A picture" style="width:175px;height:100px" />
<img src="http://pic1.win4000.com/wallpaper/c/537b28b60619b.jpg" alt="A picture" style="width:240px;height:180px" />
</div>
<p>左图,将第一幅图像绝对定位,其完全脱离文档流,并且覆盖在第二幅图像之上;由此看出,绝对定位的破坏性不仅让元素没有了高度,甚至没有了宽度。</p>
<p>右图,将第一幅图像左浮动,其虽然脱离了文档流,但是并没有覆盖在其他元素之上;浮动的破坏性仅仅破坏了元素的高度,而保留了元素的宽度。</p>
</div>
<div class="box3">
<img src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg" alt="A picture" style="width:175px;height:100px" />
<img src="http://pic1.win4000.com/wallpaper/c/537b28b60619b.jpg" alt="A picture" style="width:240px;height:180px" />
<p>将容器绝对定位,体现其包裹性。</p>
</div>
</body>
</html>
二、绝对定位的一般规则:
元素绝对定位时,会完全脱离文档流,并相对于其包含块定位。
绝对定位的包含块,是其最近的已定位的祖先元素。
如果这个祖先元素是块级元素,包含块则为该祖先元素的内边距边界,即边框。
如果这个祖先元素是行内元素,包含块则为该祖先元素的内容边界,即内容区。
如果没有已定位的祖先元素,元素的包含块定义为初始包含块。
偏移属性:top、right、bottom、left。
如果绝对定位的元素没有设置偏移属性,虽然脱离文档流,但是它的位置是原地不动的。
偏移属性可以为负值,将元素定位到包含块之外。
代码在这里:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>绝对定位的一般规则</title>
<style>
body {
background-color: #ccc;
}
.container {
width:500px;
background-color: orange;
margin:20px auto 50px auto;
padding:20px;
border:2px solid red;
} .box2 img,.box3 img,
.box4 img,.box5 img {
position: absolute;
} .box3 img,.box4 img {
left:0;
bottom:0;
} .box5 img {
left: -30px;
bottom: -50px;
} .block {
position :relative;
height: 200px;
}
</style>
</head>
<body>
<div class="container">
<div class="box1">
<p>元素绝对定位时,会完全脱离文档流,并相对于其包含块定位。绝对定位的包含块,是其最近的已定位的祖先元素。</p>
<img src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg" alt="A picture" style="width:175px;height:100px" />
<ul>
<li>如果这个祖先元素是块级元素,包含块则为该祖先元素的内边距边界,即边框。</li>
<li>如果这个祖先元素是行内元素,包含块则为该祖先元素的内容边界,即内容区。</li>
<li>如果没有已定位的祖先元素,元素的包含块定义为初始包含块(一个视窗大小的矩形)。</li>
</ul>
</div><!--关闭box1-->
</div><!--关闭container-->
<div class="container">
<div class="box2">
<p>不管有没有已经定位的祖先元素,只要没有偏移量,绝对定位之后,原地不动,脱离文档流。</p>
<p>下面这个已经绝对定位的图像原地没动,但是已经脱离了文档流。</p>
<img src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg" alt="A picture" style="width:175px;height:100px" />
</div><!--关闭box2-->
</div><!--关闭container-->
<div class="container">
<div class="box3">
<p>没有已经定位的祖先元素,有偏移量,绝对定位之后,以初始包含块(一个视窗大小的矩形)为基准进行偏移。</p>
<p>当偏移量为left:0; buttom:0时,图像水平偏移到了初始包含块左下角(打开网页最开始的那一个视窗的左下角)。</p>
<img src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg" alt="A picture" style="width:175px;height:100px" />
</div><!--关闭box3-->
</div><!--关闭container-->
<div class="container block">
<div class="box4">
<p>有已经定位的祖先元素,有偏移量,绝对定位之后,以已经定位的祖先元素为基准进行偏移。</p>
<p>此处已经定位的祖先元素为这个图像的容器div元素,偏移量为left:0; bottom:0时,图像到了这个容器的左下角(以边框为界)。</p>
<img src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg" alt="A picture" style="width:175px;height:100px" />
</div><!--关闭box4-->
</div><!--关闭container-->
<div class="container block">
<div class="box5">
<p>有已经定位的祖先元素,有偏移量,绝对定位之后,以已经定位的祖先元素为基准进行偏移。</p>
<p>此处已经定位的祖先元素为这个图像的容器div元素,偏移量为left:-30px; bottom:-50px时,图像到了这个容器之外(以边框为界)。</p>
<img src="http://a.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fa38bfc55a964034f79f019ec.jpg" alt="A picture" style="width:175px;height:100px" />
</div><!--关闭box5-->
</div><!--关闭container-->
</body>
</html>
三、用margin调整绝对定位元素的位置
绝对定位的元素,除了可以使用top、right、bottom、left进行偏移之外,还能够通过margin值进行精确定位,而且自适应性更好。
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用margin调整绝对定位元素的位置</title>
<style>
.box1,.box2,.box3 {
display: inline-block;
margin-right: 150px;
border:1px solid blue;
} span {
background-color: red;
} .box2 span,.box3 span {
position: absolute;
} .meng {
margin-left: -3em;
} .box4 {
border:1px solid red;
width: 500px;
margin: 50px auto 0 auto;
padding:20px;
} li {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="box1">
<span>卡哇伊</span>
<img src="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg" style="width:200px;height:300px" />
<span>萌萌哒</span>
</div>
<div class="box2">
<span>卡哇伊</span>
<img src="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg" style="width:200px;height:300px" />
<span>萌萌哒</span>
</div>
<div class="box3">
<span>卡哇伊</span>
<img src="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg" style="width:200px;height:300px" />
<span class="meng">萌萌哒</span>
</div>
<div class="box4">
<ol>
<li>第一幅图,最开始的样子。</li>
<li>第二幅图,两个标签绝对定位,但是不指定任何偏移量。</li>
<li>第三幅图,用margin负值调整“萌萌哒”的位置,完成。</li>
</ol>
</div>
</body>
</html>
放弃偏移属性而改用margin来调整绝对定位元素,其原理在于:
绝对定位的元素,在不设置偏移量的时候,它虽然完全脱离了文档流,但它还在原来的位置。
利用偏移属性进行精确定位,其具体位置是取决于绝对定位元素的包含块,不同的包含块将会有完全不一样的定位结果。
而利用margin进行精确定位,不依赖于任何其他东西,更加可控。
在写这篇博文的时候,absolute让我给relative带话,它说:“relative,你给我滚好吗,我这辈子都不想看到你!”
预知后事如何,请移步absolute与relative不得不说的故事!
四、绝对定位与整体布局
如何用绝对定位来对页面进行整体布局?
简单粗暴,不学就浪费啦!!!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>绝对定位与整体页面布局</title>
<style>
* {
margin: 0;
}/*重置所有margin为0,这一步极其重要,否则布局必乱。*/ html,body,.page {
width: 100%;
height: 100%;
overflow: hidden;
} .page {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: #ccc;
} .header {
position: absolute;
height: 50px;
left: 0;
right: 0;
background-color: purple;
padding: 0 5px;
z-index: 1;
} .header>h1 {
line-height: 50px;
text-align: center;
} .aside {
position: absolute;
top: 50px;
bottom: 50px;
left: 0;
width: 100px;
background-color: orange;
} .footer {
position: absolute;
right: 0;
bottom: 0;
left: 0;
height: 50px;
background-color: purple;
} .footer>h1 {
text-align: center;
line-height: 50px;
} .content {
position: absolute;
top: 50px;
right: 0;
bottom: 50px;
left: 100px;
background-color: cyan;
overflow: auto;
} .content h1 {
margin-top: 100px;
margin-left: 50px;
} .content li {
margin-left: 100px;
margin-top: 80px;
font-size: 24px;
line-height: 1.5;
} ol {
margin-bottom: 80px;
}
</style>
</head>
<body>
<div class="page">
<div class="header">
<h1>Header</h1>
</div>
<div class="aside">
<h1>Aside</h1>
</div>
<div class="content">
<h1>实现原理</h1>
<ol>
<li>创建一个div.page,绝对定位,铺满全屏。</li>
<li>创建一个div.header,绝对定位,设定高度。</li>
<li>创建一个div.aside,绝对定位,设定宽度。</li>
<li>创建一个div.footer,绝对定位,设定高度。</li>
<li>创建一个div.content,绝对定位,根据周围div的宽高设定它的宽高;<br />
以div.content元素取代原body元素,所有的页面内容都放在这里面。</li>
</ol>
</div>
<div class="footer">
<h1>Footer</h1>
</div>
</div>
</body>
</html>
CSS position:absolute浅析的更多相关文章
- css position: absolute、relative详解
CSS2.0 HandBook上的解释: 设置此属性值为 absolute 会将对象拖离出正常的文档流绝对定位而不考虑它周围内容的布局.假如其他具有不同 z-index 属性的对象已经占据了给定的位置 ...
- css position absolute 浮动特性
absolute的元素不会占据未浮动的元素的空间<html> <head> <style type="text/css"> .flipbox{ ...
- css position absolute相对于父元素的设置方式
手机赚钱怎么赚,给大家推荐一个手机赚钱APP汇总平台:手指乐(http://www.szhile.com/),辛苦搬砖之余用闲余时间动动手指,就可以日赚数百元 大家知道css的position abs ...
- css position:absolute 如何居中对齐
写死宽度,就好弄了 position: absolute;left: 50%;width: 980px;margin-left: -490px; text-algin:center
- CSS position: absolute、relative定位问题详解
CSS2.0 HandBook上的解释: 设置此属性值为 absolute 会将对象拖离出正常的文档流绝对定位,而不考虑它周围内容的布局.假如其他具有不同 z-index 属性的对象已经占据了给定的 ...
- css position:absolute align center bottom
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- CSS position绝对定位absolute relative
常常使用position用于层的绝对定位,比如我们让一个层位于一个层内具体什么位置,为即可使用position:absolute和position:relative实现. 一.position语法与结 ...
- CSS position relative absolute fixed
position属性absolute与relative 详解 最近一直在研究javascript脚本,熟悉DOM中CSS样式的各种定位属性,以前对这个属性不太了解,从网上找到两篇文章感觉讲得很透彻 ...
- CSS绝对定位和相对定位 position: absolute/relative
absolute(绝对定位): 会把对象拖离HTML文档流,并通过top, left, right, bottom确定对象的具体位置,这个四个位置属性至少要设置一个,否则无法激活对象的absolute ...
随机推荐
- 课堂讨论 alpha版最后总结
议时间:组队开发最后总结会议 星期一 下午4:30-5:30 会议地点:学院楼自习室 到会人员:唐野野 胡潘华 王永伟 魏孟 会议概要: 1.展示最后开发成果: 2.交流开发过程心得体会: 会 ...
- 将搬家至CSDN
emmm,感觉没利用好博客,自己也弄了一个github上面的hexo博客https://clarkkun.github.io/,但是死活传不上去内容,尴尬 ̄□ ̄||,三个博客齐头并进吧
- C++:钻石继承与虚继承
QUESTION:什么是钻石继承? ANSWER:假设我们已经有了两个类Father1和Father2,他们都是类GrandFather的子类.现在又有一个新类Son,这个新类通过多继承机制对类Fat ...
- Visual Studio 2015的安装和简单的测试
首先是Visual Studio 2015的安装 Visual Studio是微软开发的一套基于组件的软件开发工具,目前最新的版本是2015. 在 I Tell you 网站下载Visual Stud ...
- Adobe X沙箱
一.Adobe X沙箱简介 Adobe Reader X自从引入沙箱以来,对其攻击的难度就提高了很多.Reader X的沙箱是基于Google的Chrome沙箱,Chrome是开源的,Reader X ...
- 关于PSP(个人软件过程)
在第一堂课时,杨老师就提到了PSP(个人软件过程),但是我从2016年3月10日才开始进行粗略的PSP时间管理统计,这是长迭代,用老师的话“差评”.然后在2016年3月11日下午的软件项目管理上,老师 ...
- ionic npm安装报错 no such file ,解决办法
Install the latest version of NodeJS from their website (e.g. 6.X.X). Open the Node.js command promp ...
- phpStudy-坑爹的数据库管理器-phpMyAdmin的默认用户名和密码
在这里我必须承认自己的弱智,第一次使用phpMyAdmin竟然搞了10分钟才进去!!! 要使用默认的用户名和密码: 用户名:root 密码:root 尼玛!坑爹啊!不说清楚让我百度了半天!!!!
- [COGS 2551] 新型武器
图片加载可能有点慢,请跳过题面先看题解,谢谢 这个题好多解法啊... 可以主席树,可以按深度将操作排序离线做 我这里是动态开点线段树,对每一个深度种一棵线段树,下标是节点的\(dfs\)序 然后这个做 ...
- BZOJ 3624: [Apio2008]免费道路
3624: [Apio2008]免费道路 Time Limit: 2 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1201 Solved: ...