Blogger Information
Blog 31
fans 0
comment 0
visits 18315
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
弹性布局之弹性元素-九期线上班
Content っ
Original
460 people have browsed it

1.将课堂中的全部案例照写一遍, 并达到默写级别

公共代码

  1. <div class="container flex demo">
  2. <span class="item">item1</span>
  3. <span class="item">item2</span>
  4. <span class="item">item3</span>
  5. </div>
(1)增长因子flex-grow
  1. @import "public.css";
  2. .container{
  3. width: 500px;
  4. }
  5. .item{
  6. width: 100px;
  7. }
  8. /*默认显示方式*/
  9. .demo1 > .item{
  10. flex-grow: 0;
  11. }
  12. /*将全部剩余空间分配给指定弹性元素,如最后一个。*/
  13. .demo2 > .item:last-of-type{
  14. flex-grow: 1;
  15. }
  16. /*全部剩余空间按增长因子在不同弹性元素间分配*/
  17. .demo3 > .item:first-of-type{
  18. flex-grow: 1;
  19. }
  20. .demo3 > .item:nth-of-type(2){
  21. flex-grow: 1;
  22. }
  23. .demo3 > .item:last-of-type{
  24. flex-grow: 3;
  25. }
  26. /*增长因子按小数分配剩余空间*/
  27. .demo4 > .item:first-of-type{
  28. flex-grow: 0.3;
  29. }
  30. .demo4 > .item:nth-of-type(2){
  31. flex-grow: 0.2;
  32. }
  33. .demo4 > .item:last-of-type{
  34. flex-grow: 0.5;
  35. }
  36. /*每个弹性元素宽度不同时, 同样适用以上分配规律*/
  37. .demo5 > .item:first-of-type{
  38. width: 120px;
  39. flex-grow: 0.3;
  40. }
  41. .demo5 > .item:nth-of-type(2){
  42. width: 130px;
  43. flex-grow: 0.2;
  44. }
  45. .demo5 > .item:last-of-type{
  46. width: 180px;
  47. flex-grow: 0.5;
  48. }

运行效果

(2)缩减因子flex-shrink
  1. @import "public.css";
  2. .container{
  3. width: 500px;
  4. }
  5. .item{
  6. width: 250px;
  7. }
  8. /*所有弹性元素不缩减,以原始宽度显示,缩减因子为: 0*/
  9. .demo1 > .item{
  10. flex-shrink: 0;
  11. }
  12. /*所有弹性元素自适应容器宽度且不换行,缩减因子: 1 (默认)*/
  13. .demo2 > .item{
  14. flex-shrink: 1;
  15. }
  16. /*当三个元素的缩减因为子不相等时*/
  17. .demo3 > .item:first-of-type{
  18. flex-shrink: 1;
  19. }
  20. .demo3 > .item:nth-of-type(2){
  21. flex-shrink: 2;
  22. }
  23. .demo3 > .item:last-of-type{
  24. flex-shrink: 3;
  25. }
  26. /*缩减因子也可以是小数,只要大于就可以了, 负数无效*/
  27. .demo4 > .item:first-of-type{
  28. flex-shrink: 0.3;
  29. }
  30. .demo4 > .item:nth-of-type(2){
  31. flex-shrink: 0.2;
  32. }
  33. .demo4 > .item:last-of-type{
  34. flex-shrink: 0.5;
  35. }
  36. /*5. 当每个弹性元素宽度不一样时*/
  37. .demo5 > .item:first-of-type{
  38. width: 120px;
  39. flex-shrink: 2;
  40. }
  41. .demo5 > .item:nth-of-type(2){
  42. width: 130px;
  43. flex-shrink: 3;
  44. }
  45. .demo5 > .item:last-of-type{
  46. width: 180px;
  47. flex-shrink: 6;
  48. }

运行效果

(3)基准尺寸flex-basis
  1. @import "public.css";
  2. .container{
  3. width: 500px;
  4. }
  5. .item{
  6. width: 250px;
  7. }
  8. /*1.在未设置元素宽度时,以内容宽度显示*/
  9. .demo1 > .item{
  10. flex-basis: content;
  11. }
  12. /*2.存在自定义元素宽度时,则以该宽度显示*/
  13. .demo2 > .item{
  14. flex-basis: 100px;
  15. }
  16. /*3.自动状态下, 将设置权限交给浏览器*/
  17. .demo3 > .item {
  18. flex-basis: auto;
  19. }
  20. /*4.当元素存在自定义宽度和flex-basic基准宽度时,基准值为准*/
  21. .demo4 > .item {
  22. width: 100px;
  23. flex-basis: 150px;
  24. }
  25. /*5. 元素基准宽度支持百分比*/
  26. .demo5 > :first-child {
  27. flex-basis: 20%;
  28. }
  29. .demo5 > :nth-child(2) {
  30. flex-basis: 30%;
  31. }
  32. .demo5 > :last-child {
  33. flex-basis: 50%;
  34. }

运行效果

(4)简写弹性元素flex
  1. @import "public.css";
  2. .container{
  3. width: 550px;
  4. }
  5. /*根据宽度计算,允许缩减适应容器*/
  6. .demo1 > .item{
  7. width: 100px;
  8. height: 60px;
  9. flex: initial;
  10. /*等价于下面默认值*/
  11. flex: 0 1 auto;
  12. }
  13. /*根据宽度计算,元素完全弹性以适应容器*/
  14. .demo2 > .item{
  15. width: 100px;
  16. height: 60px;
  17. flex: auto;
  18. /*等价于下面*/
  19. /*flex: 1 1 auto;*/
  20. }
  21. /*元素完全失去弹性, 以原始大小呈现*/
  22. .demo3 > .item{
  23. width: 100px;
  24. height: 60px;
  25. flex: none;
  26. /*等价于*/
  27. flex: 0 0 auto;
  28. }
  29. /*一个数值表示增长因子,其它值默认: flex: 1 1 auto*/
  30. .demo4 > .item{
  31. width: 100px;
  32. height: 60px;
  33. flex: 1;
  34. /*等价于下面 */
  35. /*flex: auto;*/
  36. /*等价于下面*/
  37. /*flex: 1 1 auto;*/
  38. }
  39. /*第三个有具体数值时, 以它为计算标准*/
  40. .demo5 > .item{
  41. width: 100px;
  42. height: 60px;
  43. flex: 1 0 200px;
  44. }
  45. /*单独设置某一个元素弹性大小*/
  46. .demo6 > .item{
  47. width: 100px;
  48. height: 60px;
  49. }
  50. .demo6 > .item:last-of-type{
  51. flex: 1 1 50%;
  52. }

运行效果

2.将flex属性的用法, 手抄, 建议二遍以上

3.自学align-self order

  1. @import "public.css";
  2. .demo1{
  3. width: 500px;
  4. height: 400px;
  5. flex-flow: row nowrap;
  6. }
  7. .item{
  8. flex-basis: content;
  9. }
  10. /*设置为父元素的 align-items 值,如果该元素没有父元素的话,就设置为 stretch。*/
  11. .demo1 > .item:first-of-type{
  12. align-self: auto;
  13. }
  14. /*flex 元素会对齐到的起点*/
  15. .demo1 > .item:nth-of-type(2){
  16. align-self: flex-start;
  17. }
  18. /*flex 元素会对齐到的中间,如果该元素的 cross-size 的尺寸大于 flex 容器,将在两个方向均等溢出。*/
  19. .demo1 > .item:nth-of-type(3){
  20. align-self: center;
  21. }
  22. /*所有的 flex 元素会沿着基线对齐*/
  23. .demo1 > .item:nth-of-type(4){
  24. align-self: baseline;
  25. }
  26. /*flex 元素会对齐到的终点*/
  27. .demo1 > .item:nth-of-type(4){
  28. align-self: flex-end;
  29. }
  30. /*flex 元素将会基于容器的宽和高,按照自身 margin box 的 cross-size 拉伸。*/
  31. .demo1 > .item:last-of-type{
  32. align-self: stretch;
  33. }
  34. /*order用法-定义项目的排列顺序。数值越小,排列越靠前,默认为0*/
  35. .demo2{
  36. width: 500px;
  37. flex-flow: row nowrap;
  38. }
  39. .demo2 > .item:nth-of-type(2){
  40. order: -1;
  41. }

运行效果

手抄代码

4.修改圣杯布局

  1. header,footer{
  2. background-color: #999;
  3. height: 40px;
  4. }
  5. main{
  6. display: flex;
  7. height: 300px;
  8. }
  9. main > article{
  10. background-color: lightgreen;
  11. /*分配所有空间*/
  12. flex: 1;
  13. }
  14. main > aside:first-of-type{
  15. background-color: lightpink;
  16. flex: 0 1 200px;
  17. order: -1;
  18. }
  19. main > aside:last-of-type{
  20. background-color: lightblue;
  21. flex: 0 1 200px;
  22. }

运行效果

手抄代码

总结:

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
Author's latest blog post