Blogger Information
Blog 12
fans 0
comment 0
visits 8583
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
CSS弹性布局2-flex属性
brait
Original
713 people have browsed it

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

DEMO1:设置元素的增长因子

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>设置弹性元素的增长因子</title>
  6. <link rel="stylesheet" href="css/style1.css">
  7. </head>
  8. <body>
  9. <h1>flex-grow: 设置弹性元素增长因子</h1>
  10. <h3>(1): 所有弹性元素不增长,以原始宽度显示,增长因子为: 0(默认)</h3>
  11. <div class="container flex demo1">
  12. <span class="item">item1</span>
  13. <span class="item">item2</span>
  14. <span class="item">item3</span>
  15. </div>
  16. <h3>(2): 将全部剩余空间分配给指定弹性元素,例如: 第三个</h3>
  17. <div class="container flex demo2">
  18. <span class="item">item1</span>
  19. <span class="item">item2</span>
  20. <span class="item">item3</span>
  21. </div>
  22. <h3>(3): 全部剩余空间按增长因子在不同弹性元素间分配</h3>
  23. <div class="container flex demo3">
  24. <span class="item">item1</span>
  25. <span class="item">item2</span>
  26. <span class="item">item3</span>
  27. </div>
  28. <h3>(4): 增长因子支持小数, 因为是按增长比例分配</h3>
  29. <div class="container flex demo4">
  30. <span class="item">item1</span>
  31. <span class="item">item2</span>
  32. <span class="item">item3</span>
  33. </div>
  34. <h3>(5): 每个弹性元素宽度不同时, 同样适用以上分配规律</h3>
  35. <div class="container flex demo5">
  36. <span class="item">item1</span>
  37. <span class="item">item2</span>
  38. <span class="item">item3</span>
  39. </div>
  40. </body>
  41. </html>

CSS部分:

  1. @import "public.css";
  2. .container {
  3. width: 550px;
  4. }
  5. .item {
  6. width: 100px;
  7. }
  8. .demo1 > .item {
  9. flex-grow: 0;
  10. }
  11. .demo2 > .item:first-of-type {
  12. flex-grow: 0;
  13. }
  14. .demo1 > .item:nth-of-type(2) {
  15. flex-grow: 0;
  16. }
  17. .demo2 > .item:last-of-type {
  18. flex-grow: 1;
  19. }
  20. .demo3 > .item:first-of-type {
  21. flex-grow: 1;
  22. }
  23. .demo3 > .item:nth-of-type(2) {
  24. flex-grow: 1;
  25. }
  26. .demo3 > .item:last-of-type {
  27. flex-grow: 3;
  28. }
  29. .demo4 > .item:first-of-type {
  30. flex-grow: 0.5;
  31. }
  32. .demo4 > .item:nth-of-type(2) {
  33. flex-grow: 0.5;
  34. }
  35. .demo4 > .item:last-of-type {
  36. flex-grow: 1.5;
  37. }
  38. .demo5 > .item:first-of-type {
  39. width: 120px;
  40. flex-grow: 2;
  41. }
  42. .demo5 > .item:nth-of-type(2) {
  43. width: 150px;
  44. flex-grow: 2;
  45. }
  46. .demo5 > .item:last-of-type {
  47. width: 180px;
  48. flex-grow: 6;
  49. }

运行效果:


DEMO2:设置弹性元素的缩减因子

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>设置弹性元素的缩减因子</title>
  6. <link rel="stylesheet" href="css/style2.css">
  7. </head>
  8. <body>
  9. <h1>flex-shrink: 设置弹性元素缩减因子</h1>
  10. <h3>(1): 所有弹性元素不缩减,以原始宽度显示,缩减因子为: 0</h3>
  11. <div class="container flex demo1">
  12. <span class="item">item1</span>
  13. <span class="item">item2</span>
  14. <span class="item">item3</span>
  15. </div>
  16. <h3>(2): 所有弹性元素自适应容器宽度且不换行,缩减因子: 1 (默认)</h3>
  17. <div class="container flex demo2">
  18. <span class="item">item1</span>
  19. <span class="item">item2</span>
  20. <span class="item">item3</span>
  21. </div>
  22. <h3>(3): 当三个弹性元素的缩减因为子不相等时</h3>
  23. <div class="container flex demo3">
  24. <span class="item">item1</span>
  25. <span class="item">item2</span>
  26. <span class="item">item3</span>
  27. </div>
  28. <h3>(4): 缩减因子也可以是小数,只要大于就可以</h3>
  29. <div class="container flex demo4">
  30. <span class="item">item1</span>
  31. <span class="item">item2</span>
  32. <span class="item">item3</span>
  33. </div>
  34. <h3>(5): 当每个弹性元素宽度不一样时, 完全是另一道风景线</h3>
  35. <div class="container flex demo5">
  36. <span class="item">item1</span>
  37. <span class="item">item2</span>
  38. <span class="item">item3</span>
  39. </div>
  40. </body>
  41. </html>

CSS部分:

  1. @import "public.css";
  2. .container {
  3. width: 550px;
  4. }
  5. .item {
  6. width: 250px;
  7. }
  8. .demo1 > .item {
  9. flex-shrink: 0;
  10. }
  11. .demo2 > .item {
  12. flex-shrink: 1;
  13. }
  14. .demo3 > .item:first-of-type {
  15. flex-shrink: 1;
  16. }
  17. .demo3 > .item:nth-of-type(2) {
  18. flex-shrink: 2;
  19. }
  20. .demo3 > .item:last-of-type {
  21. flex-shrink: 3;
  22. }
  23. .demo4 > .item:first-of-type {
  24. flex-shrink: 0.2;
  25. }
  26. .demo4 > .item:nth-of-type(2) {
  27. flex-shrink: 0.3;
  28. }
  29. .demo4 > .item:last-of-type {
  30. flex-shrink: 0.5;
  31. }
  32. .demo5 > .item:first-of-type {
  33. width: 220px;
  34. flex-shrink: 2;
  35. }
  36. .demo5 > .item:nth-of-type(2) {
  37. width: 250px;
  38. flex-shrink: 2;
  39. }
  40. .demo5 > .item:last-of-type {
  41. width: 280px;
  42. flex-shrink: 6;
  43. }

运行效果:


DEMO3:设置弹性元素的基准尺寸

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>设置弹性元素的基准尺寸</title>
  6. <link rel="stylesheet" href="css/style3.css">
  7. </head>
  8. <body>
  9. <h1>flex-basis: 设置弹性元素的基准尺寸</h1>
  10. <h3>(1): 在未设置弹性元素宽度时, 以内容宽度显示</h3>
  11. <div class="container flex demo1">
  12. <span class="item">item1</span>
  13. <span class="item">item2</span>
  14. <span class="item">item3</span>
  15. </div>
  16. <h3>(2): 存在自定义元素宽度时,则以该宽度显示</h3>
  17. <div class="container flex demo2">
  18. <span class="item">item1</span>
  19. <span class="item">item2</span>
  20. <span class="item">item3</span>
  21. </div>
  22. <h3>(3): 自动状态下, 由浏览器根据预设值自行判定</h3>
  23. <div class="container flex demo3">
  24. <span class="item">item1</span>
  25. <span class="item">item2</span>
  26. <span class="item">item3</span>
  27. </div>
  28. <h3>(4): 当元素存在自定义宽度与基准宽度时, 以基准宽度为准 </h3>
  29. <div class="container flex demo4">
  30. <span class="item">item1</span>
  31. <span class="item">item2</span>
  32. <span class="item">item3</span>
  33. </div>
  34. <h3>(5): 元素基准宽度支持百分比设置 </h3>
  35. <div class="container flex demo5">
  36. <span class="item">item1</span>
  37. <span class="item">item2</span>
  38. <span class="item">item3</span>
  39. </div>
  40. </body>
  41. </html>

CSS部分:

  1. @import "public.css";
  2. .container {
  3. width: 550px;
  4. }
  5. .demo1 > .item {
  6. flex-basis: content;
  7. }
  8. .demo2 > .item {
  9. width: 100px;
  10. }
  11. .demo3 > .item {
  12. flex-basis: auto;
  13. }
  14. .demo4 > .item {
  15. width: 100px;
  16. flex-basis: 150px;
  17. }
  18. .demo5 > :first-child {
  19. flex-basis: 20%;
  20. }
  21. .demo5 > :nth-child(2) {
  22. flex-basis: 30%;
  23. }
  24. .demo5 > :last-child {
  25. flex-basis: 50%;
  26. }

运行效果:


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

3. 自学:align-self, order的用法

1.align-self

  1. 语法:
  2. align-self: auto
  3. align-self: flex-start
  4. align-self: flex-end
  5. align-self: center
  6. align-self: baseline
  7. align-self: stretch
  8. 备注:写这个部分的时候搞混了,align-items 为弹性容器中所有项目设置缺省对齐属性,包括匿名弹性项目。align-self 可以为单独的弹性项目设置对齐来覆盖缺省值。实例做的是align-items

实例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>设置弹性元素的align-self</title>
  6. <link rel="stylesheet" href="css/style1.css">
  7. </head>
  8. <body>
  9. <p class="label start">flex-start:</p>
  10. <div class="flex">
  11. <div class="item">1</div>
  12. <div class="item">2</div>
  13. <div class="item">3</div>
  14. <div class="item">4</div>
  15. </div>
  16. <p class="label center">center:</p>
  17. <div class="flex center">
  18. <div class="item">1</div>
  19. <div class="item">2</div>
  20. <div class="item">3</div>
  21. <div class="item">4</div>
  22. </div>
  23. <p class="label end">flex-end:</p>
  24. <div class="flex end">
  25. <div class="item">1</div>
  26. <div class="item">2</div>
  27. <div class="item">3</div>
  28. <div class="item">4</div>
  29. </div>
  30. <p class="label baseline">baseline:</p>
  31. <div class="flex baseline">
  32. <div class="item">1</div>
  33. <div class="item">2</div>
  34. <div class="item">3</div>
  35. <div class="item">4</div>
  36. </div>
  37. <p class="label stretch">stretch:</p>
  38. <div class="flex stretch">
  39. <div class="item s">1</div>
  40. <div class="item s">2</div>
  41. <div class="item s">3</div>
  42. <div class="item s">4</div>
  43. </div>
  44. </body>
  45. </html>

CSS部分:

  1. .flex {
  2. display: flex;
  3. align-items: flex-start;
  4. min-height: 100px;
  5. }
  6. .flex.center {
  7. align-items: center;
  8. }
  9. .flex.end {
  10. align-items: flex-end;
  11. }
  12. .flex.baseline {
  13. align-items: baseline;
  14. }
  15. .flex.stretch {
  16. align-items: stretch;
  17. /*default value*/
  18. }
  19. .item {
  20. width: 80px;
  21. box-sizing: border-box;
  22. }
  23. .flex >.item:nth-child(1) {
  24. height: 60px;
  25. }
  26. .flex >.item:nth-child(2) {
  27. background: orange;
  28. height: 80px;
  29. font-size: 22px;
  30. }
  31. .flex >.item:nth-child(3) {
  32. height: 50px;
  33. }
  34. .flex >.item:nth-child(4) {
  35. background: green;
  36. height: 70px;
  37. font-size: 32px;
  38. }
  39. .flex >.item.s {
  40. height: auto;
  41. }
  42. /* other styles just for format */
  43. .flex {
  44. background: #333;
  45. margin-bottom: 10px;
  46. }
  47. .flex.stretch {
  48. margin-bottom: 60px;
  49. }
  50. .item {
  51. padding: 5px;
  52. background: #999;
  53. text-align: center;
  54. }
  55. .label {
  56. font-size: 18px;
  57. }

运行效果:



2.order (用来排序)

  1. 语法 order:数字; 数字越小,顺序越靠前

实例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <link rel="stylesheet" href="css/style1.css">
  6. </head>
  7. <body>
  8. <p class="label start">排列顺序为模块中数字2314:</p>
  9. <div class="flex">
  10. <div class="item">1</div>
  11. <div class="item">2</div>
  12. <div class="item">3</div>
  13. <div class="item">4</div>
  14. </div>
  15. </body>
  16. </html>

CSS部分:

  1. .flex {
  2. display: flex;
  3. align-items: flex-start;
  4. min-height: 100px;
  5. }
  6. .item {
  7. width: 80px;
  8. box-sizing: border-box;
  9. }
  10. .flex >.item:nth-child(1) {
  11. height: 60px;
  12. order: 2;
  13. }
  14. .flex >.item:nth-child(2) {
  15. background: orange;
  16. height: 80px;
  17. font-size: 22px;
  18. order: 0;
  19. }
  20. .flex >.item:nth-child(3) {
  21. height: 50px;
  22. order: 1;
  23. }
  24. .flex >.item:nth-child(4) {
  25. background: green;
  26. height: 70px;
  27. font-size: 32px;
  28. order: 3;
  29. }
  30. .flex >.item{
  31. height: auto;
  32. }
  33. /* other styles just for format */
  34. .flex {
  35. background: #333;
  36. margin-bottom: 10px;
  37. }
  38. .item {
  39. padding: 5px;
  40. background: #999;
  41. text-align: center;
  42. }
  43. .label {
  44. font-size: 18px;
  45. }

运行效果:


4. 试着将之前的一些案例用flex布局改定, 例如圣杯布局

实例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>用flex做圣杯布局</title>
  6. <link rel="stylesheet" href="css/style1.css">
  7. </head>
  8. <h3>用flex做圣杯布局</h3>
  9. <body>
  10. <div class="container">
  11. <div class="head">head</div>
  12. <div class="main">
  13. <div class="aside">left</div>
  14. <div class="article">article</div>
  15. <div class="aside">right</div>
  16. </div>
  17. <div class="footer">footer</div>
  18. </div>
  19. </body>
  20. </html>

CSS部分:

  1. .container{
  2. display: flex;
  3. flex-direction: column;
  4. height: 800px;
  5. }
  6. .head,.footer{
  7. background-color: gray;
  8. width: 80%;
  9. flex-grow: 1;
  10. }
  11. .main{
  12. display: flex;
  13. flex-direction: row;
  14. width: 80%;
  15. flex-grow: 8;
  16. }
  17. .aside{
  18. background-color: green;
  19. width: 30px;
  20. flex-grow: 2;
  21. }
  22. .article{
  23. background-color:goldenrod ;
  24. width: 80px;
  25. flex-grow: 6;
  26. }

运行效果



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