position定位干貨

定位

  • Position-設(shè)置定位方式
  • top,right,bottom,left,z-index --設(shè)置位置

top right bottom left

image

元素邊緣距參照物的邊緣的距離

Demo:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>位置</title>
    <style>
        .sample{background-color: pink;}
        .sample{position: absolute;}

        /*.sample{top: 30px;left: 30px;}*/ 注釋掉以后會(huì)距上邊30px 距左邊30px
        /*.sample{bottom: 30px;right: 30px;}*/注釋掉以后div會(huì)被撐開(kāi)
    </style>
</head>
<body>
    <div class="sample">sample</div>
</body>
</html>

z-index

image

z軸上的排序:默認(rèn)為z-index:0; 正值越大,在z軸上越在上面,在下面的會(huì)被覆蓋.

z-index:-value;值可為負(fù)值

是不是值越大約在上面呢?不一定

z-index棧

image

上圖有兩個(gè)定位元素,如果給z-index:1;元素這個(gè)定位,父元素設(shè)為z-index:9; z-index:100;的元素的祖先元素設(shè)為z-index:1; z-index紅色元素蓋在了綠色元素上

Demo:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>z-index</title>
    <style>
        .sample0, .sample1{position: absolute;width: 200px;line-height: 150px;text-align: center;}
        .sample0{background-color: #ff0;}
        .sample1{background-color: pink;}

        .sample1{top: 100px;left: 100px;}正常情況下的排列是按照元素在文檔流中的位置排的
            
        /*.sample0{z-index: 9;}*/    會(huì)在上面

        /*.container0, .container1{position: relative;}*/
        /*.container1{z-index: 99;}*/    
    </style>
</head>
<body>
    <div class="container0"><div class="sample0">sample 0</div></div>
    <div class="container1"><div class="sample1">sample 1</div></div>
</body>
</html>

position

  • position: static | relative | absolute | fixed

position:relative

  • 仍在文檔流中
  • 參照物為元素本身
  • 可以改變z軸上的層級(jí)
  • 使用場(chǎng)景:絕對(duì)定位元素的參照物
image
Demo:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>相對(duì)定位</title>
    <style>
        .container{width: 400px;line-height: 2;border: 1px dashed #aaa;}
        .sample{background-color: pink;}

        .sample{position: relative;}
        .sample{top: 10px;left: 30px;}
    </style>
</head>
<body>
    <div class="container">
        <div>相對(duì)定位元素的前序元素</div>
        <div class="sample">sample</div>
        <div>相對(duì)定位元素的后序元素</div>
    </div>
</body>
</html>

position:absolute

  • 默認(rèn)寬度為內(nèi)容寬度
  • 脫離文檔流(浮起來(lái)了)
  • 參照物為第一個(gè)定位祖先/根元素
  • 使用場(chǎng)景:輪播頭圖

輪播頭圖定位靜態(tài)實(shí)現(xiàn)

Demo:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>絕對(duì)定位</title>
    <style>
        .container{width: 300px;margin: 50px;line-height: 2;border: 1px dashed #aaa;}
        .sample{background-color: pink;}

        /*.sample{position: absolute;}*/
        /*.sample{bottom: 10px;left: -30px;}*/
        /*.container{position: relative;}*/
    </style>
</head>
<body>
    <div class="container">
        <div>絕對(duì)定位元素的前序元素</div>
        <div class="sample">sample</div>
        <div>絕對(duì)定位元素的后序元素</div>
    </div>
</body>
</html>

position:fixed(固定定位)

  • 默認(rèn)寬度為內(nèi)容寬度
  • 脫離文檔流
  • 參照物為視窗
  • 使用場(chǎng)景:固定頂欄 遮罩
Demo:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>fixed定位</title>
    <style>
        .container{width: 400px;margin: 200px;line-height: 2;border: 1px dashed #aaa;}
        .sample{background-color: pink;}
        
        /*.sample{position: fixed;}*/    脫離文檔流
        /*.sample{bottom: 0;left: 10px;}*/   相對(duì)于視窗定位
        /*.container{height: 1000px;}   */
    </style>
</head>
<body>
    <div class="container">
        <div>絕對(duì)定位元素的前序元素</div>
        <div class="sample">sample</div>
        <div>絕對(duì)定位元素的后序元素</div>
    </div>
</body>
</html>
遮罩

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>遮罩</title>
    <style>
        .mask{
            position: fixed;
            top: 0;
            left: 0;
            z-index: 999;
            width: 100%;
            height: 100%;
            background-color: #000;
            opacity: 0.3;
        }
        .content{
            height: 3000px;
        }
    </style>
</head>
<body>
    <div class="mask"></div>
    <div class="content">content area</div>
</body>
</html>
固定頂欄

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>固定頂欄</title>
    <style>
        body{margin: 0;line-height: 1.8;}
        .top{background-color: pink;color: #fff;}
        .main{height: 3000px;background-color: #eee;}


        body{padding-top: 50px;}
        .top{position: fixed;top: 0;width: 100%;height: 50px;}
    </style>
</head>
<body>
    <div class="top">top bar</div>
    <div class="main">main content area</div>
</body>
</html>

使用定位怎么做布局?看個(gè)DEMO

三行自適應(yīng)布局

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>三行自適應(yīng)布局</title>
    <style>
        .head{
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100px;
            background-color: #ccc;
        }
        .body{
            position: absolute;
            top: 100px;
            left: 0;
            bottom: 100px;
            right: 0;
            overflow: auto;
        }
        .content{
            height: 2000px;
        }
        .foot{
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 100px;
            background-color: #ccc;
        }
    </style>
</head>
<body>
    <div class="head">head</div>
    <div class="body">
      <div class="content">content area</div>
    </div>
    <div class="foot">foot</div>
</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 問(wèn)答題47 /72 常見(jiàn)瀏覽器兼容性問(wèn)題與解決方案? 參考答案 (1)瀏覽器兼容問(wèn)題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 13,796評(píng)論 1 92
  • 1.浮動(dòng)元素有什么特征?對(duì)父容器、其他浮動(dòng)元素、普通元素、文字分別有什么影響? 浮動(dòng)元素 浮動(dòng)元素是設(shè)置float...
    Volcaner閱讀 361評(píng)論 0 0
  • 學(xué)習(xí)建議 定位、浮動(dòng)是 CSS 核心知識(shí)點(diǎn),必須熟練掌握。 1.文檔流的概念指什么?有哪種方式可以讓元素脫離文檔流...
    饑人谷_任磊閱讀 1,120評(píng)論 0 3
  • position屬性比起其他的基礎(chǔ)屬性來(lái)講要復(fù)雜一些,我在這試著把里面的門道全部總結(jié)出來(lái)。 目前position有...
    microkof閱讀 3,662評(píng)論 3 5
  • 1、0x8badf00d: 讀做 “ate bad food”! (把數(shù)字換成字母,是不是很像 :p)該編碼表示應(yīng)...
    落葉悠悠閱讀 906評(píng)論 0 0