css之absolute

时间:2023-03-09 23:19:22
css之absolute

一、absolute和float有相同的特性,包裹性和破坏性

1、absolute和float的相似(看下面的demo,如果图片在左上角,那么用float和absolute都一样)

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>absolute和float高尔基</title>
<style>
body {
background-color: #B0DEF5;
font-size: 14px; font-family: arial;
color: #444;
}
a {
color: #0078b6;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
em {
font-style: normal;
color: #aeaeae;
margin: 0 5px;
}
svg {
position: absolute;
}
.container {
width: 560px; height: -webkit-calc(100vh - 16px); height: calc(100vh - 16px);
margin-left: auto; margin-right: auto;
background-color: #fff;
}
.list {
padding: 20px;
position: relative;
}
.space {
padding-left: 60px;
line-height: 22px;
}
.space img {
vertical-align: text-bottom;
}
.info {
font-size: 12px;
overflow: hidden;
color: #808080;
}
.from {
float: left;
}
.from a {
color: #9abbc8;
}
p {
margin: 6px 0;
}
.operate {
float: right;
}
.operate img {
vertical-align: text-bottom;
margin-right: 3px;
}
.test {
position: relative;
font-size: 18px; font-family: 'microsoft yahei';
}
.test p {
width: 200px;
margin-left: auto; margin-right: auto;
padding: 5px;
background-color: #f0f0f0;
}
.test input {
width: 20px; height: 20px;
vertical-align: -4px;
}
</style>
</head> <body>
<div class="container">
<svg xmlns="http://www.w3.org/2000/svg" width="520" height="360">
<path d="M45 80C45 280,45 320,165 300" stroke-width="1" stroke="#444" fill="none"></path>
</svg>
<div class="list">
<a href="#" id="avatar" class="avatar" style="float: left;"><img src="http://img.mukewang.com/54447ac70001f5cc00500050.jpg"></a>
<div class="space">
<a href="//weibo.com/mukewang" target="_blank" title="慕课网">慕课网</a>
<img src="http://img.mukewang.com/54447b1a0001750000160013.png">
<a href="http://huati.weibo.com/k/%E6%85%95%E8%AF%BE%E7%BD%91%E6%B4%BB%E5%8A%A8?from=501">#慕课网活动#</a>【全民晒课ing,火速赢取IT图书】无论你是慕课网的“资深粉丝”,还是刚加入的新同学,在慕课网活动帖:<a href="http://www.imooc.com/wenda/detail/30394">http://www.imooc.com/wenda/detail/30394</a> 写下你在慕课网所学习的任意一门课程,并附上课程的学习心得,就有机会赢得精品IT好书。<img src="http://img.mukewang.com/54447cc700010dbf00220022.gif">这事很急哒,仅有2天呦。在<a href="http://huati.weibo.com/k/%E7%A8%8B%E5%BA%8F%E5%91%98%E8%8A%82?from=501">#程序员节#</a>送给自己一份礼物吧!<img src="http://img.mukewang.com/54447af90001ab1c00010001.gif">
<p><img src="http://img.mukewang.com/54447aea0001f43301200084.jpg"></p>
<div class="info">
<span class="from">
<a href="#">9月13日 10:27</a>
<em>来自</em><a href="#">微博 weibo.com</a>
</span>
<span class="operate">
<a href="javascript:" title="赞"><img src="http://img.mukewang.com/54447c350001055b00120013.png">(4)</a><em>|</em>
<a href="javascript:">转发(5)</a><em>|</em>
<a href="javascript:">收藏</a><em>|</em>
<a href="javascript:">评论(2)</a>
</span>
</div>
</div>
</div>
<div class="test">
<p><input type="radio" name="layout" id="float" checked><label for="float">float: left;</label></p>
<p><input type="radio" name="layout" id="absolute"><label for="absolute">position: absolute;</label></p>
</div>
</div>
<script>
var eleAvatar = document.getElementById("avatar"),
eleFloat = document.getElementById("float"),
eleAbsolute = document.getElementById("absolute"); // SVG路径以及相关动画
var elePath = document.querySelector("path"), timerPath = null;
var funPathMove = function(end) {
clearTimeout(timerPath);
var d = elePath.getAttribute("d"), arrD = d.split(" ");
// 动画起始值
var start = arrD.slice(-1) * 1;
if (start == end) return;
// 速率
var rate = 5, current = start;
console.log(arrD);
var step = function() {
if (Math.abs(end - current) < 1) {
current = end;
} else {
current += (end - current) / 5;
timerPath = setTimeout(step, 30);
}
// 替换最后一个值
arrD.splice(-1, 1, current);
// 曲线走起
elePath.setAttribute("d", arrD.join(" "));
};
step();
}; if (eleAvatar && eleFloat && eleAbsolute) {
var attrFloat = eleAvatar.style.cssFloat != undefined? "cssFloat": "styleFloat";
eleFloat.onclick = function() {
eleAvatar.style.position = "";
eleAvatar.style[attrFloat] = "left";
elePath && funPathMove(300);
};
eleAbsolute.onclick = function() {
eleAvatar.style[attrFloat] = "";
eleAvatar.style.position = "absolute";
elePath && funPathMove(340);
};
}
</script>
</body>
</html>

2、破坏性

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>absolute的破坏性</title>
<style>
.box {
padding: 10px;
background-color: #f0f0f0;
}
input {
position: absolute; top: 234px;
width: 160px; height: 32px;
font-size: 100%;
}
</style>
</head> <body>
<div class="box"><img id="image" src="http://img.mukewang.com/54447b06000171a002560191.jpg" width="256" height="191"></div>
<input id="button" type="button" value="图片absolute化">
<script>
var eleImg = document.getElementById("image"), eleBtn = document.getElementById("button");
if (eleImg != null && eleBtn != null) {
eleBtn.onclick = function() {
if (this.absolute) {
eleImg.style.position = "";
this.value = "图片absolute化";
this.absolute = false;
} else {
eleImg.style.position = "absolute";
this.value = "图片去absolute";
this.absolute = true;
}
};
}
</script>
</body>
</html>

3、包裹性

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>absolute的包裹性</title>
<style>
.box {
padding: 10px;
background-color: #f0f0f0;
}
input {
position: absolute; top: 234px;
width: 160px; height: 32px;
font-size: 100%;
}
</style>
</head> <body>
<div id="box" class="box"><img src="http://img.mukewang.com/54447b06000171a002560191.jpg" width="256" height="191"></div>
<input id="button" type="button" value="容器absolute化">
<script>
var eleBox = document.getElementById("box"), eleBtn = document.getElementById("button");
if (eleBox != null && eleBtn != null) {
eleBtn.onclick = function() {
if (this.absolute) {
eleBox.style.position = "";
this.value = "容器absolute化";
this.absolute = false;
} else {
eleBox.style.position = "absolute";
this.value = "容器去absolute";
this.absolute = true;
}
};
}
</script>
</body>
</html>

二、absolute的独立性

absolute使用的时候,不一定非要和relative在一起,可以独立使用,而且它越独立,能力越大

css之absolute

三、absolute特性

1、去浮动

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>折翼天使表现特性一:去浮动</title>
<style>
input[type=button] {
height: 32px;
font-size: 100%;
}
</style>
</head> <body>
<img src="http://img.mukewang.com/54447b06000171a002560191.jpg">
<img src="http://img.mukewang.com/54447f4a0001eb7d01910256.jpg">
<img src="http://img.mukewang.com/54447f550001ccb002560191.jpg">
<p><input type="button" id="float" value="点击第2张图片应用float:left"></p>
<p><input type="button" id="button" value="点击第2张图片应用position:absolute"></p>
<script>
var flbtn = document.getElementById("float"),
button = document.getElementById("button"),
image2 = document.getElementsByTagName("img")[1];
if (flbtn && button && image2) {
var value_init = button.value;
button.onclick = function() {
if (this.value == value_init) {
image2.style.position = "absolute";
this.value = "撤销";
} else {
image2.style.position = "";
this.value = value_init;
}
}; flbtn.onclick = function() {
image2.style["cssFloat" in this.style? "cssFloat": "styleFloat"] = "left";
};
}
</script>
</body>
</html>

2、位置跟随

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>折翼天使表现特性二:位置跟随</title>
<style>
input[type=button] {
height: 32px;
font-size: 100%;
}
p { margin-left: 260px; }
img + p { margin-top: 60px; }
</style>
</head> <body>
<img src="http://img.mukewang.com/54447b06000171a002560191.jpg">
<div><img src="http://img.mukewang.com/54447f4a0001eb7d01910256.jpg"></div>
<img src="http://img.mukewang.com/54447f550001ccb002560191.jpg">
<p><input type="button" id="block" value="点击第2张图片应用display:block"></p>
<p><input type="button" id="button" value="点击第2张图片应用position:absolute变天使"></p>
<script>
var block = document.getElementById("block"),
button = document.getElementById("button"),
image2 = document.getElementsByTagName("img")[1];
if (block && button && image2) {
var value_init_button = button.value;
button.onclick = function() {
if (this.value == value_init_button) {
image2.style.position = "absolute";
this.value = "撤销";
} else {
image2.style.position = "";
this.value = value_init_button;
}
}; var value_init_block = block.value;
block.onclick = function() {
if (this.value == value_init_block) {
image2.style.display = "block";
this.value = "撤销";
} else {
image2.style.display = "";
this.value = value_init_block;
}
};
}
</script>
</body>
</html>

3.超越overflow

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>超越overflow</title>
<style>
body {
background-color: #bbb;
}
.scroll {
width: 500px; height: 300px;
margin: 200px auto 0;
margin-top: -webkit-calc(50vh - 150px);
margin-top: calc(50vh - 150px);
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(http://img.mukewang.com/5444835b000100ce00340075.png) no-repeat;
}
.close:hover {
background-position: 0 -41px;
}
img {
display: block;
margin: 10px;
}
</style>
</head> <body>
<div class="scroll">
<a href="javascript:" class="close" title="关闭"></a>
<img src="http://img.mukewang.com/54447b06000171a002560191.jpg">
<img src="http://img.mukewang.com/54447f550001ccb002560191.jpg">
</div>
</body>
</html>

滑动滚动条的时候,关闭按钮不移动,因为设置了absolute

四、absolute实际使用

1、图片图标绝对定位覆盖

css之absolute

上面图片里面的Hot、推荐、vip都是用absolute实现的,而且没有用relative

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>图标定位二三事</title>
<style>
body { font: 14px/1.4 "Microsoft YaHei"; background-color: #EDEFF0; }
body, h3, h5 { margin: 0; }
img { border: 0 none; vertical-align: bottom; }
.l { float: left; }.r { float: right; }
.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; } .course { padding-top: 10px; }
.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; }
.course-list-h { line-height: 50px; font-size: 14px; font-weight: 400; color: #363d40; text-align: center; }
.course-list-tips { margin: 0 14px; font-size: 12px; color: #b4bbbf; overflow: hidden; } .icon-hot { position: absolute; width: 28px; height: 11px; margin: -6px 0 0 2px; background: url(http://img.mukewang.com/545304730001307300280011.gif); }
.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(http://img.mukewang.com/5453048000015d8800360036.gif); text-indent: -9em; overflow: hidden; }
</style>
</head> <body>
<div class="header">
<div class="constr">
<div class="nav">
<h3 class="nav-list">
<a href="http://www.imooc.com/course/list" class="nav-a">课程</a>
</h3>
<h3 class="nav-list">
<a href="http://www.imooc.com/wenda" class="nav-a">问答</a>
</h3>
<h3 class="nav-list">
<a href="http://www.imooc.com/seek/index" class="nav-a">
求课<i class="icon-hot"></i>
</a>
</h3>
</div>
</div>
</div> <div class="main">
<div class="constr">
<div class="course">
<a href="http://www.imooc.com/view/121" class="course-list">
<div class="course-list-img">
<span class="icon-recom">推荐</span>
<img width="280" height="160" alt="分享:CSS深入理解之float浮动" src="http://img.mukewang.com/53d74f960001ae9d06000338-300-170.jpg"><!--
--><i class="icon-vip">vip</i>
</div>
<h5 class="course-list-h">分享:CSS深入理解之float浮动</h5>
<div class="course-list-tips">
<span class="l">已完结</span>
<span class="r">3514人学习</span>
</div>
</a>
</div>
</div>
</div>
</body>
</html>

2、下拉框定位

css之absolute

用的是无依赖绝对定位,就是只用了absolute,没有用relative

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>下拉框定位二三事</title>
<style>
body { margin: 0; font: 14px/1.4 "Microsoft YaHei"; background-color: #EDEFF0; }
.constr { width: 1200px; max-width: 80%; margin-left: auto; margin-right: auto; padding-bottom: 300px; overflow: hidden; }
.course-sidebar { width: 262px; float: left; }
.course-sidebar > div { border: 1px solid #e6e8e9; box-shadow: 0px 1px 2px #d5d7d8; background-color: #fff; }
.course-sidebar-type { height: 380px; }
.course-sidebar-search { margin-top: 20px; overflow: hidden; }
.course-search-input { width: 200px; line-height: 18px; padding: 10px; margin: 0; border: 0 none; font-size: 12px; font-family: inherit; float: left; }
.course-sidebar-search.focus { border-color: #2ea7e0; }
.course-search-input:focus { outline: 0 none; }
.course-search-input::-ms-clear { display: none; }
.course-search-btn { width: 38px; height: 38px; float: right; background: url(http://img.mukewang.com/545305ba0001f3f600380076.png); text-indent: -9em; overflow: hidden; }
.focus .course-search-btn { background-position: 0 -38px; } .course-sidebar-result { display: none; position: absolute; width: 260px; margin: 39px 0 0 -1px; padding-left: 0; list-style-type: none; border: 1px solid #e6e8e9; background-color: #fff; box-shadow: 0px 1px 2px #d5d7d8; font-size: 12px; }
.course-sidebar-result > li { line-height: 30px; padding-left: 12px; }
.course-sidebar-result > li:hover { background-color: #f9f9f9; }
.course-sidebar-result a { display: block; color: #5e5e5e; text-decoration: none; }
.course-sidebar-result a:hover { color: #000; }
</style>
</head> <body>
<div class="constr">
<div class="course-sidebar">
<div class="course-sidebar-type"></div>
<div class="course-sidebar-search">
<ul id="result" class="course-sidebar-result">
<li><a href="http://www.imooc.com/view/121">分享:CSS深入理解之float浮动</a></li>
<li><a href="http://www.imooc.com/view/118">案例:CSS圆角进化论</a></li>
<li><a href="http://www.imooc.com/view/93">案例:CSS Sprite雪碧图应用</a></li>
<li><a href="http://www.imooc.com/view/77">案例:CSS3 3D 特效</a></li>
<li><a href="http://www.imooc.com/view/57">案例:如何用CSS进行网页布局</a></li>
</ul>
<input class="course-search-input" placeholder="课程搜索">
<a href="javascript:" class="course-search-btn">搜索</a>
</div>
</div>
</div>
<script>
(function() {
var input = document.getElementsByTagName("input")[0],
result = document.getElementById("result"); if (input && result) {
input.onfocus = function() {
this.parentNode.className = "course-sidebar-search focus";
if (this.value != "") {
// show datalist
result.style.display = "block";
}
};
input.onblur = function() {
if (this.value == "") {
this.parentNode.className = "course-sidebar-search";
}
// hide datalist
result.style.display = "none";
}; // IE7 that wrap a DIV for avoid bad effect from float
if (!document.querySelector) {
var div = document.createElement("div");
input.parentNode.insertBefore(div, input);
div.appendChild(result);
}
// events of datalist
if ("oninput" in input) {
input.addEventListener("input", function() {
if (this.value.trim() != "") {
result.style.display = "block";
} else {
result.style.display = "none";
}
});
} else {
// IE6-IE8
input.onpropertychange = function(event) {
event = event || window.event;
if (event.propertyName == "value" && /focus/.test(this.parentNode.className)) {
if (this.value != "") {
result.style.display = "block";
} else {
result.style.display = "none";
}
}
}
}
} })();
</script>
</body>
</html>

3、居中、边缘定位

css之absolute

实现图片居中和边缘定位的一种思路,可以用无限制的absolute,里面用了&nbsp;和absolute的跟随性,注意代码中的&nbsp;,这个很重要

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>居中、边缘定位二三事</title>
<style>
body { margin: 0; font: 14px/1.4 "Microsoft YaHei"; background-color: #EDEFF0; }
.constr { width: 1200px; max-width: 80%; margin-left: auto; margin-right: auto; }
.course-content { float: right; position: relative; width: 920px; min-height: 1200px; background: #fff; }
.course-list-x { padding: 20px 10px; overflow: hidden; }
.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; } .goto_top_diaocha, .goto_top_app, .goto_top_feed { display: block; width: 48px; height: 48px; margin-top: 10px; background: url(http://img.mukewang.com/5453076e0001869c01920098.png) no-repeat; }
.goto_top_diaocha { background-position: -48px 0; }
.goto_top_diaocha:hover { background-position: -48px -50px; }
.goto_top_app { background-position: -96px 0; }
.goto_top_app:hover { background-position: -96px -50px; }
.goto_top_feed { background-position: -144px 0; }
.goto_top_feed:hover { background-position: -144px -50px; } .course-loading-x { height: 0; margin-top: 20px; text-align: center; letter-spacing: -.25em; overflow: hidden; }
.course-loading { position: absolute; margin-left: -26px; } .course-fixed-x { height: 0; text-align: right; overflow: hidden; }
.course-fixed { display: inline; position: fixed; margin-left: 20px; bottom: 100px; }
</style>
</head> <body>
<div class="constr">
<div class="course-content">
<div class="course-list-x">
<div class="course-list"></div>
<div class="course-list"></div>
<div class="course-list"></div>
<div class="course-list"></div>
<div class="course-list"></div>
<div class="course-list"></div>
</div>
<div class="course-loading-x">
&nbsp;<img src="http://img.mukewang.com/5453077400015bba00010001.gif" class="course-loading" alt="加载中...">
</div>
<div class="course-fixed-x">
&nbsp;<div class="course-fixed">
<a href="http://www.imooc.com/activity/diaocha" class="goto_top_diaocha"></a>
<a href="http://www.imooc.com/mobile/app" class="goto_top_app"></a>
<a href="http://www.imooc.com/user/feedback" class="goto_top_feed"></a>
</div>
</div>
</div>
</div>
</body>
</html>

4、文件图片对其与定位

css之absolute

实际项目中,有的地方有星星,有的没有,想要对其文字比较麻烦,就可以用无依赖的absolute

邮箱前面的图标,以及超出框的文字,都可以用无依赖的absolute

它给页面的布局提供了一种新的思路

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>文本图标对齐与定位二三事</title>
<style>
body { margin: 0; font: 14px/1.4 "Microsoft YaHei"; background-color: #EDEFF0; }
a { color: #50B6E5; }
.constr { width: 1200px; margin-left: auto; margin-right: auto; }
.regist-head { height: 60px; line-height: 60px; padding-left: 30px; background-color: #be3948; color: #fff; font-size: 18px; }
.regist-body { min-height: 400px; padding: 100px 0; background-color: #fff; }
.regist-main { width: 600px; margin-left: auto; margin-right: auto; }
.regist-group { margin-top: 20px; overflow: hidden; }
.regist-label { width: 70px; padding-top: 10px; float: left; }
.regist-cell { display: table-cell; *display: inline-block; }
.regist-input { height: 18px; line-height: 18px; width: 260px; padding: 10px 5px; margin: 0 10px 0 0; border: 1px solid #d0d6d9; vertical-align: top; }
.regist-code-input { width: 130px; }
.regist-btn { display: inline-block; width: 160px; line-height: 40px; background-color: #39b94e; color: #fff; text-align: center; text-decoration: none; }
.regist-btn:hover { background-color: #33a646; }
.icon-warn { display: inline-block; width: 20px; height: 21px; background: url(http://img.mukewang.com/5453084a00016ae300120012.gif) no-repeat center; } .regist-star { position: absolute; margin-left: -1em; font-family: simsun; color: #f30; }
.regist-remark { position: absolute; line-height: 21px; padding-top: 9px; color: #666; }
.regist-warn { padding-left: 20px; color: #be3948; }
.regist-warn > .icon-warn { position: absolute; margin-left: -20px; }
</style>
</head> <body>
<div class="constr">
<div class="regist-head">注册</div>
<div class="regist-body">
<div class="regist-main">
<div class="regist-group">
<label class="regist-label"><span class="regist-star">*</span>登录邮箱</label>
<div class="regist-cell">
<input type="email" class="regist-input"><span class="regist-remark regist-warn">
<i class="icon-warn"></i>邮箱格式不准确(演示)
</span>
</div>
</div>
<div class="regist-group">
<label class="regist-label"><span class="regist-star">*</span>登录密码</label>
<div class="regist-cell">
<input type="password" class="regist-input"><span class="regist-remark">
请输入6-16位密码,区分大小写,不能使用空格
</span>
</div>
</div>
<div class="regist-group">
<label class="regist-label"><span class="regist-star">*</span>用户昵称</label>
<div class="regist-cell">
<input type="password" class="regist-input">
</div>
</div>
<div class="regist-group">
<label class="regist-label">手机号码</label>
<div class="regist-cell">
<input type="tel" class="regist-input">
</div>
</div>
<div class="regist-group">
<label class="regist-label"><span class="regist-star">*</span>验 证 码</label>
<div class="regist-cell">
<input class="regist-input regist-code-input"><img src="http://img.mukewang.com/545308540001678401500040.jpg">
</div>
</div>
<div class="regist-group">
<label class="regist-label">&nbsp;</label>
<div class="regist-cell">
<input type="checkbox" checked><label>我已阅读并同意<a href="##">慕课协议</a>。</label>
<p>
<a href="javascript:" class="regist-btn">立即注册</a>
</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

五、absolute的脱离文档流

动画尽量作用在absolute上,因为absolute是脱离文档流的,这样动画的时候就不会影响其他元素

  z-index无依赖
1.如果只有一个绝对定位元素,自然不需要z-index,自动覆盖普通元素;
2.如果两个绝对定位,控制DOM流的前后顺序达到需要的覆盖效果,依然无z-index;
3.如果多个绝对定位交错,非常非常少见,z-index:1控制;
4.如果非弹窗类的绝对定位元素z-index>2,必定z-index冗余,请优化!
六、absolute与width和height的关系
1、相互替代性
绝对定位的方向是相互对立的(如:left VS right,top VS bottom)时候,会被拉伸
要想页面全屏显示,可以用position:absolute,width:100%,height:100%或者position:absolute,top:0,bottom:0,
left:0,right:0
2、相互支持性
容器无需固定width/height值,内部元素亦可以拉伸;容器拉伸,内部元素支持百分比width/height值; demo

无固定width/height容器内的绝对定位元素拉伸


 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无固定width/height容器内的绝对定位元素拉伸</title>
<style>
body {
background-color: #ddd;
}
img {
vertical-align: bottom;
}
.container {
display: inline-block;
position: relative;
}
.cover {
position: absolute;
left: 0; top: 0; right: 0; bottom: 0;
background-color: #fff;
opacity: .5; filter: alpha(opacity=50);
}
</style>
</head> <body>
<span class="container">
<i class="cover"></i>
<img src="http://img.mukewang.com/547c34cf000171a002560191.jpg" width="512" height="381">
</span>
<span class="container">
<i class="cover"></i>
<img src="http://img.mukewang.com/547c34c9000171a002560191.jpg" width="256" height="191">
</span>
</body>
</html>

没有宽度和高度声明实现的全屏自适应效果

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>没有宽度和高度声明实现的全屏自适应效果</title>
<style>
html, body { height: 100%; }
.overlay {
position: absolute;
left: 0; top: 0; right: 0; bottom: 0;
background-color: #000;
opacity: .5; filter: alpha(opacity=50);
}
</style>
</head> <body>
<div class="overlay"></div>
</body>
</html>

高度自适应的九宫格效果

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>高度自适应的九宫格效果</title>
<style>
html, body { height: 100%; margin: 0; }
.page {
position: absolute;
left: 0; top: 0; right: 0; bottom: 0;
}
.list {
float: left;
height: 33.3%; width: 33.3%;
position: relative;
}
.list:before {
content: '';
position: absolute;
left: 10px; right: 10px; top: 10px; bottom: 10px;
border-radius: 10px;
background-color: #cad5eb;
}
.list:after {
content:attr(data-index);
position: absolute;
height: 30px;
left: 0; right: 0; top: 0; bottom: 0;
margin: auto;
text-align: center;
font: 24px/30px bold 'microsoft yahei';
}
</style>
</head> <body>
<div class="page">
<div class="list" data-index="1"></div>
<div class="list" data-index="2"></div>
<div class="list" data-index="3"></div>
<div class="list" data-index="4"></div>
<div class="list" data-index="5"></div>
<div class="list" data-index="6"></div>
<div class="list" data-index="7"></div>
<div class="list" data-index="8"></div>
<div class="list" data-index="9"></div>
</div>
</body>
</html>

left/right拉伸和width同时存在,实现元素水平居中

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>left/right拉伸和width同时存在</title>
<style>
.image {
position: absolute; left: 0; right: 0; width: 50%;
}
.button {
padding-top: 200px;
}
.button input {
width: 280px; height: 40px;
font-size: 20px;
}
</style>
</head> <body>
<img class="image" src="http://img.mukewang.com/547c34c9000171a002560191.jpg" height="191">
<p class="button">
<input type="button" value="添加margin: auto;" onClick="document.getElementsByTagName('img')[0].style.margin = 'auto';">
</p>
</body>
</html>

七、absolute与整体布局

1、html,body{height:100%}

div{position:absolute;left:0;top:0;right:0;bottom:0}实现全屏布局

2、头、尾、侧边栏各居其位

header,footer{position:absolute;left:0;right:0}

header{height:48px;top:0}

footer{height:52px;bottom:0}

aside{width:250px;position:absolute;left:0;top:0;bottom:0}

.content{position:absolute;top:48px;bottom:52px;left:250px;overflow:auto}

效果:

css之absolute

这个时候,头部和尾部都是fix效果,滚动content的时候,只有content区域滚动。避免了移动端position:fixed实现的诸多问题

3.加载效果

css之absolute

css之absolute

参考地址:http://www.imooc.com/learn/192