xiaokeqi / i-learned

1 stars 0 forks source link

flex实战 #51

Open xiaokeqi opened 4 years ago

xiaokeqi commented 4 years ago

flex实战

阮一峰的flex布局教程以前仔细看过,然而没有做过demo,又全部把知识交还给阮老师了!这篇文章,我将根据阮一峰老师的flex布局教程一一实现demo。

介绍

任何容器元素都可以指定为flex布局,指定flex布局后,float、clear、vertical-align属性都将失效。采用flex布局的元素称为flex容器,它的所有子元素称为flex item,即flex项目

image

容器默认存在两根轴,水平的主轴main axios和垂直的交叉轴cross axios,主轴的开始位置叫main start,结束位置main end,交叉轴的开始位置cross start,结束位置cross end.

容器属性

其中容器,有6个属性

在上面的属性中,justify-content,align-items经常容易分不清楚,有道词典翻译如下

image

image

记住了么?属性介绍完,我们开始实战吧!!!

默认情况下

Flex-direction:row;

Flex-wrap:nowrap;

Justify-content:flex-start;

Align-items: strech;

即方向从左到右排列,不换行,主轴左对齐,交叉轴上高度延满整个容器高度

https://jsbin.com/repujob/10/edit?html,css,output

image

flex-direction

设置flex-direction:row-reverse;

image

设置 flex-direction:column;

image

设置flex-direction:column-reverse;

image

flex-direction 决定主轴的排列方向

flex-wrap

demo见https://jsbin.com/repujob/15/edit?html,css,output

默认值flex-wrap:nowrap,等比例压缩flex item,撑满flex容器

image

设置flex-wrap:wrap;原flex item尺寸不变,换行展示

image

设置flex-wrap:wrap-reverse;原flex item尺寸不变,颠倒换行展示

image

justify-content

默认值justify-content:flex-start;沿主轴start对齐

image

设置justify-content: flex-end; 沿主轴end对齐

image

设置justify-content: center; 沿主轴居中对齐

image

设置justify-content: space-between;两端对齐,项目之间的间隔都相等。

image

设置justify-content: space-around;每个项目两侧的间隔相等

image

align-items

默认值align-items:flex-start;沿交叉轴start对齐

Demo见 https://jsbin.com/repujob/24/edit?html,css,output

image

设置align-items: flex-end;沿交叉轴end对齐

image

设置align-items:center;沿交叉轴居中对齐

image

设置align-items:baseline项目的第一行文字基线对齐

demo见:https://jsbin.com/repujob/29/edit?html,css,output

image

设置align-items:strech;如果项目未设置高度或设为auto,将占满整个容器的高度

demo见:https://jsbin.com/repujob/30/edit?html,css,output

image

align-content

属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。

默认值align-content:strech;

Demo:https://jsbin.com/repujob/42/edit?html,css,output

image

设置align-content: flex-start;

image

设置align-content:flex-end;

image

设置align-content:center;

image

设置align-content:space-between;

image

设置align-content: space-around;

image

项目属性

设置order:1,将.first order 设置为1,展示到最后

见demohttps://jsbin.com/repujob/43/edit?html,css,output

image

设置flex-grow:1; 默认值为0,即如果存在剩余空间,也不放大,

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

见demohttps://jsbin.com/repujob/45/edit?html,css,output

image

设置flex-shrink:2;默认值为1,即如果空间不足,将缩小。若设置为2,将缩小2倍,见demo

https://jsbin.com/repujob/46/edit?html,css,output

image

设置flex-basis: 150px;见demo,green-1,在以150px基础上,与red根据flex-grow:1,平分剩余空间

https://jsbin.com/repujob/47/edit?html,css,output

image

设置align-self: flex-end;,其具体实践与容器属性align-items一致。设置单个项目的对其方式,覆盖align-items

demo见:https://jsbin.com/repujob/49/edit?html,css,output

image

完!!!

你学会了么?