2.07日(rì)音(yīn)訊,c✔©£δss教程之絕對(duì)定位的(de)滲入。如(rú★β÷)果說(shuō)相(xiàng)對(duì)定位沒 ₽δ☆有(yǒu)脫離(lí)文(wén)檔流,相(xiàng)對(duì)于對δ≤ (duì)象本身(shēn)進行(xíng)偏移有(yǒu)點拖泥帶水(shu↔ ǐ)的(de)話(huà),那(nà)麽絕對₩ '≤(duì)定位絕對(duì)是(shì)快(kuài)刀(dā∞$♦o)斬亂麻,因為(wèi)絕對(duì)定位可(kě)以使一(yī)個(™¥φgè)對(duì)象脫離(lí)正常的(γde)文(wén)檔流,好(hǎo)像是(shì)漂浮在正常文(w↔↑én)檔流的(de)上(shàng)空(kōng),并相(xiàng)對¥σΩ(duì)于包含此對(duì)象的(de)元素進行(xíng)定位¶↓±,當然這(zhè)個(gè)定位相(xiàng↑♠λ™)對(duì)元素在不(bù)同的(de)情況下(xià)也(y÷☆ě)有(yǒu)所不(bù)同。
如(rú)何将一(yī)個(gè)元•₩♥素設置為(wèi)絕對(duì)定位:
為(wèi)對(duì)象添加如(rú)下(xià)屬性即可(kě)将₽£©≥對(duì)象設置為(wèi)絕對(duì)定位:
複制(zhì)代碼代碼如(rú)下(xià):
position:absolute;
或者
複制(zhì)代碼代碼如(rú)下(xià):
position:fixed
定位參考對(duì)象:
可(kě)以使用(yòng)top屬性和(hé)left☆α¶屬性設置絕對(duì)定位對(duì)象的(₹♠¶de)偏移量。
絕對(duì)定位雖然脫離(lí)了(le)文(wγ∑δ♠én)檔流,但(dàn)是(shì)也(yě)✔σ≠需要(yào)有(yǒu)定位的(de)參考對("©duì)象,不(bù)過在不(bù)同的(de π)情況下(xià)參考對(duì)象也(yě)是(shì)不(b↓φ¥₽ù)同。
1.如(rú)果沒有(yǒu)設置top或者left屬性值,那(nà) ←麽相(xiàng)應方位的(de)定位參考對(duì)象就ε¥☆ (jiù)是(shì)此對(duì)象的(de)一(yī)級父元素,代♣"€$碼實例如(rú)下(xià):
複制(zhì)代碼代碼如(rú)下(xià):
<!DOCTYPEhtml>↕↑";
<html>
<head>
<metacharset=&qu¶λ&ot;utf-8">
<styletype="text/c§φ₹$ss">
body
{
margin:20px;
}
#grandfather
{
width:330px;
height:300px;
background-color:#F90;₩$φ
}
#father
{
width:200px;
height:200px;
background-color:green;
margin-left:50px;
}
#children
{
width:100px;
height:100px;
background-color:red;
float:right;
}
#inner
{
width:50px;
height:50px;
background-color:blue;
position:absolute;
top:10px;
}
</style>
</head>
<body>
<divid="granγ dfather">
<divid="father">
<divid="c↔<hildren">
<divid="inner"></★β™div>
</div>
</div>
</div>
</body>
</html>
以上(shàng)代碼中,由于inner元素采用(yòng)絕對↓≤₩×(duì)定位,并且沒有(yǒu)設置left屬性值,所以在水(shuǐ)平方₩ 位的(de)定位參考對(duì)象就(jiù)是(shì)i$♣nner元素的(de)一(yī)級父元素chil₽♣↕dren。當然如(rú)果沒有(yǒu)設置→→top屬性值,那(nà)麽垂直方位的(de)定位參考對(duì)象也(yě)是™™♣₹(shì)children。
2.如(rú)果設置了(le)left♠÷ 或者top屬性值情況:
1).如(rú)果祖先元素中有(yǒu)采用(yòng)定>位的(de),那(nà)麽此對(duì)象的(de)相(xiànδ"g)應方位的(de)定位參考對(duì)象就(jiù)是(shì)此¶ €祖先元素,如(rú)果祖先元素沒有(yǒu)采₹≈€§用(yòng)定位的(de),那(nà)₽λ麽相(xiàng)應方位的(de)上(shàng)的(de)定位參考對(±☆αduì)象就(jiù)是(shì)浏覽器(qì)客戶區(₹≈&qū),代碼實例如(rú)下(xià):
實例一(yī):
複制(zhì)代碼代碼如(rú)下(xià):
<!DOCTYPEhtml>
<html>
<head>
<metacharset="u ♣tf-8">
<title>CSS的(de)絕對(duì)定Ωβ位</title>
<styletype="text/css ®">
body
{
margin:20px;
}
#grandfather
{
width:330px;
height:300px;
background-color:• ₩☆#F90;
}
#father
{
width:200px;
height:200px;
background-color:gre✔∑πen;
margin-left:50px;
position:relative;
}
#children
{
width:100px;
height:100px;
background-color:red;
float:right;
}
#inner
{
width:50px;
height:50px;
background-color:blue;
position:absolute;
left:10px;
top:10px;
}
</style>
</head>
<body>
<divid="grandfather&quo®≈<t;>
<divid="f∏♥₹↔ather">
<divid="children">≈₹
<divid="inner&∏"φφquot;></div>
</div>
</div>
</div>
</body>
</html>
以上(shàng)代碼,inner元素采用(yòng)絕對(du★★♦ì)定位,并且它的(de)祖先元素father采用(yòng)相(xiàng↔λ✔≤)對(duì)定位,那(nà)麽它的(de)定位參考對(∏ duì)象就(jiù)是(shì)father。
實例二:
複制(zhì)代碼代碼如(rú)下(xià):
<!DOCTYPEhtml>
<html>
<head>
<metacharset=&qu¥↕σot;utf-8">
<title>CSS的(de)絕對(duì)定♦←&•位</title>
<styletype="text/css&quo✘↔t;>
body
{
margin:20px;
}
#grandfather
{
width:330px;
height:300px;
background-color:#F90;
}
#father
{
width:200px;
height:200px;
background-color:green;
margin-left:50px;
}
#children
{
width:100px;
height:100px;
background-color:r¶≥ed;
float:right;
}
#inner
{
width:50px;
height:50px;
background-color:blue;
position:absolute;
top:10px;
}
</style>
</head>
<body>
<divid="grandfather">↕★Ω;
<divid="father&quo εt;>
<divid="children">∏∏•γ;
<divid="inner₹Ω"></div>
</div>
</div>
</div>
</body>
</html>
以上(shàng)代碼中,inner元素采用(yòng)絕¥¥對(duì)定位,并且它的(de)祖先元素沒有(yǒu)采用 ™>(yòng)定位的(de),那(nà)麽垂直方位的(de)定位↑→參考對(duì)象就(jiù)是(shì)窗(chuāng)口, $•由于沒有(yǒu)設置left屬性值,那(nà)÷✔★麽水(shuǐ)平方位的(de)定位參考對(duì)象就(ji₽α£ ù)是(shì)它的(de)一(yī)級父元素children。
四.絕對(duì)定位元素脫離(lí)文(w§'∑én)檔流:
在開(kāi)頭已經提到(dào)過,絕對÷≥(duì)定位能(néng)夠使元素脫離(líγβ)文(wén)檔流,那(nà)麽它周邊文(wén)↓∑檔流中的(de)元素就(jiù)能(néng)夠占據此元素沒有(yǒu)脫離$λ←(lí)文(wén)檔流時(shí)的(de)位置₹±。
代碼實例如(rú)下(xià):
複制(zhì)代碼代碼如(rú)下(xià):☆★•
<!DOCTYPEhtml>
<html>
<head>
<metacharset="utf-8&¶">
<title>CSS的(de)絕對(duì)定位&•¥§ lt;/title>
<styletype="text/c™₽↑∑ss">
div
{
text-align:center;
line-height:100px;
}
.father
{
width:400px;
height:400px;
background-color:gree∑±n;
margin:50px;
}
.first
{
width:100px;
height:100px;
background-color:r©λ ed;
position:absolute;
}
.second
{
width:120px;
height:120px;
background-color:blue;
}
</style>
</head>
<body>
<divclass="fa♥∑↔ther">
<divclass="εβδfirst">first</div>
<divclass="second&quo'≈×t;>second</div>
</div>
</body>
</html>
在以上(shàng)代碼中,由于first元素脫離(lí)文(♠←↓αwén)檔流,所以second元素能(néng)夠占據原來(lái)firs↓↑®≠t元素本來(lái)應該占據的(de)位置。