CSS關於相對定位和絕對位置的說明執行個體

來源:互聯網
上載者:User

<style>
body
{margin: 30px; font-size:9pt;}

.a, .b, .c, .d, .e
{
width: 100px;
height: 100px;
margin: 5 auto;
color: #fff;
background: #000;
}
.aa, .bb, .cc, .dd, .ee
{
top: 10px;
left: 10px;
width: 10px;
height: 10px;
overflow: hidden;
background: #F90;
}
.b, .d, .e
{position: relative;}
.cc, .dd, .ee
{position: absolute;}
</style>

<div class="a">
<div class="aa"></div>
A:均不設定postion,一般嵌套關係
</div>

<div class="b">
<div class="bb"></div>
B:僅外div設定relative,一般嵌套關係
</div>

<div class="c">
<div class="cc"></div>
C:僅內div設定absolute,文檔中為嵌套關係,頁面中內div浮起[非float],相對於頁面定位,與外div無關。
</div>

<style>
body
{margin: 30px; font-size:9pt;}

.a, .b, .c, .d, .e
{
width: 100px;
height: 100px;
margin: 5 auto;
color: #fff;
background: #000;
}
.aa, .bb, .cc, .dd, .ee
{
top: 10px;
left: 10px;
width: 10px;
height: 10px;
overflow: hidden;
background: #F90;
}
.b, .d, .e
{position: relative;}
.cc, .dd, .ee
{position: absolute;}
</style>

<div class="a">
<div class="aa"></div>
A:均不設定postion,一般嵌套關係
</div>

<div class="b">
<div class="bb"></div>
B:僅外div設定relative,一般嵌套關係
</div>

<div class="c">
<div class="cc"></div>
C:僅內div設定absolute,文檔中為嵌套關係,頁面中內div浮起[非float],相對於頁面定位,與外div無關。
</div>

<div class="d" style="background:#ff0000">
<div class="dd" ></div>
D:外div設定relative,內div設定absolute,內div浮起來並相對於外div定位
</div>

<div class="d" style="background:#ff0000">
<div class="dd" style="position:relative"></div>
D:外div設定relative,內div設定relative,內div浮起來並相對於外div定位
</div>

<div class="e">
<div class="ee" style="left: -10px;"></div>
E:這個是說明邊界問題。-10 != 反向10px間距
</div>

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.