学习Flexbox经验总结

高洛峰
Release: 2017-03-22 14:55:17
Original
1343 people have browsed it

flex语法

采用Flex布局的元素,称为Flex容器(flex container),简称"容器"。它的所有子元素自动成为容器成员,称为Flex项目(flex item),简称"项目"。

容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end。

项目默认沿主轴排列。单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size。

容器上的属性

以下6个属性设置在容器上:

flex-direction

flex-wrap

flex-flow

justify-content

align-items

align-content
Copy after login

flex-direction:flex-direction属性决定主轴的方向(即项目的排列方向)。

row(默认) | row-reverse | column | column-reverse
Copy after login

flex-wrap:默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。

nowrap(默认) | wrap | wrap-reverse
Copy after login

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

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

flex-start | flex-end | center | space-between | space-around
Copy after login

align-items属性:align-items属性定义项目在交叉轴上如何对齐。

flex-start | flex-end | center | baseline | stretch
Copy after login

align-content:align-content属性定义了多根轴线在交叉轴上的对齐方式。如果项目只有一根轴线,该属性不起作用

flex-start | flex-end | center | space-between | space-around | stretch
Copy after login

项目上的属性

以下6个属性设置在项目上:

order

flex-grow

flex-shrink

flex-basis

flex

align-self
Copy after login

order:order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。

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

如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。

flex-shrink:flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。

如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。

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

flex-basis:  | auto; /* default auto */
Copy after login

它可以设为跟widthheight属性一样的值(比如350px),则项目将占据固定空间。

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

该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。

建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。

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

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

布局

网格布局

1 基本网格布局

最简单的网格布局,就是平均分布。item设置flex:1即可

.Grid {
  display: flex;
}

.Grid-cell {
  flex: 1;
}
Copy after login

2 百分比布局

某个网格的宽度为固定的百分比,其余网格平均分配剩余的空间。

给需要百分比的item设置width: 百分比;或flex: 0 0 百分比;,自动分配的item设置flex: 1;

3 圣杯布局





  
  
  JS Bin

  
header
  

    

content
            

  
footer
*{   margin: 0; } .HolyGrail {   display: flex;   min-height: 100vh;   flex-direction: column;   text-align: center; } header, footer {   flex: 0 0 40px;   background-color: #ccc; } .HolyGrail-body {   display: flex;   flex: 1; } .HolyGrail-content {   flex: 1;   background-color: #0f0;    } .HolyGrail-nav, .HolyGrail-ads {   /* 两个边栏的宽度设为12em */   flex: 0 0 12em;   background-color: #00f; } .HolyGrail-nav {   /* 导航放到最左边 */   order: -1;   background-color: #f00; }
Copy after login

查看demo

如果是小屏幕,躯干的三栏自动变为垂直叠加。

@media (max-width: 768px) {
  .HolyGrail-body {
    flex-direction: column;
    flex: 1;
  }
  .HolyGrail-nav,
  .HolyGrail-ads,
  .HolyGrail-content {
    flex: auto;
  }
}
Copy after login

4 流式布局

每行的项目数固定,会自动分行。

.parent {
  width: 200px;
  height: 150px;
  background-color: black;
  display: flex;
  flex-flow: row wrap;
  align-content: flex-start;
}

.child {
  box-sizing: border-box;
  background-color: white;
  flex: 0 0 25%;
  height: 50px;
  border: 1px solid red;
}
Copy after login

兼容

*在旧版的规范中,使用比例伸缩布局时,子元素的内容长短不同会导致无法“等分”,这个时候,我们需要给子元素设置一个“width:0%”来解决问题。

*不要给flexbox里的子元素设置“margin:auto”的属性,在部分安卓机下,它会导致该元素的宽度撑开到100%占位
Copy after login

The above is the detailed content of 学习Flexbox经验总结. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!