CSS学习之五 定位布局

2020-05-15 08:18:51易采站长站整理

</div>
 

效果截图:

相对定位元素的宽高

相对定位元素的宽高

结论

相对定位元素不能用top,left,right,bottom关键字指定宽高(此时right和bottom值被忽略),必须用width和height指定。
相对定位的内联元素不可以设置宽高。

2、绝对定位的情况:

HTML结构:


<div class=”father position_r”>
<div class=”child position_a” style=”top: 10px; right: 10px; bottom: 10px; left: 10px; “>绝对定位元素,可以用top,left,right,bottom关键字结合width指定宽高。IE6浏览器下不可以。
top: 10px; right: 10px; bottom: 10px; left: 10px; </div>
</div>
<div class=”father position_r”>
<div class=”child position_a” style=”top: 10px; right: 10px; bottom: 10px; left: 10px; width: 100%; “>绝对定位元素,可以用top,left,right,bottom关键字结合width指定宽高。IE6浏览器下不可以
top: 10px; right: 10px; bottom: 10px; left: 10px; width: 100%; </div>
</div>
<div class=”father position_r”>
<div class=”child position_a” style=”top: 10px; right: 10px; bottom: 10px; left: 10px; width: auto; “>绝对定位元素,可以用top,left,right,bottom关键字结合width指定宽高。IE6浏览器下不可以。
top: 10px; right: 10px; bottom: 10px; left: 10px; width: auto; </div>
</div>
<div class=”father”>
<span style=” border: 1px solid #999; ” class=”position_r”>行内父元素<span class=”child position_a” style=” top: 10px; left: 10px; width: 100%;”>绝对定位的行内元素。这里可以设置其宽高。IE6下宽度不受行内父元素限制。</span></span>
</div>

效果截图:

绝对定位元素的宽高1

绝对定位元素的宽高1

绝对定位元素的宽高2

绝对定位元素的宽高2

结论

非IE6下,绝对定位元素可以用top,left,right,bottom关键字指定宽高(注意对比,需结合width指定才行),也可以用width和height指定。
绝对定位的内联元素也可以设置宽高。
事实上,绝对定位的元素宽度有以下规则:left+margin-left+border-left-width+padding-left+width+padding-right+border-right-width+margin-right+right=包含块width。
IE6下绝对定位的行内元素宽度不受父元素限制。

五、定位元素在z轴上的放置
以下是CSS2.1关于z-index属性的一些说明: