本篇文章為大家展示了css的三種定位方式是什么,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比靈寶網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式靈寶網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋靈寶地區(qū)。費用合理售后完善,十多年實體公司更值得信賴。
css的三種定位方式是:1、相對定位,元素的位置相對于它的原始位置計算而來,語法“position:relative;”;2、固定定位,語法“position:fixed;”;3、絕對定位,語法“position:absolute;”。
本教程操作環(huán)境:windows7系統(tǒng)、CSS3&&HTML5版、Dell G3電腦。
相對定位
該元素的位置是相對于它的原始位置計算而來的。
position:relative;
他是默認參照父級的原始點為原始點,配合top、right、bottom、left進行定位。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>相對定位</title> <style type="text/css"> .king{ margin-top: 30px; margin-left: 30px; border: 1px solid silver; background-color: skyblue; width: 40%; } .king div{ width: 100px; height: 60px; margin: 10px; background-color: snow; color: black; border: 1px solid black; } .three{ position: relative; top: 20px; left: 50px; } </style> <body> <div class="king"> <div class="one">one</div> <div class="two">two</div> <div class="three">three</div> <div class="four">four</div> </div> </body> </html>
固定定位
被固定的元素不會隨著滾動條的拖動而改變位置。
position:fixed;
在默認情況下,固定定位元素的位置是相對瀏覽器而言,結(jié)合top、bottom、left和right這4個屬性一起使用。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>固定定位</title> <style type="text/css"> .first{ width: 50px; height: 160px; border: 1px solid gray; background-color: #b7f1b7; } .second{ position: fixed; top: 50px; left: 160px; width: 150px; height: 100px; border: 1px solid silver; background-color:#b7f1b7; } </style> <body> <div class="first">div元素</div> <div class="second">固定定位的div元素</div> </body> </html>
絕對定位
position:absolute;
默認情況下,絕對定位的位置是相對于瀏覽器而言,配合top、right、bottom、left進行定位。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>絕對定位</title> <style type="text/css"> .king{ padding: 15px; border: 1px solid silver; background-color: skyblue; width: 30%; } .king div{ padding: 10px; } .one{ background-color: chartreuse; } .two{ background-color: cyan; position: absolute; top: 20px; right: 40px; } .three{ background-color: darkred; } .four{ background-color: dimgrey; } </style> <body> <div class="king"> <div class="one">one</div> <div class="two">two</div> <div class="three">three</div> <div class="four">four</div> </div> </body> </html>
z-index
z-index 屬性設置元素的堆疊順序。擁有更高堆疊順序的元素總是會處于堆疊順序較低的元素的前面。
元素可擁有負的 z-index 屬性值。
Z-index 僅能在定位元素上奏效(例如 position:absolute;)
屬性值: auto:默認,堆疊順序與父元素相等。 number:設置元素的堆疊順序。 inherit:規(guī)定應該從父元素繼承 z-index 屬性的值。
例: 設置圖像的 z-index:
img{ position:absolute; left:0px; top:0px; z-index:-1; }
上述內(nèi)容就是css的三種定位方式是什么,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁標題:css的三種定位方式是什么
鏈接URL:http://aaarwkj.com/article44/jjggee.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設、營銷型網(wǎng)站建設、建站公司、服務器托管、移動網(wǎng)站建設、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)