一、绝对定位的特征
绝对定位有着与浮动一样的特性,即包裹性和破坏性。
就破坏性而言,浮动仅仅破坏了元素的高度,保留了元素的宽度;而绝对定位的元素高度和宽度都没有了。
请看下面代码:
XML/HTML Code复制内容到剪贴板
- <!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。
如果绝对定位的元素没有设置偏移属性,虽然脱离文档流,但是它的位置是原地不动的。
偏移属性可以为负值,将元素定位到包含块之外。
代码在这里:
XML/HTML Code复制内容到剪贴板
- <!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>
- </div>
- <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>
- </div>
- <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>
- </div>
- <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>
- </div>
- <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>
- </div>
- </body>
- </html>
三、用margin调整绝对定位元素的位置
绝对定位的元素,除了可以使用top、right、bottom、left进行偏移之外,还能够通过margin值进行精确定位,而且自适应性更好。
示例:
XML/HTML Code复制内容到剪贴板
- <!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进行精确定位,不依赖于任何其他东西,更加可控。
四、绝对定位与整体布局
如何用绝对定位来对页面进行整体布局?
简单粗暴,不学就浪费啦!!!
XML/HTML Code复制内容到剪贴板
- <!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 { &nbs
Opera中国的WEB标准课程
网页制作Webjx文章简介:在这篇文章里,我要向大家介绍我和其他很多人花费数月时间开发的一个课程——Web标准课程,该课程旨在向大家提供Web设计和开发的坚实基础,无论读者是谁,此教程完全免费、可访问,并且不需要预备知识。当然,我主要还
收藏 0赞 0分享
CSS样式表渐进增强的基本概念
网页制作Webjx文章简介:如果你挠着头想弄清楚优雅降级和渐进增强的区别,我告诉你,这是视角问题。优雅降级和渐进增强都考虑网站在各种设备的各种浏览器上如何良好运转。两者区别的关键在于它们各自关注的焦点,以及这种关注对工作流程的影响
收藏 0赞 0分享
简单介绍Web Developer插件制作网页
网页制作Webjx文章简介:Firefox浏览器是一个良好支持W3C标准的开放源代码的浏览器,拥有Linux/Windows/Mac版本。因为Firefox浏览器良好支持W3C标准,所以使用Firefox来调试网页是非常好的。
Firefox浏览器是
收藏 0赞 0分享
CSS布局带来的巨大影响:CSS display属性值
网页制作Webjx文章简介:网页元素应用上那些与表格相关的display属性值后,能够模仿出与表格相同的特性。我将会在该文中给大家演示这种方法给CSS布局带来的巨大影响。
应原书编辑要求,先在文章顶部给出链接:《Everything You
收藏 0赞 0分享
用div css模拟表格对角线
这只是探讨一种CSS模拟表格对角线的用法,实际在工作中可能觉得这样做有点小题大作,这不是本主题讨论的重点。如果对此深以为然的朋友,请一笑过之
首先声明:
这只是探讨一种CSS模拟表格对角线的
收藏 0赞 0分享
IE Firefox在css中的差别 (部分)
1、单位问题
问题:任何距离的数值ie可以不加单位,ff必须要求写单位(0除外)
解决:写全单位如padding:0px;
2、水平居中
问题:div里的内容,ie默认为center,而ff默认left
解决:mairgin:0px auto;
3、高度问题
收藏 0赞 0分享
不用js可以实现信息提示效果
[code]
<style>
body { font:verdena; font-size:14px; color:#000 }
h1{ font:verdena; font-size:22px; color:#000 }
h2{ font:verdena;
收藏 0赞 0分享
CSS解决未知高度的垂直水平居中自适应问题
今天有人问起,晚上试着写出来,供参考; 以下代码兼容主流浏览器IE6、IE7、Firefox、Opera。
从最简单的开始…………
一、如何让一个DIV水平居中?
这个简单不作过多说明!
[code]
<st
收藏 0赞 0分享
CSS cursor 属性 -- 鼠标指针样式效果
取值: [ [<uri> ,]* [ auto | crosshair | default | pointer | move | e-resize | ne-resize | nw-resize | n-resize | se-resize | sw-resize |
收藏 0赞 0分享
css 简单区别ie6,ie7,firefox的写法
同一样式里可以这样
[code]
margin:17px; FF
+margin:17px; IE6 IE7
_margin:17px; IE6
[/code]
按这个顺序,刚好区分开三个浏览器
收藏 0赞 0分享
查看更多