一、語法結(jié)構(gòu)
Left、right、top、bottom后跟數(shù)字+html單位。
示范結(jié)構(gòu)
div{left:20px}
div{right:20px}
div{top:20px}
div{bottom:20px}
Left 靠左距離多少
Right 靠右邊距離多少
Top 距離頂部距離多少
Bottom距離下邊距離多少
二、使用條件
通常單獨(dú)使用left、right、top、bottom均無效,需要在使用絕對定位CSS position樣式才能生效。
一般left和right在一個樣式是只能使用其一,不能left和right都設(shè)置,要么使用left就不使用right,要么使用right就不使用left,如果left和right均使用將會出現(xiàn)兼容問題,一個對象設(shè)置了靠左left多少距離,自然右邊距離自然就有了所以無需設(shè)置左邊。
相同道理,top和bottom對一個對象只能使用其一,不然會出現(xiàn)邏輯兼容問題。譬如一個人讓你往左走,一個人讓你往右走,同時發(fā)出往左往右走這個時候你也不好判斷往那邊走。
三、絕對定位中使用
一般left、right、top、bottom使用于配合position定位對象位置。大家可以進(jìn)入CSS position教程篇了解學(xué)習(xí)這些樣式屬性。
實(shí)例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>無標(biāo)題文檔</title> <script type="text/javascript" language="javascript"> function selA(id) { switch(id) { case "1": document.getElementById("subobj").style.position = "static"; break; case "2": document.getElementById("subobj").style.position = "absolute"; break; case "3": document.getElementById("subobj").style.position = "relative"; break; case "4": document.getElementById("subobj").style.position = "fixed"; break; } } </script> <style type="text/css"> #all { width:190px; height:95px; padding:10px 0 0 10px; border:1px #000 solid; position:relative; } #subobj { width:100px; height:50px; border:1px #000 solid; bottom:9px; position:static; } #sel { margin-top:5px; } select { width:200px; } </style> </head> <body> <div id="all"> <div id="subobj">子對象1</div> </div> <div id="sel"><select onchange="selA(this.value)"><option value="1">static</option><option value="2">absolute</option><option value="3">relative</option><option value="4">fixed</option></select></div> </body> </html>
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com