Flex布局(语法篇)

一、介绍Flex布局

    什么是Flex布局呢?Flex布局:又称弹性布局,它是Flexible Box 的缩写,它为盒状模型提供最大的灵活性。

二、Flex布局的概念


flex布局

flex  container:Flex容器(简称“容器”),即采用Flex布局的元素。

flex  item:Flex项目(简称“项目”),即容器的所有子元素自动成为容器成员。

mian  axis:水平的主轴。cross  axis:垂直的交叉轴。它们是容器(flex  container)默认存在的两根轴。

main  start:主轴的开始位置(与边框的交叉点)。main  end:主轴的结束位置(与边框的交叉点)。

cross  start:交叉轴的开始位置。cross  end:交叉轴的结束位置。

mian  size:单个项目占据的主轴空间。cross  size:单个项目占据的交叉轴空间。项目默认延主轴排列。

三、Flex  container(容器)的属性

Flex  container有6个属性,即 flex-direction、flex-wrap、justify-content、align-items、align-content、flex-flow。

1.flex-direction的属性

flex-direction:决定主轴的方向,即项目的排列方向

flex-direction:row | row-reverse | column | column-reverse

① flex-direction:row

row:默认值。主轴是水平方向,起点在最左边(即11)

flex-direction:row

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{  

         background:#ecbebe; 

         height: 100px; 

         width: 100px; 

        margin:10px;} 

(项目排列):

.box1{  

        display: flex;

        width:100%; 

        flex-direction: row;

}  

② flex-direction:row-reverse

row-reverse:主轴是水平方向,起点在最右边(即11)

flex-direction:row-reverse

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

          background:#ecbebe;

           height: 100px;

           width: 100px;

           margin:10px; }

(项目排列):

.box2{

        display: flex;

        width:100%;

        flex-direction: row-reverse;

③ flex-direction:column

row-reverse:主轴是垂直方向,起点在最上(即11)

flex-direction:column

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px;

}

(项目排列):

.box3{

        display: flex;

        width:100%;

        flex-direction: column;

④ flex-direction:column-reverse

column-reverse:主轴是垂直方向,起点在最下(即11)

flex-direction:column-reverse

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px;

}

(项目排列):

.box4{

        display: flex;

        width:100%;

        flex-direction: column-reverse;

2.flex-wrap的属性

flex-wrap:默认下,项目都排在轴线上。(定义:一条轴线排不下,如何换行)

flex-wrap:nowrap | wrap | wrap-reverse

① flex-wrap:nowrap

nowrap:默认值。起点在最左边(即11)

flex-wrap:nowrap

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px;

}

(不换行):

.box5{

        display: flex;

        width:100%;

        flex-wrap: nowrap;

② flex-wrap:wrap

wrap:起点在最左边(即11)

flex-wrap:wrap

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px;

(换行,第一行在上方):

.box6{

        display: flex;

        width:100%;

        flex-wrap: wrap;

③ flex-wrap:wrap-reverse

wrap-reverse:起点在最左边(即11)

flex-wrap:wrap-reverse

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5 ,.div6{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px;

}

(换行,第一行在下方):

.box7{

        display: flex;

        width:100%;

        flex-wrap: wrap-reverse;

3.justify-content的属性

justify-content:定义了项目在主轴上的对齐方式

justify-content:flex-start | flex-end | center | space-between | space-around

① justify-content:flex-start

flex-start:默认值。左对齐

justify-content:flex-start

css代码:

.div-box{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px;

}

(对齐方式):

.box8{

        display: flex;

        width:100%;

        justify-content: flex-start;

② justify-content:flex-end

flex-end:右对齐

justify-content:flex-end

css代码:

.div-box{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px;

}

(对齐方式):

.box9{

        display: flex;

        width:100%;

        justify-content: flex-end;

③ justify-content:center

center:居中

justify-content:center

css代码:

.div-box{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px;

}

(对齐方式):

.box10{

        display: flex;

        width:100%;

        justify-content: center;

④ justify-content:space-between

space-between:两端对齐,项目之间的间隔都相等

justify-content:space-between

css代码:

.div-box{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px;

}

(对齐方式):

.box11{

        display: flex;

        width:100%;

        justify-content: space-between;

⑤ justify-content:space-around

space-around:每个项目两侧的间隔相等,即项目之间的间隔比项目与边框的间隔大一倍

justify-content:space-around

css代码:

.div-box{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px;

(对齐方式):

.box12{

        display: flex;

        width:100%;

        justify-content: space-around;

4.align-items的属性

align-items:定义了项目在交叉轴上的对齐方式

align-items:flex-start | flex-end | center | baseline | stretch

① align-items:flex-start

flex-start:交叉轴的起点对齐

align-items:flex-start

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px;

}

.div2{

       height: 300px;

}

.div4{

       height: 50px;

(对齐方式):

.box13{

        display: flex;

        width:100%;

       align-items: flex-start;

② align-items:flex-end

flex-end:交叉轴的终点对齐

align-items:flex-end

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px;

.div2{

       height: 300px;

}

.div4{

       height: 50px;

}

(对齐方式):

.box14{

        display: flex;

        width:100%;

       align-items: flex-end;

③ align-items:center

center:交叉轴的中点对齐

align-items:center

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px; 

}

.div2{

       height: 300px;

}

.div4{

       height: 50px;

}

(对齐方式):

.box15{

        display: flex;

        width:100%;

        align-items: center;

④ align-items:baseline

baseline:项目的第一行文字的基线对齐

align-items:baseline

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

        background:#ecbebe;

        height: 100px;

        width: 100px;

        margin:10px;

.div2{

       height: 300px;

}

.div4{

        height: 50px;

(对齐方式):

.box16{

        display: flex;

        width:100%;

        align-items: baseline;

⑤ align-items:stretch

stretch:默认值。如果项目未设置高度或设为auto,将占满整个容器的高度

align-items:stretch

css代码:

#box17{

    width: 300px;

    height: 278px;

    border: 1px solid black;

    display:flex;

    align-items:stretch;

}

#box17 div{

  flex:1;

}

.div1{

  background-color:#eadd79;

}

.div2{

  background-color:#79e5ea;

}

.div3{

  background-color:#79ea8c;

}

5.align-content的属性

align-content:定义了项目在主轴上的对齐方式

align-content:flex-start | flex-end | center | space-between | space-around | stretch

① align-content:stretch

stretch:默认值。轴线占满整个交叉轴

align-content:stretch

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3{

        height:30px;

        width:100px;

}

.div1{

        background-color:#eadd79;

}

.div2{

        background-color:#79e5ea;

}

.div3{

        background-color:#79ea8c;

}

(对齐方式):

.box18{

        background-color:#ecbebe;

        height:200px;

        width:200px;

        display:flex;

        flex-direction:row;

        flex-wrap:wrap;

        align-content:stretch;

}

② align-content:center

center:与交叉轴的中点对齐

align-content:center

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

        height:30px;

        width:100px;

}

.div1{

        background-color:#eadd79;

}

.div2{

        background-color:#79e5ea;

}

.div3{

        background-color:#79ea8c;

}

.div4{

        background-color:#9ad1b2;

}

.div5{ 

        background-color:rgb(10,154,101);

}

(对齐方式):

.box19{

        background-color:#ecbebe;

        height:200px;

        width:200px;

        display:flex;

        flex-direction:row;

        flex-wrap:wrap;

        align-content:center;

}

③ align-content:flex-start

flex-start:与交叉轴的起点对齐

align-content:flex-start

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

        height:30px;

        width:100px;

}

.div1{

        background-color:#eadd79;

}

.div2{

        background-color:#79e5ea;

}

.div3{

        background-color:#79ea8c;

}

.div4{

        background-color:#9ad1b2;

}

.div5{

        background-color:rgb(10,154,101);

}

(对齐方式):

.box20{

        background-color:#ecbebe;

        height:200px;

        width:200px;

        display:flex;

        flex-direction:row;

        flex-wrap:wrap;

        align-content:flex-start;

③ align-content:flex-end

flex-end:与交叉轴的终点对齐

align-content:flex-end

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

        height:30px;

        width:100px;

}

.div1{

        background-color:#eadd79;

}

.div2{

        background-color:#79e5ea;

}

.div3{

        background-color:#79ea8c; 

}

.div4{

        background-color:#9ad1b2;

}

.div5{

        background-color:rgb(10,154,101);

}

(对齐方式):

.box21{

        background-color:#ecbebe;

        height:200px;

        width:200px;

        display:flex;

        flex-direction:row;

        flex-wrap:wrap;

        align-content:flex-end;

④ align-content:space-between

space-between:与交叉轴两端对齐,轴线之间的间隔平均分布

align-content:space-between

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

        height:30px;

        width:100px;

}

.div1{

        background-color:#eadd79;

}

.div2{

        background-color:#79e5ea;

}

.div3{

        background-color:#79ea8c;

}

.div4{

        background-color:#9ad1b2;

}

.div5{

        background-color:rgb(10,154,101);

(对齐方式):

.box22{

        background-color:#ecbebe;

        height:200px;

        width:200px;

        display:flex;

        flex-direction:row;

        flex-wrap:wrap;

        align-content:space-between;

}

⑤ align-content:space-around

space-around:每根轴线两侧的间隔都相等,即项目之间的间隔比项目与边框的间隔大一倍

align-content:space-around

css代码:

(11、22、33、44、55):

.div1 ,.div2 ,.div3 ,.div4 ,.div5{

        height:30px;

        width:100px;

}

.div1{

        background-color:#eadd79;

}

.div2{

        background-color:#79e5ea;

}

.div3{

        background-color:#79ea8c;

}

.div4{

        background-color:#9ad1b2;

}

.div5{

        background-color:rgb(10,154,101);

}

(对齐方式):

.box23{

        background-color:#ecbebe;

        height:200px;

        width:200px;

        display:flex;

        flex-direction:row;

        flex-wrap:wrap;

        align-content:space-around;

6.flex-flow的属性

flex-flow:flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap

(因为我们习惯于单独使用flex-direction、flex-wrap,而flex-flow却很少使用到,所以可以将它视为“糟粕”)

flex-flow:<flex-direction> ||<flex-wrap>

只举两个例子说明:

① flex-flow:column wrap

column wrap:column(主轴为垂直方向,起点在上沿),wrap(换行,第一行在上方)

flex-flow:column wrap

css代码:

.box24 {

    display: flex;

}

.flex-container.first {

    display: flex;

    background: #333;

    width: 200px;

    height: 200px;

    margin: 5px;

    flex-flow: column wrap;

}

.item { 

    background: #ecbebe;

    padding: 5px;

    width: 80px;

    height: 80px;

    margin: 5px;

    line-height: 80px;

    color: white;

    font-weight: bold;

    font-size: 2em;

    text-align: center;

ul {

    padding: 0;

}

li {

    list-style: none;

}

② flex-flow:row-reverse wrap-reverse

row-reverse wrap-reverse:row-reverse(主轴为水平方向,起点在右端),row-reverse(换行,第一行在下方)

flex-flow:row-reverse wrap-reverse

css代码:

.box25 {

    display: flex;

}

.flex-container.second {

    display: flex;

    background: #333;

    width: 200px;

    height: 200px;

    margin: 5px;

    flex-flow: row-reverse wrap-reverse;

}

.item {

    background: #ecbebe;

    padding: 5px;

    width: 80px;

    height: 80px;

    margin: 5px;

    line-height: 80px;

    color: white;

    font-weight: bold;

    font-size: 2em; 

    text-align: center;

}

ul {

    padding: 0;

}

li {

    list-style: none; 

}

四、项目(flex item)的属性

1.flex-grow 的属性

flex-grow:定义项目(flex-grow)的放大比例,默认为0,即如果存在剩余空间,也不放大

flex-grow

css代码:

#box26 {

  width: 700px;

  height: 200px;

  border: 1px;

  display: flex;

}

.blue1 {

    background-color:#2ce0e8;

    flex-grow: 2;

}

.green1 {

    background-color:#00ff66;

    flex-grow: 3;

}

.yellow1 {

    background-color:#e3fd0e;

    flex-grow: 0.5;

}

.pink1 {

    background-color:#ecbebe;

    flex-grow: 1;

}

.grey1 {

    background-color:#cec5c5;

    flex-grow: 1;

}

2.order 的属性

order:定义项目(flex-grow)的排列顺序(即数值越小,排列越靠前,默认为0)

(order在布局中基本不会使用到,不用深入了解,所以可以将它视为“糟粕”)

order:<integer>

order: <integer>

css代码:

#box27 {

    width: 400px;

    height: 150px;

    border: 1px;

    display: flex;

}

#box27 div {

    width: 100px;

    height: 100px;

}

.yellow {

    background-color:#e3fd0e;

    order: -2;

.blue  {

    background-color:#2ce0e8;

    order: 5;

}

.green {

    background-color:#00ff66;

    order: 10;

}

.pink  {

    background-color:#ecbebe;

    order: 0;

}

3.flex-shrink 的属性

flex-shrink:定义项目(flex-grow)的项目的缩小比例,默认为1,即如果空间不足,该项目将缩小

(对于flex-shrink,我个人认为这个在flex中不常使用,因为我们都基本会定义每一个元素的高度和宽度,所以可以将它视为“糟粕”)

flex-shrink:<number>

flex-shrink:<number>

css代码:

#box28 {

  display: flex;

  width: 500px;

}

#box28 div {

  flex-basis: 140px;

  border: 1px solid rgba(0,0,0,.1);

}

.boxt{

  flex-shrink: 0;

}

.boxt1 {

  flex-shrink: 1; 

}

4.flex-basis 的属性

flex-basis:定义在分配多余空间之前,项目(flex-grow)的占的主轴空间(main size),浏览器根据这个属性,计算主轴是否有多余空间(默认值为auto,即项目的本来大小)

(对于flex-basis,在布局中,也不常用,所以可以将它视为“糟粕”)

flex-basis:<length> | auto

flex-basis:<length> | auto

css代码:

#box29 {

    width: 350px;

    height: 100px;

    border: 1px;

    display: flex;

.one {

    flex-grow: 0;

    flex-shrink: 0;

    flex-basis: 100px;

}

.two {

    flex-grow: 0;

    flex-shrink: 0;

    flex-basis: 150px;

}

.three{

    flex-grow: 0;

    flex-shrink: 0;

    flex-basis: auto;

}

5.flex 的属性

flex:flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto,后两个属性可选

(因为我们习惯于单独使用flex-grow、flex-shrink、flex-basis,而flex却很少使用到,所以可以将它视为“糟粕”)

flex:none | [<flex-grow> <flex-shrink> || <flex-basis> ]

快捷值:auto (1 1 auto) 和 none (0 0 auto)

① flex:1

flex:1

css代码:

#box30 div{

    flex:1;

② flex:auto

flex:auto

css代码:

#box30 div{

    flex:auto;

③ flex:none

flex:none

css代码:

#box30 div{

    flex:none;

6.align-self  的属性

align-self :允许单个项目(flex item)有与其他项目不一样的对齐方式,可覆盖align-items的属性(默认值为auto,表示继承父元素的align-items的属性,如果没有父元素,则等同于stretch)

align-self:auto | flex-start | flex-end | center | baseline | stretch

align-self:auto | flex-start | flex-end | center | baseline | stretch

css代码:

#box31 {

    width: 1000px;

    height: 400px;

    border: 1px solid rgba(0,0,0,.2); 

    display: flex;

    align-items: flex-start;

}

.Red {

    background-color:#f32929;

    width: 300px;

    height: 100px;

    flex: 1;

    align-self: flex-start;

}

.Pink {

    background-color:#ecbebe;

    width: 300px;

    height: 100px;

    flex: 1;

.Blue {

    background-color:#2ce0e8;

    width: 300px;

    height: 100px;

    flex: 1;

    align-self: center;

}

.Green{

    background-color:#00ff66;

    width: 300px;

    height: 100px;

    flex: 1;

}

.Yellow{

    background-color:#e3fd0e;

    width: 300px;

    height: 100px;

    flex: 1;

    align-self: flex-end;

}

.Grey{ 

    background-color:#cec5c5;

    width: 300px;

    height: 100px;

    flex: 1;

    align-self: baseline;

}

.Black{

    background-color:#5f4e4e; 

    flex: 1;

    align-self: stretch; 

}


参考文献:

Flex 布局教程:语法篇(http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,271评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,725评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,252评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,634评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,549评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,985评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,471评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,128评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,257评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,233评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,235评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,940评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,528评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,623评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,858评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,245评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,790评论 2 339

推荐阅读更多精彩内容

  • 网页布局(layout)是CSS的一个重点应用。 行内元素也可以使用Flex布局。.box{ display: i...
    君临天下夜未央阅读 1,278评论 0 5
  • 原文地址:flex布局语法篇 之前写过一篇关于实现元素居中的博文,我们发现每种布局要求都可以运用flex布局实现。...
    薛普定朗谔克阅读 1,446评论 0 8
  • flex简介 当我们需要多行多列自适应,任意行对齐时。便可用flex布局实现。Flex是Flexible Box的...
    穆瑟muse阅读 1,093评论 0 5
  • 参考资料:《Flex 布局教程:语法篇》 设置为Flex布局后(display: flex;),子元素的float...
    泥垢樂阅读 333评论 0 0
  • 坚持有时候是一个痛苦的过程,尤其是在我们还看不到实际成效的阶段。在坚持的过程中,我们可能会遇到挫折,遭受身体和内心...
    快乐剑客阅读 155评论 0 0