006、position 定位:設定元件在網頁的位置

1、position 用來設定元件位置(定位)


<style type="text/css">
.square1{
    border: 1px solid #333;
    width: 100px;
    height: 100px;
}
</style>
<div class="square1" style="background-color: red;">1</div>
<div class="square1" style="background-color: yellow;">2</div>
<div class="square1" style="background-color: lime;">3</div>

1
2
3

 

2、position: relative; 相對位置


<style type="text/css">
.square2{
    border: 1px solid #333;
    width: 100px;
    height: 100px;
}
#s2{
    position: relative;     /* 設定成相對位置 */
    top: 50px;              /* 與上方距離 */
    left: 100px;            /* 與左方距離 */
}
</style>
<div class="square2" style="background-color: red;">1</div>
<div class="square2" style="background-color: yellow;" id="s2">2</div>
<div class="square2" style="background-color: lime;">3</div>

1
2
3

 

3、position: absolute 絕對位置


<style type="text/css">
.square3{
    border: 1px solid #333;
    width: 100px;
    height: 100px;
}
#box{
    position: relative;
    border: 1px solid #333;
    width: 600px;
    height: 300px;
}
#a31{
    position: absolute;
    top: 30px;
    left:30px;
}
#a32{
    position: absolute;
    right: 30px;
    bottom: 30px;
}
</style>
<div id="box">
    <div class="square3" style="background-color: yellow;" id="a31">2</div>
    <div class="square3" style="background-color: lime;" id="a32">3</div>
</div>

2
3

 

4、position: fixed 固定位置


<style type="text/css">
#box2{
    position: fixed;
    top: 100px;
    right: 100px;
    border: 10px solid lightgreen;
    border-radius: 50%;             /* 圓形 */
    overflow: hidden;               /* 溢出:超出邊框則隱藏 */
    z-index: 100;                   /* 堆疊值,設定為100 */
}
</style>
<div id="box2">
    <img src="images/logo.png">
</div>

shape
shape