为了控制“对齐”,会用到如下属性:justify-content
——控制主轴(main axis)
上全部item的对齐;align-items
——控制交叉轴(cross axis)
上全部item的对齐;align-self
——控制交叉轴(cross axis)
上某一特定item的对齐;align-content
——当项目的数量多到占据多行时,控制交叉轴(cross axis)
上每行之间空间的分布状况;css
主轴
与交叉轴
就至关于一个二维坐标系的横轴和纵轴。
当在容器的css参数中,设置display:flex;
后,该容器即成为一个flex box。
这时,咱们能够经过设置flex-direction:row;
或者flex-direction:column;
来控制容器中的item的排布方向。row
表明横向排布,column
表明纵向排布。
另外还能够取的值是:row-reverse
与column-reverse
,他们相对于row
和column
只是换了个方向而已。
值得注意的是,主轴与交叉轴的方向会根据flex-direction
值的不一样而变化。
当flex-direction:row
时,主轴和交叉轴的关系以下图所示:html
而当flex-direction:column
时,主轴与交叉轴的关系以下图所示:布局
例:flex
<html> <head> <link rel="stylesheet" href="index.css"> </head> <body> <div class="container"> <div class="item">one</div> <div class="item">two</div> <div class="item">three</div> <div class="item">four</div> <div class="item">five</div> </div> </html>
css文件:spa
html, body { margin: 0; padding: 0; } .container{ height: 600px; width: 600px; margin-top: 20px; margin-left: 20px; display: flex; border: 1px dotted black; flex-direction: row; } .item{ background-color: #666; margin-right: 2px; }
能够看到,咱们有一个高600px,宽600px的容器。并为该容器设置了display:flex;
,还经过flex-direction:row;
规定其中item的排布方向为横向排列。
咱们只为其中的item设置了背景色和一个2px的右边距。
效果如图:设计
能够看到,当没有设置item的高、宽属性时,item在容器中默认是拉伸填满容器的。
如今,咱们为item设置高、宽属性:3d
.item{ width: 100px; height: 100px; background-color: #666; margin-right: 2px; }
效果以下图:code
能够看到,拉伸效果消失了。
并且,全部的item都自动左对齐了。那么若是咱们想让item都右对齐,应该怎么作呢?
这个时候justify-content
属性就派上用场了。justify-content
属性,简单说来,就是控制item在主轴上的对齐方式。
其可取的值有:justify-content: flex-start
justify-content: flex-end
justify-content: center
justify-content: space-between
justify-content: space-around
justify-content: stretch
justify-content: space-evenly
htm
各个属性值所对应的效果以下:
flex-start(默认效果)blog
.container{ height: 600px; width: 600px; justify-content: flex-start; ... }
flex-end
.container{ height: 600px; width: 600px; justify-content: flex-end; ... }
center
.container{ height: 600px; width: 600px; justify-content: center; ... }
space-between
.container{ height: 600px; width: 600px; justify-content: space-between; ... }
space-between
属性值让各个item两边不留边距,而平均分配item之间的空间。P.S:图中最右侧的2px的边距是以前设置的item的右边距。
space-around
.container{ height: 600px; width: 600px; justify-content: space-around; ... }
能够看到,正如around
所暗示的,和space-between
不一样,此次左右两边都有分配空间。并且是子容器沿主轴均匀分布,位于首尾两端的子容器到父容器的距离是子容器间距的一半。
stretch
因为设置了item的宽和高,因此stretch
不会生效。
space-evenly
.container{ height: 600px; width: 600px; justify-content: space-evenly; ... }
space-evenly
指的是空间的平均分配。
从上面的例子能够看出,当咱们的item横向排布时,justify-content
是控制着横方向上的元素对齐方式。
那若是咱们但愿控制竖方向上item的排列方式应该怎样作能?
这时候就须要用到align-item
属性了。
为了便于理解,此次咱们只用一个容器和一个item,而且不设置justify-content
。
html文件:
<html> <head> <link rel="stylesheet" href="index.css"> </head> <body> <div class="container"> <div class="item">one</div> </div> </html>
css样式
html, body { margin: 0; padding: 0; } .container{ height: 600px; width: 100px; margin-top: 20px; margin-left: 20px; display: flex; border: 1px dotted black; flex-direction: row; } .item{ height: 50px; width: 50px; background-color: #666; }
效果以下图:
能够看到,在交叉轴(这种状况下也就是咱们常说的纵轴)上,item默认的排布也是flex-start
。
而align-items
能够取的值也和justify-content
相似:align-items: flex-start
align-items: flex-end
align-items: center
align-items: stretch
align-items: baseline
flex-start(默认值)
.container{ height: 600px; width: 100px; display: flex; flex-direction: row; align-items: flex-start; ... }
效果如上图所示。
flex-end
.container{ height: 600px; width: 100px; display: flex; flex-direction: row; align-items: flex-end; ... }
flex-center
.container{ height: 600px; width: 100px; display: flex; flex-direction: row; align-items: center; ... }
stretch
同理,因为已经设置了item的宽和高,因此stretch
不会产生拉伸效果。
.container{ height: 600px; width: 100px; display: flex; flex-direction: row; align-items: stretch; ... }
base-line
为了表现基线对齐,咱们用到了三个item,效果如图:
这三个item等宽,高度分别为60px
,80px
,100px
。并且都经过<br>
让文字向下换了一行。他们在交叉轴上仍然是基线对齐的。这里的baseline
默认是指首行文字的基线。
这就好像平面直角坐标系用横坐标和纵坐标定位一个点同样,咱们能够同时使用这两个属性来定位一个item在容器中的位置。
好比,咱们想让一个item定位到容器的中间。
就能够这样写:
又好比,想要让三个item在box的中轴线上分布,并且它们之间的空间相等:
能够看到,这里咱们经过justify-content:space-between
令这三个item之间的空间相等。又经过aling-items:center
令他们在交叉轴方向上处于中间。
此时item排布的原理与flex-direction:row
时是一致的。
只不过此次主轴换到了竖直方向,而交叉轴换到了水平方向。
根据上面咱们讲到的原理,咱们不妨试举一例。
好比,有竖直排布的三个item,咱们但愿它们能够实现以下图所示的设计:
就能够这样设置咱们的css样式:
.box { height:300px; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; }
经过justify-content: flex-end
咱们实现了让这三个item排布到主轴(这种状况下是竖直方向这个轴)的尾端(end);
再经过align-items: center
咱们实现了让这三个item排布到交叉轴(这种状况下是横向的这个轴)的中间。
综合起来,就造成了这样一个布局。
简单起见,咱们能够直接将start
理解为坐标系原点所在的方位,而end
则是坐标轴的箭头所指向的无限远的方向。
须要指出的是,对于从左往右的书写模式,好比汉语、英语等,start能够理解为left,end能够理解为right,而对于从右往左的书写模式,好比阿拉伯语,则有,start能够理解为right,end能够理解为left。
当咱们为一个box容器设置了align-items
和 justify-content
属性以后,这套约束的规则将对其中的全部item都适用。
但是若是咱们不但愿某一个item被这套规则约束怎么办呢?
这时候align-self
就派上用场了。align-self
属性能够取align-items
属性的全部值。
好比,在上面的例子中,若是咱们把3 号item的align-self
值设置为:align-self:flex-end;
,则有:
这就至关于3号item宣告天下,它不接受经过align-items
设置的规则,而是要设置本身的规则,这也是为何align-self
能够取的值和align-items
如出一辙。
到目前为止,咱们讨论了在flex容器内单个或多个容器的排布问题。若是咱们有一个flex容器,而且其中的item占据了多行,这时候咱们能够用到align-content
属性,来控制行与行之间的空间分布。
align-content
要发挥做用,须要容器的高度比各行item的总高度之和要高。
align-content
属性能够取的值以下: align-content: flex-start
align-content: flex-end
align-content: center
align-content: space-between
align-content: space-around
align-content: stretch
align-content: space-evenly
好比,当align-content
取flex-end
时,有:
这些属性值与以前提到的功能一致,再也不赘述。