flex布局总结

"常见flex的语法及用处"

Posted by ZWW on October 11, 2015

“关于flex的文章 ”

开始前复习一下最新看苏宁和京东用到的侧边栏导航的实现方式:

        position:fixed top left +margin-left
        position:fixed top right+margin-right 实现侧边栏不随屏幕大小而滚动。

新浪是这样的:

.W_gotop {
		position: fixed;
		left: 50%;
		margin-left: 476px; /*这个显然经过了计算,且受限于容器宽度值*/
	bottom: 100px;
		}

如果对absolute绝对定位有更深的理解还有一种更巧妙的办法,没有应用left/top等属性值的absolute元素就是个不占据空间的普通元素,如果是block水平的,换行显示;如果是inline水平的,跟在前面的文字后面显示

   <div class="sina_box">
    <div class="sina_backtop_box">
        &nbsp;<a href="#" class="sina_backtop">返回顶部</a>
    </div>
</div>
        .sina_box {
        width: 960px;
        height: 2000px;
        margin: 0 auto;
        background: url(http://img1.t.sinajs.cn/t4/skin/skin000/images/body_bg.jpg) top center;
    }
    
    .sina_backtop_box {
        text-align: right;
    }
    
    .sina_backtop {
        width: 20px;
        padding: 5px 0;
        background-color: #DFF3FD;
        text-align: center;
        position: fixed;
        bottom: 100px;
    }
    .sina_backtop_box a{
        text-decoration: none;
        color: black;
    }

添加空格是为了兼容ie。text-align:center会作用于inline-inline-block元素,但是text-align属性对应用了position:absloute/fixed声明的元素无效!而是absolute元素之前的inline/inline-block水平的元素。

flex正文开始

        float nothing,flex everything~
        概念:Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。 任何一个容器都可以指定为Flex布局。
        块元素:display:flex;行内元素:display:inline-flex;注意,设为Flex布局以后,子元素的float、clear和vertical-align属性将失效。
        以下6个属性设置在容器上。flex-direction
                            flex-wrap
                            flex-flow 包含前面两者
                            justify-content
                            align-items
                            align-content
       
       1,flex-direction: row | row-reverse | column | column-reverse;
            row(默认值):主轴为水平方向,起点在左端。
            row-reverse:主轴为水平方向,起点在右端。
            column:主轴为垂直方向,起点在上沿。
            column-reverse:主轴为垂直方向,起点在下沿。
        2,flex-wrap: nowrap | wrap | wrap-reverse;换行
            flex-wrap属性定义,如果一条轴线排不下,如何换行。nowrap(默认):不换行。wrap:换行,第一行在上方。wrap-reverse:换行,第一行在下方。
        3,flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。
    
        4,justify-content属性定义了项目在主轴上的对齐方式。
    属性值:justify-content: flex-start | flex-end | center | space-between | space-around;
    flex-start(默认值):左对齐
    flex-end:右对齐
    center: 居中
    space-between:两端对齐,项目之间的间隔都相等。
    space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
    
        5,align-items属性定义项目在交叉轴上如何对齐。
            align-items: flex-start | flex-end | center | baseline | stretch;
        
            flex-start:交叉轴的起点对齐。
            flex-end:交叉轴的终点对齐。
            center:交叉轴的中点对齐。
            baseline: 项目的第一行文字的基线对齐。
            stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。    
        6,align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
            flex-start:与交叉轴的起点对齐。
            flex-end:与交叉轴的终点对齐。
            center:与交叉轴的中点对齐。
            space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
            space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
            stretch(默认值):轴线占满整个交叉轴。
    
    
        以下6个属性设置在项目上。
                order
                flex-grow
                flex-shrink
                flex-basis
                flex
                align-self
            order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。
            flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。
            flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
            如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。负值对该属性无效。
            flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。它可以设为跟width或height属性一样的值(比如350px),则项目将占据固定空间。
            flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
            该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。
            建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。
            align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
     align-self: auto | flex-start | flex-end | center | baseline | stretch;
    该属性可能取6个值,除了auto,其他都与align-items属性完全一致。

写了一个demo: http://codepen.io/tianzi77/full/rOLrGL

<body class="flex-wrap col-flex">
<header class="midCenter flex-wrap row-flex">我是标题flex布局</header>

<div class="page flex-wrap col-flex">
    <div class="scroll-wrap">
        <section class="midCenter flex-wrap" id="banner">
            <div>我是banner</div>
        </section>

        <section id="quirk" class="flex-wrap row-flex">
            <div></div>
            <div class="flex-wrap col-flex">
                <div></div>
                <div></div>
            </div>
        </section>

        <section id="four-col" class="flex-wrap row-flex">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </section>

        <section id="two-col" class="flex-wrap row-flex">
            <div></div>
            <div></div>
        </section>

        <section id="three-col" class="flex-wrap row-flex">
            <div></div>
            <div></div>
            <div></div>
        </section>
    </div>
</div>


<footer class="flex-wrap row-flex">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</footer>
</body>

样式:
html{
        height:100%;
    }

    body{
        background: #fff;
        margin:0;
        padding:0;
        width: 100%;
        height: 100%;
    }

    /** 水平伸缩容器**/
    .row-flex{
        -moz-box-orient: horizontal;
        -webkit-box-orient: horizontal;
        -moz-box-direction: normal;
        -webkit-box-direction: normal;
        -moz-box-lines: multiple;
        -webkit-box-lines: multiple;
        -ms-flex-flow: row wrap;
        -webkit-flex-flow: row wrap;
        flex-flow: row wrap;
    }

    /** 垂直伸缩容器**/
    .col-flex{
        -moz-box-orient: vertical;
        -webkit-box-orient: vertical;
        -moz-box-direction: normal;

        -moz-box-lines: multiple;
        -webkit-box-lines: multiple;
        -webkit-flex-flow: column wrap;
        -ms-flex-flow: column wrap;
        flex-flow: column wrap;
    }

    /** 伸缩容器**/
    .flex-wrap{
        /** 各种版本兼容**/
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
    }

    /** 垂直居中**/
    .midCenter{
        /** 垂直居中核心**/
        -moz-box-pack: center;
        -webkit-box-pack: center;
        box-pack:center;
        -moz-box-align: center;
        -webkit-box-align: center;
        box-align: center;
        box-pack:center;
        -ms-flex-pack:center;
        display: -ms-flexbox;
        -ms-flex-align:center;
        justify-content:center;
        align-items: center;
    }

    /** 占位器**/
    .page{
        -webkit-box-flex: 1;
        -moz-box-flex: 1;
        -ms-flex: 1;
        -webkit-flex: 1;
        flex: 1;
        overflow: hidden;
    }

    /** 真正滚动**/
    .scroll-wrap{
        position: relative;
        width: 100%;
        height: 100%;
        overflow: scroll;
        -webkit-box-flex: 1;
        -moz-box-flex: 1;
        -webkit-flex: 1;
        -ms-flex: 1;
        flex: 1;
    }

    /** 头和尾巴**/
    header,footer{
        background:#f7f7f7;
        height:44px;
    }

    footer>div{
        -webkit-box-flex: 1;
        -moz-box-flex: 1;
        -ms-flex: 1;
        -webkit-flex: 1;
        flex: 1;
        border:1px solid #666;
        margin:4px;
    }



    section{
        margin:3px 5px;
    }

    /** 真正设置高度 **/
    #banner{
        border:2px solid #999;
        margin:1px;
        height:100px;
    }
    #quirk,#four-col{
        height:150px;
    }
    #three-col,#two-col{
        height:100px;
    }


    /** 内部组件**/
    #four-col>div,#three-col>div,#two-col>div{
        -webkit-box-flex: 1;
        -moz-box-flex: 1;
        -ms-flex: 1;
        -webkit-flex: 1;
        flex: 1;
    }
    #four-col>div{
        background: #aaebbe;
        margin:0 2px;
    }
    #three-col>div{
        background: #8f82bc;
        margin:0 2px;
    }
    #two-col>div{
        background: #f7baba;
        margin:0 2px;
    }

    #quirk>div{
        margin:0 2px;
    }

    #quirk>div:nth-child(1){
        background: #b9e2ee;
        -webkit-box-flex: 200;
        -moz-box-flex: 200;
        -ms-flex: 200;
        -webkit-flex: 200;
        flex: 200;
    }

    #quirk>div:nth-child(2){
        -webkit-box-flex: 175;
        -moz-box-flex: 175;
        -ms-flex: 175;
        -webkit-flex: 175;
        flex: 175;

    }
    #quirk>div:nth-child(2)>div:nth-child(1){
        background: #b9e2ee;

        -webkit-box-flex: 60;
        -moz-box-flex: 60;
        -ms-flex: 60;
        -webkit-flex: 60;
        flex: 60;
        margin-bottom: 4px;
    }
    #quirk>div:nth-child(2)>div:nth-child(2){
        background: #b9e2ee;

        -webkit-box-flex: 30;
        -moz-box-flex: 30;
        -ms-flex: 30;
        -webkit-flex: 30;
        flex: 30;
    }

参考文献: 阮一峰Flex 布局教程:实例篇:http://www.ruanyifeng.com/blog/2015/07/flex-examples.html?bsh_bid=683103006
阮一峰flex布局:语法:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html
Introducing Flexbox Fridays:https://lincolnloop.com/blog/introducing-flexbox-fridays/