day04 盒子模型 + 居中 + 浮動(dòng) + 定位 + z-index

A今天我學(xué)到了什么

1 盒子模型

1.1 box-sizing:border-box

當(dāng)設(shè)置box-sizing:border-box; 時(shí),
設(shè)置padding,和border,div的寬度還是會(huì)保持不變
box-sizing:content-box;(默認(rèn)清晰)
當(dāng)設(shè)置padding和border時(shí)寬度會(huì)發(fā)生改變
總寬度=width+border+padding
div{
            border:10px solid #333;
            padding:20px;
            box-sizing: border-box;
            width:100px;
            height:100px;
            background-color: red;
        }

1.2 實(shí)現(xiàn)元素居中

margin-left:auto;margin-right:auto;

2 浮動(dòng)float

2.1 float:left | right 可以讓元素并排顯示

<ul>
    <li>手機(jī)</li>
    <li>電視</li>
    <li>平板</li>
</ul>
     li{float: left}

3 清除浮動(dòng)

3.1 給下面的兄弟元素設(shè)置 clear:both;

 .one{
            width:100px;
            height:50px;
            background-color: red;
            float: left;
        }
        .two{
            clear: both;
            width:200px;
            height:50px;
            background-color: yellow;
        }

3.2 給父級(jí)加 overflow:hidden;

  .one{
            width:100px;
            height:100px;
            background:red;
            float: left;
        }
        /*子級(jí)float,父級(jí)沒有高度,如何讓父級(jí)有高度*/
        .parent{
            overflow: hidden;
            width:200px;
            background:yellow;
         }

3.3 父級(jí)后面增加偽元素

.row:after{ content:””;display:table; clear:both;}
.one{
            width:100px;
            height:100px;
            background:red;
            float: left;
        }
        /*子級(jí)float,父級(jí)沒有高度,如何讓父級(jí)有高度*/
        .parent{
            /*overflow: hidden;*/
            width:200px;
            background:yellow;
                  }
        .parent:after{
            content:"";
            display: table;
            clear:both;
        }

4 定位:posintion

4.1 position:absolute | relative

position:relative(相對(duì)定位)
//相對(duì)定位元素的定位是相對(duì)其正常位置。
position:absolute (絕對(duì)定位)
//絕對(duì)定位的元素的位置相對(duì)于最近的已定位父元素,如果沒有已定位的父元素,那么它的位置相對(duì)<html>;
通過設(shè)置 top,right,bottom 移動(dòng)
沒有設(shè)置已定位的父元素,position:absolute 通過left移動(dòng)
   *{margin:0;padding:0}
        /*相對(duì)定位:就是元素在頁(yè)面上正常的位置*/
        .one{
            margin-left:100px;
            width:100px;
            height:100px;
            background-color: red;
        }
        .two{
            width:50px;
            height:50px;
            background-color: yellow;
            position: absolute;
            left:0;
           }
利用float和display實(shí)現(xiàn)導(dǎo)航
li{float: left}
        a{
            text-decoration: none;
            display: block;
            width: 150px;
            height: 50px;
            background-color: red;
        }
        ul{
            list-style: none;
            background-color: chartreuse;
            text-align: center;
            line-height: 50px;
        }
        ul:after{
            content: "";
            display: table;
            clear: both;
        }
        a:hover{
            background-color: aquamarine;
        }

4.2 z-index

z-index:設(shè)置元素的堆疊順序 
堆疊必須必須在父級(jí)上!!!!!
給position:absolute絕對(duì)定位的元素
  /*z-index:可以給絕對(duì)定位的元素,設(shè)置它們的堆疊順序*/
      <style>
        .box{
            width: 900px;
            height: 900px;
            background-color: lawngreen;
            position: relative;
            z-index: 1;
        }
        .one{
            z-index: 2;
            width: 500px;
            height: 500px;
            background-color: red;
            position: absolute;
            top: 200px;
            left: 200px;
        }
        .two{
            z-index: 3;
            width: 300px;
            height: 300px;
            background-color: blue;
            position: absolute;
            top: 200px;
            left: 200px;
        }
        .box:hover .two{
            background-color: darkorchid;
            z-index: 0;
        }
    </style>

<div class="box">
    <div class="one"></div>
    <div class="two"></div>
</div>

4.3 用position實(shí)現(xiàn)搜索框

div{
            margin: 100px;
            width: 300px;
            position: absolute;
        }
        input{
            width: 300px;
            height: 30px;
            background-color: #eeeeee;
            border-radius: 15px;
            border: none;
            outline: none;
            padding-left: 10px;
        }
        button{
            width: 23px;
            height: 22px;
            background-image: url("images/icon4.png");
            position: absolute;
            right: 0;
            top: 50%;
            margin-top: -11px;
            border: none;
            outline: none;
        }

4.4 布局方式的總結(jié)

1.默認(rèn)布局
2.浮動(dòng)布局(左右安置)
3.層級(jí)布局(定位)

5 實(shí)現(xiàn)元素的垂直水平居中

 .box{
            width: 500px;
            height: 500px;
            background-color: red;
            position: relative;
        }
        .one{
            width: 200px;
            height: 200px;
            background-color: yellow;
            position: absolute;
            top:50%;
            left: 50%;
            margin-top: -100px;
            margin-left: -100px;
        }

6 CSS樣式的幾種引入方式

6.1 外部樣式

<link rel="stylesheet" type="text/css" href="/c5.css">

6.2 內(nèi)部樣式表(位于 <head> 標(biāo)簽內(nèi)部)

<style>
p{color:pink;font-size:16px}
</style>

6.3 內(nèi)聯(lián)樣式(在 HTML 元素內(nèi)部)

<p style=”color:pink;font-size:16px”>hello world</p>
樣式的優(yōu)先級(jí)
內(nèi)聯(lián)樣式 > 內(nèi)部樣式 > 外部樣式
以后統(tǒng)一使用外聯(lián)樣式
最后編輯于
?著作權(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,093評(píng)論 1 92
  • 學(xué)習(xí)建議 定位、浮動(dòng)是 CSS 核心知識(shí)點(diǎn),必須熟練掌握。 1.文檔流的概念指什么?有哪種方式可以讓元素脫離文檔流...
    饑人谷_任磊閱讀 1,156評(píng)論 0 3
  • 一、文檔流的概念指什么?有哪種方式可以讓元素脫離文檔流? 1、文檔流指的是元素在排列布局中所占用的位置,具體的說是...
    鴻鵠飛天閱讀 840評(píng)論 0 0
  • 窮人思維和富人思維的最大區(qū)別是什么?我認(rèn)為,一個(gè)是固化思維,一個(gè)是成長(zhǎng)思維。吳伯凡老師在《伯凡日志錄》里說的“給予...
    芒曼閱讀 612評(píng)論 0 0
  • 下午有空,在西湖邊逛
    行攝在路上閱讀 268評(píng)論 0 0

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