Html/CSS06(定位,居中)

今天是2018年7月16日

1.相對(duì)定位

給元素設(shè)置position: relative;可以使元素處于相對(duì)定位中。

<style>
        /* 相對(duì)定位就是元素在頁(yè)面上正常的位置 */
        div{
            width: 100px;
            height: 100px;
            background-color: red;
            position: relative;
            top: 0px;
            left:0px;
            /* 相對(duì)定位一般不適用right/bottom */
            right: 0px;
            bottom: 0px;
        }
    </style>

2.絕對(duì)定位

不同于相對(duì)定位,絕對(duì)定位是指在父元素中絕對(duì)的位置

<style>
        .parent{
            width: 200px;
            height: 200px;
            background-color: red;
            position: relative;
        }
        .child{
            width: 50px;
            height: 50px;
            background-color: blue;
            position: absolute;
            right: 0;
        }
    </style>

3.元素的垂直水平居中

利用絕對(duì)定位和相對(duì)定位,可以實(shí)現(xiàn)元素在父類中的水平垂直居中

原理是使父元素相對(duì)定位,子元素絕對(duì)定位,給子元素50%定位后利用margin-top和margin-left回移子元素width和height屬性的一半。

<style>
        *{padding: 0;margin: 0}
        .parent{
            width:300px;
            height: 300px;
            background-color: red;
            position: relative;
        }
        .child{
            width: 50px;
            height: 50px;
            position: absolute;
            left: 50%;
            top: 50%;
            background-color: blue;
            margin-top: -25px;
            margin-left: -25%;
        }
    </style>

4.固定定位

固定定位可以使元素不受到滾動(dòng)條的影響,始終固定在某一個(gè)位置

    <style>
        body{
            line-height: 30px;
        }
        div{
            width: 20px;
            height: 50px;
            background: tomato;
            position: fixed;
            right: 10px;
            bottom: 500px;
        }
    </style>

5.Z-index

給元素設(shè)置z-index值可以更改元素覆蓋情況下的優(yōu)先顯示順序

<style>
        .parent{
            width: 300px;
            height: 300px;
            background-color: red;
            position: relative;
        }
        .one{
            width: 100px;
            height: 200px;
            background-color: green;
            position: absolute;
            z-index: 2;
        }
        .two{
            width: 200px;
            height: 100px;
            background-color: blue;
            position: absolute;
            z-index: 3;
        }
        .parent:hover .one{
            z-index:20;
        }
    </style>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,084評(píng)論 1 92
  • 學(xué)會(huì)使用CSS選擇器熟記CSS樣式和外觀屬性熟練掌握CSS各種選擇器熟練掌握CSS各種選擇器熟練掌握CSS三種顯示...
    七彩小鹿閱讀 6,432評(píng)論 2 66
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,756評(píng)論 1 45
  • CSS 是什么 css(Cascading Style Sheets),層疊樣式表,選擇器{屬性:值;屬性:值}h...
    崔敏嫣閱讀 1,564評(píng)論 0 5
  • 1.CSS基本概念 1.1 CSS的定義 CSS(Cascading Style Sheets)層疊樣式表,主要用...
    寥寥十一閱讀 2,037評(píng)論 0 6

友情鏈接更多精彩內(nèi)容