html案例代码:css
<div class="flex-container">
<div class="flex-item red">
1
</div>
<div class="flex-item green">
2
</div>
<div class="flex-item yellow">
3
</div>
<div class="flex-item black">
4
</div>
</div>
复制代码
css案例代码:html
html,body{
background: #f7f7f7;
}
.flex-container{
display: flex
color: #fff;
}
.flex-item{
width: 150px;
height: 100px;
}
.red{
background: red;
}
.green{
background: green;
}
.yellow{
background: yellow;
}
.black{
background: black;
}
复制代码
页面以下:
css3
决定主轴(通常是x轴)的方向,即项目排列的方向,有四个可能的值:row(默认)| row-reverse | column | column-reverse布局
.flex-container{
display: flex;
height: 100px;
flex-direction: row; /* 1 */
flex-direction: row-reverse; /* 2 */
flex-direction: column; /* 3 */
flex-direction: column-reverse;/* 4*/
color: #fff;
}
.flex-item{
flex: 1
}
复制代码
默认状况下,item排列在一条线上,即主轴上,flex-wrap决定当排列不下时是否换行以及换行的方式,可能的值nowrap(默认) | wrap | wrap-reverseflex
.flex-container{
display: flex;
color: #fff;
flex-wrap: nowrap; /* 1 */
flex-wrap: wrap; /* 2 */
flex-wrap: wrap-reverse; /* 3 */
}
.flex-item{
width: 150px;
height: 100px;
}
.red{
width: 300px;
}
复制代码
备注: flex-item宽度定义分别是300px 150px 150px 150px; 因此按照比例2:1:1:1来分割父节点宽度,实际宽度为 150px 75px 75px 75px (合计375px)spa
是flex-direction和flex-wrap的简写形式,如:row wrap | column wrap-reverse等。默认值为row nowrap,即横向排列 不换行。ssr
.flex-container{
flex-flow: row nowrap;
}
复制代码
组合:3d
flex-flow | nowrap | wrap | wrap-reverse |
---|---|---|---|
row | 一、row nowrap | 五、row wrap | 九、row wrap-reverse |
column | 二、column nowrap | 六、column wrap | 十、column wrap-reverse |
row-reverse | 三、row-reverse nowrap | 七、row-reverse wrap | 十一、row-reverse wrap-reverse |
column-reverse | 四、column-reverse nowrap | 八、column-reverse wrap | 十二、column-reverse wrap-reverse |
第一种:当flex-wrap设置为nowrap时,属性(宽度比例,非px实际宽度)按照flex-direction来排列 code
第二种:当flex-wrap设置为wrap时,item则按照实际宽度排列,不足则到下行 cdn
第三种:当flex-wrap设置为wrap-reverse时, item则按照实际宽度排列,不足则到上行
很是重要,决定item在主轴上的对齐方式,可能的值有flex-start(默认),flex-end,center,space-between,space-around。
当主轴沿水平方向时,具体含义为
备注: 当项目item不满父节点item-container时,flex-start, flex-end不起到做用
.flex-container{
justify-content: flex-start; /* 1 */
justify-content: flex-end; /* 2 */
justify-content: center; /* 3 */
justify-content: space-between; /* 4 */
justify-content: space-around; /* 5 */
}
.flex-item{
width: 50px;
height:50px;
}
复制代码
决定了item在交叉轴上(Y轴)的对齐方式,可能的值有flex-start | flex-end | center | baseline | stretch
当主轴水平(Y轴)时,其具体含义为:
备注:父节点flex-container与子节点flex-item存在高度差,才能起到做用
.flex-container{
height: 200px;
align-items: flex-start; /* 1 */
align-items: flex-end; /* 2 */
align-items: center; /* 3 */
align-items: baseline; /* 4 */
align-items: stretch; /* 5 */
}
.flex-item{
height: 100px;
}
.red,.green{
font-size: 14px;
}
复制代码
该属性定义了当有多根主轴时,即item不止一行时,多行在交叉轴轴上的对齐方式。注意当有多行时,定义了align-content后,align-items属性将失效。
align-content可能值含义以下(假设主轴为水平方向):
备注:一、设置flex-wrap:wrap,否则默认nowrap按照比例排满一行。
二、父节点flex-container与子节点flex-item存在高度差,才能起到做用
.flex-container{
flex-wrap: wrap;
height:300px;
background: #969799;
align-content: flex-start; /* 1 */
align-content: flex-end; /* 2 */
align-content: center; /* 3 */
align-content: space-between; /* 4 */
align-content: space-between; /* 5 */
align-content: stretch; /* 6 */
}
复制代码
item的属性在item的style中设置。item共有以下六种属性
order的值是整数,默认为0,整数越小,item排列越靠前
.flex-container{
flex-flow: wrap;
}
.flex-items{
order:1;
}
复制代码
定义了当flex容器有多余空间时,item是否放大。默认值为0,即当有多余空间时也不放大;可能的值为整数,表示不一样item的放大比例
备注:item项目未规定宽度,则分配一行宽度,即便规定了宽度也分配多余空间
.flex-item{
flex-grow: 1;
}
/* 1 */
.flex-contanier{
flex-wrap: wrap;
}
/* 2 */
.flex-contanier{
flex-wrap: nowrap;
}
复制代码
定义了当容器空间不足时,item是否缩小。默认值为1,表示当空间不足时,item自动缩小,其可能的值为整数,表示不一样item的缩小比例。
备注:这个比较懵逼,待解析
表示项目在主轴上占据的空间,默认值为auto。
.red{
flex-basis: 100px;
}
.green {
flex-basis: 100px;
}
复制代码
flex属性是flex-grow、flex-shrink和flex-basis三属性的简写总和
具体查看以上三个
align-self属性容许item有本身独特的在交叉轴上的对齐方式,它有六个可能的值,默认值为auto
备注:与父节点flex-container中的align-item, align-content同个意思,不一样的是align设置的是单个的,而align-items设置的
.green{
align-self: auto;
align-self: flex-start;
align-self: flex-end;
align-self: center;
align-self: baseline;
align-self: stretch;
}
复制代码
备注:align-self: baseline; align-self: stretch; 与1 2同样在此不作多介绍,能够参考align-items
在实际项目中,关于flex这么多属性,能用到的比较少,经常使用的就那几个,记住这几个基本在项目开发中可以很好的实现布局
属性 | 场景 | 经常使用值 |
---|---|---|
justify-content | 水平居中,左右浮动,左右间隙分开 | flex-end center space- between space- around |
align-items | 垂直居中 | center |
flex | item子元素的宽度比例分配 | flex:1 1 100px / flex: 2等等 |
有不足之处,请评论留言... 谢谢