Blogger Information
Blog 18
fans 0
comment 0
visits 11017
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
210325 CSS flex布局 项目属性
xyphpblog
Original
636 people have browsed it

flex布局 项目属性

1. 项目整体伸缩属性

  • flex-grow
    • 允许放大
  • flex-shrink
    • 允许收缩
  • flex-basis
    • 设置item在主轴上的大小(覆盖width属性,但会被 min-width 和 max-width 属性覆盖)

以上属性简写:
flex: 放大因子 收缩因子 大小;
例:flex: 2 2 10rem;
默认值:flex: 0 1 auto; (initial)
1 1 auto = auto
0 0 auto = none

默认效果

flex-grow

  • 当容器宽度<=item宽度之和时,每个item宽度一样;
  • 当容器宽度足够,即大于item宽度之和时,item按照flex grow factor进行放大
    例:flex-grow: 1;
    变化如下

CSS

  1. .container>.item {
  2. padding: 1rem;
  3. background-color: lightsteelblue;
  4. border: 1px solid black;
  5. flex-basis: 10rem;
  6. }
  7. .container>.item:first-of-type {
  8. flex-grow: 1;
  9. }
  10. .container>.item:nth-of-type(2) {
  11. flex-grow: 2;
  12. }
  13. .container>.item:nth-of-type(3) {
  14. flex-grow: 3;
  15. }
  16. .container>.item:last-of-type {
  17. flex-grow: 4;
  18. }

flex-shrink

  • 当容器宽度足够容纳所有项目时, 不会收缩
  • 当容器宽度不够够容纳所有项目时从收缩因子最大的开始收缩

CSS

  1. .container>.item {
  2. padding: 1rem;
  3. background-color: lightsteelblue;
  4. border: 1px solid black;
  5. flex-basis: 10rem;
  6. }
  7. .container>.item:first-of-type {
  8. flex-shrink: 1;
  9. }
  10. .container>.item:nth-of-type(2) {
  11. flex-shrink: 2;
  12. }
  13. .container>.item:nth-of-type(3) {
  14. flex-shrink: 3;
  15. }
  16. .container>.item:last-of-type {
  17. flex-shrink: 4;
  18. }

当容器宽度足够容纳所有项目时:

当容器宽度不够时开始收缩:

下图中,容器总width为 366 < 400, 此时子项目大概宽度:
item1-85, item2-89, item3-92, item4-96
注:容器宽度过小时,会从item3-item1开始逐渐妥协,即item3-item1宽度逐渐与item4相等,最后每个item宽度相等

2. 单独设置某个项目 align-self

  1. .container>.item:nth-of-type(3) {
  2. flex-grow: 3;
  3. align-self: flex-start;
  4. }

3. flex项目支持定位,不支持浮动

  1. .container>.item:nth-of-type(3) {
  2. flex-grow: 3;
  3. align-self: flex-start;
  4. position: absolute;
  5. background-color: blanchedalmond;
  6. left: 5rem;
  7. top: 5rem;
  8. }

4. 改变flex项目顺序

order: value;
数字大靠后排列,数字小靠前排列

  1. .container>.item:nth-of-type(3) {
  2. flex-grow: 3;
  3. order: 20
  4. }
  5. .container>.item:last-of-type {
  6. flex-grow: 4;
  7. order: -1;
  8. }

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments