以前看过屡次CSS绝对定位,可是缺少一个好的案例。偶尔看到一个控件,以为用它来讲明是很是简明的。app
假设咱们有一个DIV,内部还嵌入两个平级的DIV,代码以下:spa
<div class="wrapper">
<div class="block1"></div>
<div class="block2"></div>
</div>
<style>
.wrapper{border: solid 1px;height:20px;width:220px;}
.block1{background: red;height:10px;}
.block2{background:blue;height:10px;width:100%;}
</style>
复制代码
那么按照默认的盒子模型,两个平级的DIV一上一下,占满整个父亲DIV。若是想要让第二个DIV覆盖第一个怎么办?code
此时就必须取消默认排版过程,转而使用绝对定位。方法就是设置.block2直接相对.wrapper定位,top距离为0便可。具体作法就是在.wrapper内加入代码:rem
position:relative
复制代码
添加CSS代码到.block2内:it
position:absolute;top:0;
复制代码
就能够看到.block2覆盖于.block1之上。这样就达到了咱们但愿的效果了。io
使用彻底相同的结构,咱们能够制做一个进度条控件:class
<style>
.progress { position: relative; border: solid 1px;width: 100px;height:1rem;}
.progress > .bar { background: red; height: 100%; width:10%}
.progress > .label {position: absolute; top: 0; left: 0; width: 100%;
text-align: center; font-size: 0.8rem; }
</style>
<div class="progress">
<div class="bar"></div>
<div class="label">10%</div>
</div>
复制代码
这里的.label正是经过对其父容器.progress的绝对定位,实现了.bar和.label的重合,从而实现的进度条效果。容器