以下討論的是和定位 絕對定位 相對定位 fixed static 相關的css基礎教程之定位方法:絕對定位、相對定位、fixed和static教程文章,內容是本站精心挑選整理的教程,希望對廣大的網友給到幫助,下面是詳細內容:
1.絕對定位(absolute):脫離原來位置進行定位,可以使用left right top bottom進行定位
html代碼
<!DOCTYPE html><html lang="en"><head> <meter charset="utf-8"> <title>hhhh</title> <link rel="stylesheet" type="text/css" href="lesson3.css"></head><body> <p class="demo"></p> <p class="box"></p></body></html>
css代碼:
*{ margin:0; padding:0;}.demo{ position:absolute; /*top:100px; left:100px;*/ width: 100px; height: 100px; background-color: red; opacity: 0.5; /*透明度*/}.box{ width:150px; height:150px; background-color: green;}2.相對定位(relative):保留原來位置進行定位,可以使用left right top bottom進行定位
html代碼:
<!DOCTYPE html><html lang="en"><head> <meter charset="utf-8"> <title>hhhh</title> <link rel="stylesheet" type="text/css" href="lesson3.css"></head><body> <p class="demo"></p> <p class="box"></p></body></html>
css代碼
*{ margin:0; padding:0;}.demo{ position:relative; /*top:100px; left:100px;*/ width: 100px; height: 100px; background-color: red; opacity: 0.5; /*透明度*/}.box{ width:150px; height:150px; background-color: green;}3.fixed:相對于瀏覽器的窗口對元素進行定位,可以使用left right top bottom進行定位
4.static:默認值,沒有定位,top left 等不起作用
<div class="wrapper"> <div class="demo"> <div class="box"></div> </div></div>
*{ margin:0; padding:0;}.wrapper{ margin-left: 100px; width: 200px; height: 200px; background-color: orange;}.demo{ position:relative; margin-left: 100px; width: 100px; height: 100px; background-color: red; }.box{ position:absolute; left: 50px; width:50px; height:50px; background-color: green;} css基礎教程之定位方法:絕對定位、相對定位、fixed和static文章就講到這里,歡迎瀏覽本站的其它內容,點擊這里返回首頁
新聞熱點
疑難解答