CSS入門(5.2)

定位

絕對定位
position:absolute
相對定位
position:relative
區(qū)別:定位就好比圖層中新開了一個層 讓定位元素在這個新開的圖層上

  • 相對定位
    就是 雖然新建了一個層 但是原本層所占據(jù)的位置是不被騰出的 就是你占據(jù)不了我 我走了也不給你
  • 絕對定位
    就顯得大方一點(diǎn) 我走了 我去另外一個層去了 那么我原來那個層的位置留給你們(騰出) 未被定位的元素就會去占據(jù)

(形象了一些 話糙理不糙)

屬性:
left/right ???????? ???????? ??? top/bottom
基準(zhǔn)線是不一樣
top 以上邊為基準(zhǔn)線 ??? bottom 以下邊為基準(zhǔn)線
left 以左邊為基準(zhǔn)線 ?????right 以右邊為基準(zhǔn)線
left/right 只能取其一 ??? top/bottom 只能取其一
只要是定位元素 都可以跟

<style type="text/css">
    .box1{
        width: 400px;
        height: 400px;
        border: 1px solid #000;
        margin: 100px 0 0 100px;
    }
    .box1 .box2{
        position: relative;
        width: 200px;
        height: 200px;
        background-color: snow;
    }
    .box1 .box2 .box3{
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100px;
        height: 100px;
        background-color:orangered;
    }
</style>
<body>
<div class="box1">
    <div class="box2">
        <div class="box3">
        </div>
    </div>
</div>
</body>

定位元素的相對性
是看他的父級元素
是往上找 看他的哪個父級元素有定位
如果找父級元素 找到最上層 都沒有定位 那就相對于html 整個文檔 去定位
那要是父級都有定位 那上一層父級元素也有定位 就相對于上一層父級元素定位 就不再往上找
這個往上找相對性 我只要找到有了 哪一個父級有了 就不再往上找

絕對定位(absolute)總結(jié):
相對于最近的 有定位的父元素絕對定位
如果往上找 并沒有找到 定位的父元素那么就相對html文檔進(jìn)行絕對定位

但是下面這種
相對定位

<style type="text/css">
  .box1{
      width: 400px;
      height: 400px;
      border: 1px solid #000;
      margin: 100px 0 0 100px;
  }
  .box1 .box2{
      width: 200px;
      height: 200px;
      background-color: snow;
  }
  .box1 .box2 .box3{
      position: relative;
      top: 10px;
      left: 10px;
      width: 100px;
      height: 100px;
      background-color:orangered;
  }
</style>
<body>
<div class="box1">
  <div class="box2">
      <div class="box3">
      </div>
  </div>
</div>
</body>
1.png

相對定位是相對于它本身定位的 它right和bottom找不找(所以設(shè)置也不起作用) 只能找得找 左側(cè)和上側(cè)
相對定位(relative)一般不會這樣定位

<style type="text/css">
  .box{
      position: relative;
      width: 400px;
      height: 400px;
      background-color: orangered;
      margin: 100px 0 0 100px;
  }
  .box .box2{
      position: absolute;
      bottom: 10px;
      right: 20px;
      width: 200px;
      height: 200px;
      background-color: snow;
  }

</style>
<body>
<div class="box">
  <div class="box2">
  </div>
</div>
</body>  

一般定位 都這樣寫 相對于父級 進(jìn)行絕對定位

relative一般有兩個作用

  1. position:relative
    就是把父級上面做了一個相對定位 子級向上找時(shí)就可以找到這個父級進(jìn)行絕對定位
  2. position:relative
    box1盒子絕對定位 box2盒子被蓋住 加上這個屬性 就可以使box2在最上層
<style type="text/css">
    .box1{

        width: 100px;
        height: 100px;
        background-color: orangered;
    }
    .box2{
        width: 100px;
        height: 100px;
        background-color: snow;
    }

</style>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
</body>
零.png

最初

<style type="text/css">
    .box1{
        position: absolute;
        width: 100px;
        height: 100px;
        background-color: orangered;
    }
    .box2{
        width: 100px;
        height: 100px;
        background-color: snow;
    }

</style>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
一.png

非定位的盒子box2被box1遮灼嗬簟(box1絕對定位 被騰出 box2上去占據(jù) 所以 會被蓋渍浯佟)

<style type="text/css">
    .box1{
        position: absolute;
        width: 100px;
        height: 100px;
        background-color: orangered;
    }
    .box2{
        width: 100px;
        height: 100px;
        background-color: snow;
    }

</style>
<body>
<div class="box2">
</div>
<div class="box1">
</div>

</body>
二.png

這個box1是絕對定位 但是box1 上面 還有box2 這個非定位的盒子 (box2就在那)

<style type="text/css">
    .box1{
        position: absolute;
        width: 100px;
        height: 100px;
        background-color: orangered;
    }
    .box2{
        position: relative;
        width: 100px;
        height: 100px;
        background-color: snow;
    }

</style>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
</body>
三.png

position:relative 就使box2上來

z-index z軸的排列

1-999 越大 越靠前(越在上面)
Z軸 朝向我眼睛的軸

<style type="text/css">
    .box1{
        position: absolute;
        width: 100px;
        height: 100px;
        background-color: orangered;
    }
    .box2{
        position: absolute;
        width: 100px;
        height: 100px;
        background-color: snow;
    }

</style>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
</body>

這個 都是絕對定位 先來的在下面 后來的在上面 所以是snow顏色

<style type="text/css">
    .box1{
        position: absolute;
        z-index: 1;
        width: 100px;
        height: 100px;
        background-color: orangered;
    }
    .box2{
        position: absolute;
        width: 100px;
        height: 100px;
        background-color: snow;
    }

</style>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
</body>

絕對定位的兩欄設(shè)計(jì)

這個是左側(cè)寬度固定 右側(cè)根據(jù)瀏覽器寬度自適應(yīng)

<style type="text/css">
    html,
    body{
        height: 100%;
        margin: 0;
        overflow-y: hidden;
    }
    .left{
        position: absolute;
        left: 0;
        top: 0;
        width: 300px;
        height: 100%;
        background-color: orangered;
    }
    .right{
        margin-left: 300px;
        height: 100%;
        background-color: orange;
    }

</style>
<body>
<div class="left">
</div>
<div class="right">
</div>
</body>

左右調(diào)換

這個是右側(cè)寬度固定 左側(cè)根據(jù)瀏覽器寬度自適應(yīng)

<style type="text/css">
    html,
    body{
        height: 100%;
        margin: 0;
        overflow-y: hidden;
    }
    .left{
        margin-right: 300px;
        height: 100%;
        background-color: orangered;
    }
    .right{
        position: absolute;
        right: 0;
        top: 0;
        width: 300px;
        height: 100%;
        background-color: orange;
    }

</style>
<body>
<div class="left">
</div>
<div class="right">
</div>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市垦细,隨后出現(xiàn)的幾起案子如蚜,更是在濱河造成了極大的恐慌压恒,老刑警劉巖影暴,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異探赫,居然都是意外死亡型宙,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進(jìn)店門伦吠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來妆兑,“玉大人,你說我怎么就攤上這事毛仪〖” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵潭千,是天一觀的道長谱姓。 經(jīng)常有香客問我,道長刨晴,這世上最難降的妖魔是什么屉来? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮狈癞,結(jié)果婚禮上茄靠,老公的妹妹穿的比我還像新娘。我一直安慰自己蝶桶,他們只是感情好慨绳,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著真竖,像睡著了一般脐雪。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上恢共,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天战秋,我揣著相機(jī)與錄音,去河邊找鬼讨韭。 笑死脂信,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的透硝。 我是一名探鬼主播狰闪,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼濒生!你這毒婦竟也來了埋泵?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤甜攀,失蹤者是張志新(化名)和其女友劉穎秋泄,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體规阀,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡恒序,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了谁撼。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片歧胁。...
    茶點(diǎn)故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖厉碟,靈堂內(nèi)的尸體忽然破棺而出喊巍,到底是詐尸還是另有隱情,我是刑警寧澤箍鼓,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布崭参,位于F島的核電站,受9級特大地震影響款咖,放射性物質(zhì)發(fā)生泄漏何暮。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一铐殃、第九天 我趴在偏房一處隱蔽的房頂上張望海洼。 院中可真熱鬧,春花似錦富腊、人聲如沸坏逢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽是整。三九已至,卻和暖如春民假,著一層夾襖步出監(jiān)牢的瞬間贰盗,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工阳欲, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留舵盈,地道東北人。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓球化,卻偏偏與公主長得像秽晚,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子筒愚,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評論 2 355

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