站長資訊網
        最全最豐富的資訊網站

        深入淺析了解CSS中定位布局的細節

        本篇文章給大家帶來了css定位布局的相關知識,下面我們就來看一下什么是相對定位、絕對定位以及固定定位不同的元素性質與用途等知識,希望對大家有幫助。

        深入淺析了解CSS中定位布局的細節

        1.?相對定位

        1.1) 什么是相對定位

        相對定位:盒子可以根據自己原來的位置進行位置調整(通過位置描述詞實現)。

        位置描述詞:
        left: 向右移動; right 向左移動;top 向下移動;bottom 向上移動
        (當里面值為負數的時候,往相反方向移動)

        舉個例子:
        原來:

        <!DOCTYPE html><html lang="en"><head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>相對定位</title>     <style>         * {             margin: 0;             padding: 0;         }          p {             width: 500px;             height: 500px;             border: 1px solid #000;             margin: 50px auto;         }          p {             width: 100px;             height: 100px;             background-color: lightblue;             position: relative;             top: 50px;             left: 50px;         }     </style></head><body>     <p>     	<p></p>     </p></body></html>

        深入淺析了解CSS中定位布局的細節
        將 p 設置成相對定位:

        p {     width: 100px;     height: 100px;     background-color: lightblue;     position: relative;     top: 50px;     left: 50px;}

        深入淺析了解CSS中定位布局的細節

        1.2)相對定位的性質與用途

        性質

        • 相對定位的元素,本質上仍在原來的位置,只不過在新的地方渲染出現,不會對頁面其它元素產生影響。

        用途

        • 用來微調元素位置
        • 相對定位的盒子可以用來做絕對定位的參考盒子

        舉個例子:

        <!DOCTYPE html><html lang="en"><head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>相對定位</title>     <style>         * {             margin: 0;             padding: 0;         }          nav {             width: 780px;             height: 50px;             margin: 40px auto;         }          nav ul {             list-style: none;         }          nav ul li {             float: left;             width: 156px;             height: 50px;             line-height: 50px;             text-align: center;         }          nav ul li a {             display: block;             width: 156px;             height: 50px;             background-color: lightcyan;             color: #000;             text-decoration: none;         }          nav ul li a:hover {             border-top: 3px solid red;         }     </style></head><body>     <nav>         <ul>             <li>                 <a href="#">導航一</a>             </li>             <li>                 <a href="#">導航二</a>             </li>             <li>                 <a href="#">導航三</a>             </li>             <li>                 <a href="#">導航四</a>             </li>             <li>                 <a href="#">導航五</a>             </li>         </ul>     </nav></body></html>

        這個時候效果是這樣:
        深入淺析了解CSS中定位布局的細節
        會發現鼠標懸浮在上面的時候,導航那一塊區域都會下沉
        我們給它設置了相對定位并微調之后:

        nav ul li a:hover {     border-top: 3px solid red;     position: relative;     top: -3px;}

        深入淺析了解CSS中定位布局的細節
        這樣就可以解決剛剛的問題了


        2.?絕對定位

        2.1) 什么是絕對定位

        絕對定位:盒子以坐標進行位置描述,擁有自己絕對位置。

        絕對定位的參考盒子:
        絕對定位的盒子會以自己的祖先元素中,離自己最近的擁有定位屬性的盒子,當做基準點。

        這個盒子通常是相對定位的,所以也被稱作 “子絕父相”。

        位置描述詞:
        left:到左邊的距離;right:到右邊的距離;top:到上邊的距離;bottom:到下邊的距離

        舉個例子:

        <!DOCTYPE html><html lang="en"><head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>絕對定位</title>     <style>         * {             margin: 0;             padding: 0;         }          .box {             position: absolute;             width: 500px;             height: 300px;             left: 200px;             top: 100px;             background-color: antiquewhite;         }     </style></head><body>     <p class="box">     </p></body></html>

        2.2)絕對定位的性質與用途

        絕對定位的盒子垂直居中:

        .box { 	position: absolute; 	top: 50%; 	margin-top: -自己高度一半;}

        絕對定位的盒子水平居中:

        .box { 	position: absolute; 	left: 50%; 	margin-left: -自己寬度一半;}
        • 堆疊順序 z-index 屬性

        設置絕對定位元素的壓疊順序.
        是一個沒有單位的正整數,數值大的能夠壓住數值小的(即數值大的顯示在上層)

        舉個例子:

        <!DOCTYPE html><html lang="en"><head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>絕對定位</title>     <style>         * {             margin: 0;             padding: 0;         }          .box1 {             width: 300px;             height: 300px;             position: absolute;             left: 100px;             top: 100px;             background-color: antiquewhite;         }          .box2 {             width: 300px;             height: 300px;             position: absolute;             left: 200px;             top: 200px;             background-color: lightblue;         }     </style></head><body>     <p class="box1"></p>     <p class="box2"></p></body></html>

        此時效果如下:


        這個時候我們想讓box1顯示在上層,就設置一個z-index 屬性。

        .box1 {     width: 300px;     height: 300px;     position: absolute;     left: 100px;     top: 100px;     background-color: antiquewhite;     z-index: 100;}.box2 {     width: 300px;     height: 300px;     position: absolute;     left: 200px;     top: 200px;     background-color: lightblue;     z-index: 1;}

        看看效果:
        深入淺析了解CSS中定位布局的細節

        • 用途

        絕對定位用來“壓蓋”,“遮罩”的效果
        可以結合 CSS 精靈使用
        可以結合 JS 實現一些動畫

        3.?固定定位

        3.1) 什么是固定定位

        固定定位:不管頁面如何滾動,它永遠以頁面為參考點,固定在那里。

        位置描述詞:
        left:到左邊的距離;right:到右邊的距離;top:到上邊的距離;bottom:到下邊的距離

        .box { 	position: fixed; 	top: 100px; 	left: 100px;}

        3.2)固定定位的性質與用途

        可以用來實現一些元素要一直浮現在當前窗口前,比如瀏覽一個頁面時的返回頂部按鈕,會一直出現在當前頁面的某個位置

        舉個例子:

        <!DOCTYPE html><html lang="en"><head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>固定定位</title>     <style>         * {             margin: 0;             padding: 0;         }          .box {             position: fixed;             bottom: 20px;             right: 20px;             width: 40px;             height: 40px;                          text-align: center;             line-height: 40px;             border-radius: 50%;             background-color: rgba(78, 209, 226, 0.5);                          cursor: pointer;             font-size: 24px;         }     </style></head><body>     <a class="box">^</a>     <p>         <img src="https://dummyimage.com/600x400/00bcd4/fff" alt="">     </p>     <p>         <img src="https://dummyimage.com/600x400/00bcd4/fff" alt="">     </p>     <p>         <img src="https://dummyimage.com/600x400/00bcd4/fff" alt="">     </p></body></html>

        效果如下:
        當頁面到下方時,右下角返回頂部的按鈕位置不變。
        深入淺析了解CSS中定位布局的細節

        大家感興趣的話,可以繼續訪問:css視頻教程。

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 久久99亚洲综合精品首页| 午夜精品射精入后重之免费观看| 日韩精品无码永久免费网站| 99在线热播精品免费99热| 无码精品人妻一区二区三区影院| 98香蕉草草视频在线精品看| 精品国产乱码久久久久久郑州公司| 久久夜色精品国产亚洲av| 亚洲精品在线观看视频| 2018国产精华国产精品| 午夜欧美精品久久久久久久| 亚洲AV蜜桃永久无码精品| 精品91自产拍在线观看| 夜色www国产精品资源站| 国产91精品在线| 国产精品美女一区二区视频| 亚洲国产精品高清久久久| 亚洲精品成人片在线观看| 天天爽夜夜爽夜夜爽精品视频| 精品久久综合1区2区3区激情| 国产精品JIZZ在线观看老狼| 香蕉久久夜色精品国产小说| 国产精品一区二区不卡| 2021国产成人精品国产| 精品熟女少妇a∨免费久久| 日韩人妻精品无码一区二区三区| 亚洲精品欧美精品日韩精品 | 国产一区精品| 99riav国产精品| Xx性欧美肥妇精品久久久久久 | 一夲道无码人妻精品一区二区| 国产午夜精品理论片| 国产精品亚洲一区二区在线观看| 99在线精品免费视频| 国产精品一级AV在线播放| 国产精品毛片无码| 精品无码国产自产拍在线观看蜜| 精品无码三级在线观看视频| 欧美精品一二区| 在线涩涩免费观看国产精品| 久久久久久夜精品精品免费啦|