1. 程式人生 > 實用技巧 >WEB前端第十三課——瀏覽器核心、位置

WEB前端第十三課——瀏覽器核心、位置

1.瀏覽器核心及字首

  在CSS中新的屬性標準尚未明確的情況下,各瀏覽器廠商對新屬性的支援情況也不相同,這個階段會對屬性加廠商字首進行區分。

  根據不同的瀏覽器核心,CSS字首有所不同,最基本的瀏覽器核心有四種,其他核心都是基於此四種進行再研發的。

  ①Gecko核心,字首為“-moz-”,火狐瀏覽器

  ②Webkit核心,字首是“-webkit-”,也叫谷歌核心,Chrome瀏覽器最先開發使用,Safari瀏覽器也使用該核心

         目前,國內很多瀏覽器也使用了webkit核心,如360極速、世界之窗、獵豹等

  ③Trident核心,字首為“-ms-”,也稱 IE核心

  ④Presto核心,字首是“-o-”,目前只有Opera使用

  程式碼例項

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS瀏覽器核心</title>
    <style>
        div {
            width: 500px;
            height: 500px;
            background-color:orange;
            border: 36px solid rgba(255,255,255,0.5);
            margin: 60px auto;
            padding: 50px;
            font-size: 200px;
            font-weight: bolder;
            color: rgba(255,255,255,0.5);
            text-align: center;
            -webkit-background-clip: content-box;
            /*屬性值:
            border-box,從邊框開始實現背景
            padding-box,從padding開始實現背景
            content-box,從內容開始實現背景
            text,從文字開始實現背景
       為適應不同瀏覽器,需要同時設定帶有不同瀏覽器核心字首的屬性 */ } </style> </head> <body> <div>馬可波羅</div> </body> </html>

2.定位,定義元素相對於正常位置應該出現的位置

  分類:

    ⑴ 普通流定位,Normal

    ⑵ 浮動定位

      將元素排除在普通流之外,浮動元素不在頁面中佔據空間,可以放置在包含框的左邊或者右邊,浮動元素依舊位於包含框內

      浮動元素框可以向左或向右移動,直到外邊緣碰到包含框或另一個浮動框的邊框

      浮動元素外邊緣不會超過其父元素的內邊緣

      浮動元素不會互相重疊、不會上下浮動

      行內元素浮動後會變為塊級元素

      語法:float: none / left / right;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS浮動定位</title>
    <style>
        div {
            width: 200px;
            height: 200px;
        }
        div.one{ background-color: pink; float: right;}
        div.two{ background-color: hotpink;}
        div.three{ background-color: deeppink;}
    </style>
</head>
<body>
    <div class="one" align="center">1</div>
    <div class="two" align="center">2</div>
    <div class="three" align="center">3</div>
</body>
</html>

      清除浮動,是在使用了浮動之後必不可少的,為了網站佈局的效果清除浮動也變的非常麻煩

        屬性clear,屬性值:left(清除左浮動)、right(清除右浮動)、both(全部清除)

        常用清除方式:①結尾處加空div標籤clear:both,或在下一個元素前加clear:both②浮動元素的父元素定義偽元素“ ::after”③浮動元素的父元素定義“overflow: hidden”④浮動元素的父元素定高

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS浮動定位</title>
    <style>
        div.parent{ border:2px solid red; overflow: hidden}  /*第三種方式:overflow:hidden 或 display:inline-block 或 display:table-cell */
        div.one{ width: 150px; height: 150px; background-color: pink; float: left;}
        div.two{ width: 150px; height: 150px; background-color: hotpink; float: left;}
        div.three{ width: 150px; height: 150px; background-color: deeppink; float: left;}
        /*div.clear{ clear: both;}  第一種方式*/
        /*div.parent::after{
            content: "";
            display: block;
            clear: both;
        }                           第二種方式*/
    </style>
</head>
<body>
    <div class="parent">
        <div class="one" align="center">1</div>
        <div class="two" align="center">2</div>
<!--    <div class="clear"></div>   第一種方式-->
    </div>
    <div class="three" align="center">3</div>
</body>
</html>

    ⑶ 相對定位

    ⑷ 絕對定位

    ⑸ 固定定位

3.display屬性

  根據CSS規範,每一個網頁元素都有一個display屬性,用於確定該元素的型別

  每一個元素都有預設的display屬性值,比如 div的預設display屬性值為“block”(塊級元素),而span的預設display屬性值為“inline”(行內元素)

  塊級元素與行內元素是可以轉換的,即display屬性值可以設定修改

  display常用屬性值:

    none,隱藏物件,且不佔據空間

    inline,指定物件為內聯元素(行內元素)

    block,指定物件為塊級元素

    inline-block,指定物件為內聯塊級元素,可以設定寬高、margin、padding,但會識別程式碼之間的空白!

    table-cell,指定物件作為表格的單元格

    flex,指定物件為彈性盒子

  另外,visibility:hidden和opacity:0也會隱藏物件,但仍然佔據物理空間

4.position定位,指定一個元素定位方法的型別

  屬性值:① static,預設值,元素顯示在正常的內容流中,忽略top、bottom、left、right偏移量及z-index宣告,一般用於去除定位

      ②relative,生成相對定位的元素,相對於其正常位置進行定位,因此,left:20px會向元素的left位置新增20畫素,但其正常位置依然佔據

      ③fixed,生成固定定位的元素,一旦生成其原始位置不再佔據,且不隨瀏覽器介面滾動而改變,元素的位置通過top、right、bottom、left屬性值進行規定

      ④absolute,生成絕對定位的元素,相對於瀏覽器視窗進行偏移,一旦發生偏移,原正常位置不再佔有,元素的位置通過top、right、bottom、left屬性值進行規定

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS定位</title>
    <style>
        div{
            width: 200px;
            height: 200px;
            background-color: blue;
            left: 50px;
            top: 30px;
            /*position: absolute;*/
            /*position: relative;*/
            position: fixed;
        }
        pre{font-size: 130px;}
    </style>
</head>
<body>
    <div></div>
    <pre>文字內容</pre>
    <pre>文字內容</pre>
    <pre>文字內容</pre>
    <pre>文字內容</pre>
    <pre>文字內容</pre>
</body>
</html>

5.常用居中方式小結

  文字內容水平居中,text-align:center

  行內文字垂直居中,line-height=height

  盒子水平居中,margin:0px center

  子元素在父元素內居中,①父元素定義為彈性盒子,display:flex; align-items:center; justify-content:center;

             ②定義父元素為單元格,display:table-cell; vertical-align:middle; 子元素中定義水平居中,margin: 0 auto;

             ③ 定義父元素為相對位置,position:relative; 定義子元素為絕對位置,position:absolute; left:25%; top:25%;

6.z-index堆疊順序

  一旦修改了元素的定位方式,在元素可能發生堆疊,使用 z-index屬性可以控制元素重疊的順序

  z-index屬性:

    ① z-index僅能在定位的元素上生效

    ②z-index屬性值為數值,數值越大表示堆疊越高,即離使用者越近

    ③可以設定為負值,表示離使用者更遠,一般不建議設定

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS定位</title>
    <style>
        div{
            width: 200px;
            height: 200px;
            background-color: blue;
            left: 100px;
            top: 100px;
            /*position: absolute;*/
            position: relative;
            /*position: fixed;*/
        }
        div:first-child{
            background-color: deeppink;
            left: 0px;
            top: 0px;
            z-index: 2;
        }
        div:last-child{
            background-color: darkorange;
            left: 200px;
            top: 200px;
            z-index: 3;
        }
        div:nth-child(2){
            z-index: 1;
        }
    </style>
</head>
<body>
    <div></div>
    <div></div>
    <div></div>
</body>
</html>