flex布局
一般我们传统的布局方式是,利用float,position等来实现页面效果,最近用了flex发现效果的实现非常方便,比如垂直居中,还有经常项目中遇到的这种效果:

如果用传统布局方式,虽然能够实现,但是会非常麻烦,如果三个元素的宽度不确定的话,会更加得复杂;
可是用flex就会非常方便:
Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。
现在各个浏览器对flex的兼容情况也还不错,相信未来会成为主流

下面介绍下主要的属性:
1.flex-direction
规定子元素的排列方向、
值:
row(默认):自左向右
row-reverse:自由向左
column:自上到下
column-reverse:自下到上
*{ padding:0;margin:0; } .div1{ display:flex; flex-direction:row; width:100%; justify-content:space-between; } h3{ margin:20px; } .content{ width:300px; height:30px; line-height: 30px; border:1px solid #ccc; text-align: center; } .div2{ display:flex; flex-direction:row-reverse; width:100%; justify-content:space-between; } .div3{ display:flex; flex-direction:column; width:300px; height:300px; justify-content:space-between; } .div4{ display:flex; flex-direction:column-reverse; width:300px; height:300px; justify-content:space-between; }
效果:


2.flex-wrap
默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。
.box{ flex-wrap: nowrap | wrap | wrap-reverse;
取值:
(1)nowrap(默认):不换行。
如果子元素内容宽度超过父元素,会被按比例压缩

(2)wrap:换行,第一行在上方。

(3)wrap-reverse:换行,第一行在下方。

3.flex-flow
flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。
.box { flex-flow: <flex-direction> || <flex-wrap>; }
4.justify-content属性
justify-content属性定义了项目在主轴上的对齐方式。
.box { justify-content: flex-start | flex-end | center | space-between | space-around; }
它可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。
(1)flex-start(默认值):左对齐

(2)flex-end:右对齐

(3)center: 居中

(4)space-between:两端对齐,项目之间的间隔都相等。(为了美观我给了父元素一定的padding值)

(5)space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。

5 align-items属性
align-items属性定义项目在交叉轴上如何对齐。
交叉轴就是和主轴相反的轴,如果主轴是水平,交叉轴就是竖直的
.box { align-items: flex-start | flex-end | center | baseline | stretch; }
(1)flex-start

(2)flex-end

(3)center

(4)baseline: 项目的第一行文字的基线对齐。
(5)stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。
6. align-content属性
align-content属性定义了多根轴线的对齐方式。注:如果项目只有一根轴线,该属性不起作用。
.box { align-content: flex-start | flex-end | center | space-between | space-around | stretch; }
代码:
<style>
*{
padding:0;margin:0;
}
.div1{
display:flex;
flex-direction:row;
width:100%;
justify-content:space-between;
}
h3{
margin:20px;
}
.content{
width:100px;
height:100px;
line-height: 30px;
border:1px solid #ccc;
text-align: center;
}
.div2{
padding:15px;
border:1px solid red;
display:flex;
flex-direction:row;
width:50%;
height:400px;
margin:20px auto;
flex-wrap: wrap;
justify-content:flex-start;
align-content: center;
}
</style>
<div class="div2">
<h1 class="content">box1</h1>
<h1 class="content">box2</h1>
<h1 class="content">box3</h1>
<h1 class="content">box1</h1>
<h1 class="content">box2</h1>
<h1 class="content">box3</h1>
<h1 class="content">box1</h1>
<h1 class="content">box2</h1>
<h1 class="content">box3</h1>
</div>
效果:

具体属性值可以自己测试,图太大不好放,不一一测试了
上面介绍的时父元素的各个属性,下面还有子元素的属性
1.order
属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。
2.flex-wrap
定义 如果一条轴线排不下,如何换行
.box{
flex-wrap: nowrap | wrap | wrap-reverse;
}
3.flex-grow
属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大;
如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。
4.flex-shrink
属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。
5flex-basis
flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。
6.flex
flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。
建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。
在移动端我们经常会遇到左边宽度固定,右边自适应的布局,这时候用flex就很方便
<div class="container"> <div class="left"></div> <div class="right"></div> </div>
css
.container{ display: flex; } .left{ flex:0 0 200px; background: #ccc; } .right{ flex: 1; background: #00a0dc; }


另:关于flex布局,推荐阮一峰老师的一篇博客,讲得很详细!!!!

浙公网安备 33010602011771号