flex布局

flex布局在PC端可能兼容性堪忧,但是在移动端可以做兼容性处理,Android2.1起开始支持旧语法,iOS safari从3.2开始支持旧语法。

兼容性测试报告

Android 4.4+ 与ios9+ 支持新语法,不需要做兼容性即可使用。表中查阅可知Android 2.1 - Android 4.3 部分支持旧语法。需使用带有-webkit-前缀的旧语法;支持主轴方向设置、支持主轴对齐、支持侧轴对齐、支持设置显示顺序。不支持换行(box-lines属性无效),即只能做单行。支持box-flex属性,但是因为没有flex-basis属性,需要手动指定width: 0%后可以实现等分。总结:虽然部分功能缺失,但水平垂直居中与等分功能已经可以实现。

代码实现:

/* 父元素-flex容器 */

.flex {

    display: -webkit-box;      /* OLD - iOS 6-, Safari 3.1-6 */

    display: -moz-box;        /* OLD - Firefox 19- (buggy but mostly works) */

    display: -ms-flexbox;      /* TWEENER - IE 10 */

    display: -webkit-flex;    /* NEW - Chrome */

    display: flex;            /* NEW, Spec - Opera 12.1, Firefox 20+ */

}

/* 子元素-等分 */

.flex1 {

    -webkit-box-flex: 1;      /* OLD - iOS 6-, Safari 3.1-6 */

    -moz-box-flex: 1;        /* OLD - Firefox 19- */

    -webkit-flex: 1;          /* Chrome */

    -ms-flex: 1;              /* IE 10 */

    flex: 1;                  /* NEW, Spec - Opera 12.1, Firefox 20+ */

}

/* 父元素-横向排列(主轴) */

.flex-h {

    /* 09版 */

    -webkit-box-orient: horizontal;

    /* 12版 */

    -webkit-flex-direction: row;

    -moz-flex-direction: row;

    -ms-flex-direction: row;

    -o-flex-direction: row;

    flex-direction: row;

}

/* 父元素-换行 */

.flex-hw {

    /* 09版 */

    /*-webkit-box-lines: multiple;*/ /* 这个没有效果,W3C上也不建议使用 */

    /* 12版 */

    -webkit-flex-wrap: wrap;

    -moz-flex-wrap: wrap;

    -ms-flex-wrap: wrap;

    -o-flex-wrap: wrap;

    flex-wrap: wrap;

}

/* 父元素-主轴方向居中 */

.flex-hc {

    /* 09版 */

    -webkit-box-pack: center;

    /* 12版 */

    -webkit-justify-content: center;

    -moz-justify-content: center;

    -ms-justify-content: center;

    -o-justify-content: center;

    justify-content: center;

    /* 其它取值如下:

        align-items    主轴原点方向对齐

        flex-end        主轴延伸方向对齐

        space-between  等间距排列,首尾不留白

        space-around    等间距排列,首尾留白

    */

}

/* 父元素-主轴space-between */

.flex-hsbt {

    /* 09版 */

    -webkit-box-pack: justify;

    /* 12版 */

    -webkit-justify-content: space-between;

    -moz-justify-content: space-between;

    -ms-justify-content: space-between;

    -o-justify-content: space-between;

    justify-content: space-between;

    /* 其它取值如下:

        align-items    主轴原点方向对齐

        flex-end        主轴延伸方向对齐

        space-between  等间距排列,首尾不留白

        space-around    等间距排列,首尾留白

    */

}

/* 父元素-纵向排列(主轴) */

.flex-v {

    /* 09版 */

    -webkit-box-orient: vertical;

    /* 12版 */

    -webkit-flex-direction: column;

    -moz-flex-direction: column;

    -ms-flex-direction: column;

    -o-flex-direction: column;

    flex-direction: column;

}

/* 父元素-侧轴居中) */

.flex-vc {

    /* 09版 */

    -webkit-box-align: center;

    /* 12版 */

    -webkit-align-items: center;

    -moz-align-items: center;

    -ms-align-items: center;

    -o-align-items: center;

    align-items: center;

}

/* 子元素-显示在从左向右(从上向下)第1个位置,用于改变源文档顺序显示 */

.flex-1 {

    -webkit-box-ordinal-group: 1;  /* OLD - iOS 6-, Safari 3.1-6 */

    -moz-box-ordinal-group: 1;      /* OLD - Firefox 19- */

    -ms-flex-order: 1;              /* TWEENER - IE 10 */

    -webkit-order: 1;              /* NEW - Chrome */

    order: 1;                      /* NEW, Spec - Opera 12.1, Firefox 20+ */

}

/* 子元素-显示在从左向右(从上向下)第2个位置,用于改变源文档顺序显示 */

.flex-2 {

    -webkit-box-ordinal-group: 2;  /* OLD - iOS 6-, Safari 3.1-6 */

    -moz-box-ordinal-group: 2;      /* OLD - Firefox 19- */

    -ms-flex-order: 2;              /* TWEENER - IE 10 */

    -webkit-order: 2;              /* NEW - Chrome */

    order: 2;                      /* NEW, Spec - Opera 12.1, Firefox 20+ */

}

你可能感兴趣的:(flex布局)