1. 程式人生 > >css display:box 佈局 css display:box 新屬性

css display:box 佈局 css display:box 新屬性

css display:box 新屬性

 

一、display:box;

  在元素上設定該屬性,可使其子代排列在同一水平上,類似display:inline-block;。

二、可在其子代設定如下屬性

  前提:使用如下屬性,必須在父代設定display:box;

  1.box-flex:number;

    1)佔父級元素寬度的number份

    2)若子元素設定固定寬度,則該子元素應用固定寬度,其他未設定固定寬度的字元素將餘下的父級寬度(父級-已設定固定寬度的子代元素的總寬度按 number佔份數

    3)若子元素有margin值,則按餘下(父級寬度-子代固定總寬度-總margin值)寬度佔number份

  2.box-orient:horizontal/vertical

    在父級上設定該屬性,則子代按水平排列或豎直排列。

    注:所有主流瀏覽器不支援該屬性,必須加上字首。

    1)horizontal  水平排列,子代總width=父級width。若父級固定寬度,則子代設定的width無效,子代會撐滿父級寬度。

    2)vertical  垂直排列,子代總height=父級height。若父級固定高度,則子代設定的height無效,子代會撐滿父級高度。

  3.box-direction:normal/reverse

    在父級上設定該屬性,確認子代的排列順序。

    1)normal  預設值,子代按html順序排列

    2)reverse  反序

  4.box-align:start/end/center/stretch

    在父級設定,子代的垂直對齊方式。

    1)start  垂直頂部對齊

    2)end 垂直底部對齊

    3)center 垂直居中對齊

    4)stretch 拉伸子代的高度,與父級設定的高度一致。子代height無效。

  5.box-pack:start/end/center

    在父級設定,子代的水平對齊方式。

    1)start  水平左對齊

    2)end  水平右對齊

    3)center  水平居中對齊

三、例項

<article class="wrap">
        <section class="sectionOne">01</section>
        <section class="sectionTwo">02</section>
        <section class="sectionThree">03</section>
</article>
.wrap{
    width:600px;
    height:200px;
    display:-moz-box;
    display:-webkit-box;
    display:box;
}
.sectionOne{
    background:orange;
    -moz-box-flex:3;        /*(600-200)/4 *3 = 300px */
    -webkit-box-flex:3;
    box-flex:3;
}
.sectionTwo{
    background:purple;
    -moz-box-flex:1;        /*(600-200)/4 *1 = 100px  */
    -webkit-box-flex:1;
    box-flex:1;
}
.sectionThree{
    width:200px;            /* 設定固定寬度 */
    background:green;
}

效果:

一、display:box;

  在元素上設定該屬性,可使其子代排列在同一水平上,類似display:inline-block;。

二、可在其子代設定如下屬性

  前提:使用如下屬性,必須在父代設定display:box;

  1.box-flex:number;

    1)佔父級元素寬度的number份

    2)若子元素設定固定寬度,則該子元素應用固定寬度,其他未設定固定寬度的字元素將餘下的父級寬度(父級-已設定固定寬度的子代元素的總寬度按 number佔份數

    3)若子元素有margin值,則按餘下(父級寬度-子代固定總寬度-總margin值)寬度佔number份

  2.box-orient:horizontal/vertical

    在父級上設定該屬性,則子代按水平排列或豎直排列。

    注:所有主流瀏覽器不支援該屬性,必須加上字首。

    1)horizontal  水平排列,子代總width=父級width。若父級固定寬度,則子代設定的width無效,子代會撐滿父級寬度。

    2)vertical  垂直排列,子代總height=父級height。若父級固定高度,則子代設定的height無效,子代會撐滿父級高度。

  3.box-direction:normal/reverse

    在父級上設定該屬性,確認子代的排列順序。

    1)normal  預設值,子代按html順序排列

    2)reverse  反序

  4.box-align:start/end/center/stretch

    在父級設定,子代的垂直對齊方式。

    1)start  垂直頂部對齊

    2)end 垂直底部對齊

    3)center 垂直居中對齊

    4)stretch 拉伸子代的高度,與父級設定的高度一致。子代height無效。

  5.box-pack:start/end/center

    在父級設定,子代的水平對齊方式。

    1)start  水平左對齊

    2)end  水平右對齊

    3)center  水平居中對齊

三、例項

<article class="wrap">
        <section class="sectionOne">01</section>
        <section class="sectionTwo">02</section>
        <section class="sectionThree">03</section>
</article>
.wrap{
    width:600px;
    height:200px;
    display:-moz-box;
    display:-webkit-box;
    display:box;
}
.sectionOne{
    background:orange;
    -moz-box-flex:3;        /*(600-200)/4 *3 = 300px */
    -webkit-box-flex:3;
    box-flex:3;
}
.sectionTwo{
    background:purple;
    -moz-box-flex:1;        /*(600-200)/4 *1 = 100px  */
    -webkit-box-flex:1;
    box-flex:1;
}
.sectionThree{
    width:200px;            /* 設定固定寬度 */
    background:green;
}

效果: