微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

css---flex布局--容器

http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool

容器设置

      新版的为display为flex                                            老版的为display为webkit-Box

 

      布局方向                                                                   老版的布局方向

   flex-direction: row;                                                     -webkit-Box-orient: horizontal;
   flex-direction: column;                                               -webkit-Box-orient: vertical;

<!DOCTYPE html>
<html>
    <head>
        <Meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            {
                margin: 0;
                padding: 0;
            }
            #wrap{
                width: 400px;
                height: 300px;
                border: 1px solid;
                margin:100px auto;
                display:flex;
                /*x排列*/
                flex-direction: column;
            }
            #wrap > .item{
                width: 50px;
                height: 50px;
                background: pink;
                text-align: center;
                line-height: 50px;
            }
            </style>
    </head>
    <body><div id="wrap">
            <div class="item">1</div>
            <div class="item">2</div>
            <div class="item">3</div>
            <div class="item">4</div>
            <div class="item">5</div>
        </div>
    </body>
</html>
flex布局

<!DOCTYPE html>
<html>
    <head>
        <Meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            {
                margin: 0;
                padding: 0;
            }
            #wrap{
                width: 400px;
                height: 300px;
                border: 1px solid;
                margin:100px auto;
                display:-webkit-Box;
                /*  x排列*/
                 -webkit-Box-orient: vertical;
            }
            #wrap > .item{
                width: 50px;
                height: 50px;
                background: pink;
                text-align: center;
                line-height: 50px;
            }
            </style>
    </head>
    <body><div id="wrap">
            <div class="item">1</div>
            <div class="item">2</div>
            <div class="item">3</div>
            <div class="item">4</div>
            <div class="item">5</div>
        </div>
    </body>
</html>
Box布局

老版容器排列方向

-webkit-Box-direction: normal;
-webkit-Box-direction: reverse;


(注意:项目永远是沿着主轴的正方向排列的)
-webkit-Box-direction属性本质上改变了主轴的方向

新版

flex-direction:row-reverse;
flex-direction:column-reverse;

<!DOCTYPE html>
<html>
    <head>
        <Meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            #wrap{
                width: 400px;
                height: 300px;
                border: 1px solid;
                margin:100px auto;
                display:flex;
                flex-direction: row-reverse;
            }
            #wrap > .item{
                width: 50px;
                height: 50px;
                background: pink;
                text-align: center;
                line-height: 50px;
            }
            
        </style>
    </head>
    <body>
        <div id="wrap">
            <div class="item">1</div>
            <div class="item">2</div>
            <div class="item">3</div>
            <div class="item">4</div>
            <div class="item">5</div>
        </div>
    </body>
</html>
flex容器排列方向

<!DOCTYPE html>
<html>
    <head>
        <Meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            #wrap{
                width: 400px;
                height: 300px;
                border: 1px solid;
                margin:100px auto;
                display:-webkit-Box;
                /*-webkit-Box-orient控制主轴和侧轴分别是哪一根*/
                -webkit-Box-orient:horizontal;
                /*-webkit-Box-direction控制主轴方向*/
                -webkit-Box-direction: reverse;
            }
            #wrap > .item{
                width: 50px;
                height: 50px;
                background: pink;
                text-align: center;
                line-height: 50px;
            }
            
        </style>
    </head>
    <body>
        <div id="wrap">
            <div class="item">1</div>
            <div class="item">2</div>
            <div class="item">3</div>
            <div class="item">4</div>
            <div class="item">5</div>
        </div>
    </body>
</html>
Box排列方向

老版

富裕空间的管理(主轴)
start
end
center
justify
-webkit-Box-pack:start; 不会给项目区分配空间,只是确定富裕空间的位置

<!DOCTYPE html>
<html>
    <head>
        <Meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            #wrap{
                width: 400px;
                height: 300px;
                border: 1px solid;
                margin:100px auto;
                display:-webkit-Box;
                /*-webkit-Box-orient控制主轴和侧轴分别是哪一根*/
                -webkit-Box-orient:horizontal;
                /*-webkit-Box-direction控制主轴方向*/
                -webkit-Box-direction: reverse;
                 /* 
                  start  富裕空间在右边
                  end       富裕空间在左边
                  center  富裕空间在两边
                  justify 富裕空间在项目之间
                 */
                -webkit-Box-pack: start;
            }
            #wrap > .item{
                width: 50px;
                height: 50px;
                background: pink;
                text-align: center;
                line-height: 50px;
            }
            
        </style>
    </head>
    <body>
        <div id="wrap">
            <div class="item">1</div>
            <div class="item">2</div>
            <div class="item">3</div>
            <div class="item">4</div>
            <div class="item">5</div>
        </div>
    </body>
</html>
old Box富裕空间主

<!DOCTYPE html>
<html>
    <head>
        <Meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            #wrap{
                width: 400px;
                height: 300px;
                border: 1px solid;
                margin:100px auto;
                display:-webkit-Box;
                /*-webkit-Box-orient控制主轴和侧轴分别是哪一根*/
                -webkit-Box-orient:horizontal;
                /*-webkit-Box-direction控制主轴方向*/
                -webkit-Box-direction: reverse;
                 /* 
                  start  富裕空间在右边
                  end       富裕空间在左边
                  center  富裕空间在两边
                  justify 富裕空间在项目之间
                 */
                -webkit-Box-pack: start;
            }
            #wrap > .item{
                width: 50px;
                height: 50px;
                background: pink;
                text-align: center;
                line-height: 50px;
            }
            
        </style>
    </head>
    <body>
        <div id="wrap">
            <div class="item">1</div>
            <div class="item">2</div>
            <div class="item">3</div>
            <div class="item">4</div>
            <div class="item">5</div>
        </div>
    </body>
</html>
old Box 富余空间在侧

 



富裕空间的管理(侧轴)
start
end
center
-webkit-Box-align:center; 不会给项目区分配空间,只是确定富裕空间的位置

新的

更强大的富裕空间的管理(主轴)
justify-content: flex-start;
flex-start
flex-end
center
space-between
space-around(Box 没有的)

更强大的富裕空间的管理(侧轴)
align-items: stretch;
flex-start
flex-end
center
baseline(Box 没有的)
stretch(Box 没有的)

<!DOCTYPE html>
<html>
    <head>
        <Meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            #wrap{
                width: 400px;
                height: 300px;
                border: 1px solid;
                margin:100px auto;
                display:flex;
                flex-direction: row-reverse;
                 /*
                 flex-start                  富裕空间在主轴的正方向
                 flex-end                    富裕空间在主轴的反方向
                 center                                    富裕空间在主轴的两边
                 space-between               富裕空间在项目之间
                 space-around(Box 没有的)    富裕空间在项目两边
                 */
                justify-content: space-around;
            }
            #wrap > .item{
                width: 50px;
                height: 50px;
                background: pink;
                text-align: center;
                line-height: 50px;
            }
            
        </style>
    </head>
    <body>
        <div id="wrap">
            <div class="item">1</div>
            <div class="item">2</div>
            <div class="item">3</div>
            <div class="item">4</div>
            <div class="item">5</div>
        </div>
    </body>
</html>
new flex 富余空间在主轴

<!DOCTYPE html>
<html>
    <head>
        <Meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            #wrap{
                width: 400px;
                height: 300px;
                border: 1px solid;
                margin:100px auto;
                display:flex;
                flex-direction: row-reverse;
                 /*
                 flex-start                  富裕空间在主轴的正方向
                 flex-end                    富裕空间在主轴的反方向
                 center                                    富裕空间在主轴的两边
                 space-between               富裕空间在项目之间
                 space-around(Box 没有的)    富裕空间在项目两边
                 */
                justify-content: space-around;
                 /*
                 flex-start: 富裕空间在侧轴的正方向;
                 flex-end: 富裕空间在侧轴的反方向;
                 content: 富裕空间在侧轴的两边;
                 
                 baseline(Box 没有的) 按基线对齐
                 stretch(Box 没有的)      等高布局
                 */
                align-items: stretch;
            }
            #wrap > .item{
                width: 50px;
                height: 50px;
                background: pink;
                text-align: center;
                line-height: 50px;
            }
            
        </style>
    </head>
    <body>
        <div id="wrap">
            <div class="item">1</div>
            <div class="item">2</div>
            <div class="item">3</div>
            <div class="item">4</div>
            <div class="item">5</div>
        </div>
    </body>
</html>
new flex富余空间在侧轴

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐