博客列表 >1222 flex的简单练习

1222 flex的简单练习

fanyigle_php
fanyigle_php原创
2021年02月13日 20:22:30745浏览
  1. 定位的布局思路练习

    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <title>定位的布局思路练习</title>
    7. <!--
    8. 1.确定文档结构
    9. 2.样式的初始化
    10. 3.分块的样式设定
    11. -->
    12. <style>
    13. * {
    14. margin: 0;
    15. padding: 0;
    16. box-sizing: border-box;
    17. }
    18. header {
    19. background-color: antiquewhite;
    20. min-height: 10em;
    21. }
    22. .container {
    23. min-height: 20em;
    24. position: relative;
    25. margin: 1em 0;
    26. }
    27. .container aside:first-of-type {
    28. min-height: inherit;
    29. width: 5em;
    30. background-color: hotpink;
    31. position: absolute;
    32. top: 0;
    33. left: 0;
    34. }
    35. .container aside:last-of-type {
    36. min-height: inherit;
    37. width: 5em;
    38. background-color: darkgoldenrod;
    39. position: absolute;
    40. top: 0;
    41. right: 0;
    42. }
    43. .container main {
    44. min-height: inherit;
    45. background-color: darkmagenta;
    46. /* padding: 0 5em; */
    47. position: absolute;
    48. left: 5em;
    49. right: 5em;
    50. margin: 0 0.5em;
    51. }
    52. footer {
    53. background-color: aquamarine;
    54. min-height: 10em;
    55. }
    56. </style>
    57. </head>
    58. <body>
    59. <header>头部</header>
    60. <div class="container">
    61. <aside>左侧</aside>
    62. <main>主题</main>
    63. <aside>右侧</aside>
    64. </div>
    65. <footer>尾部</footer>
    66. </body>
    67. </html>
  2. flex实现水平和垂直居中

    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <title>flex实现水平和垂直居中</title>
    7. <style>
    8. .box {
    9. width: 15em;
    10. height: 15em;
    11. background-color: aquamarine;
    12. position: relative;
    13. }
    14. .box .item {
    15. width: 5em;
    16. height: 5em;
    17. background-color: yellow;
    18. position: absolute;
    19. }
    20. /* .box .item {
    21. width: 5em;
    22. height: 5em;
    23. background-color: yellow;
    24. position: absolute;
    25. left: 0;
    26. top: 0;
    27. right: 0;
    28. bottom: 0;
    29. margin: auto;
    30. } */
    31. .box {
    32. display: flex;
    33. justify-content: center;
    34. align-items: center;
    35. }
    36. </style>
    37. </head>
    38. <body>
    39. <div class="box">
    40. <div class="item"></div>
    41. </div>
    42. </body>
    43. </html>
  3. 用flex写三列

    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <title>flex写三列</title>
    7. <style>
    8. * {
    9. box-sizing: border-box;
    10. }
    11. /*实体元素改色可见*/
    12. body *:not(.container) {
    13. background-color: antiquewhite;
    14. }
    15. header,
    16. footer {
    17. height: 10vh;
    18. }
    19. .container {
    20. height: calc(80vh - 1em);
    21. margin: 0.5em 0;
    22. display: flex;
    23. }
    24. .container aside {
    25. min-width: 15em;
    26. }
    27. .container main {
    28. min-width: calc(100vw - 30em - 2em);
    29. margin: 0 1em;
    30. }
    31. </style>
    32. </head>
    33. <body>
    34. <header>头部</header>
    35. <div class="container">
    36. <aside>左侧</aside>
    37. <main>主题</main>
    38. <aside>右侧</aside>
    39. </div>
    40. <footer>尾部</footer>
    41. </body>
    42. </html>
  4. grid写三行三列

    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <title>grid写三行三列</title>
    7. <style>
    8. body {
    9. height: 100vh;
    10. display: grid;
    11. grid-template-columns: 10em 1fr 10rem;
    12. grid-template-rows: 10vh 1fr 10vh;
    13. gap: 0.5em;
    14. }
    15. body > * {
    16. background-color: aqua;
    17. }
    18. header,
    19. footer {
    20. grid-column: span 3;
    21. }
    22. </style>
    23. </head>
    24. <body>
    25. <header>头部</header>
    26. <aside>左侧</aside>
    27. <main>主题</main>
    28. <aside>右侧</aside>
    29. <footer>尾部</footer>
    30. </body>
    31. </html>
  5. 弹性容器与子项目

    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <title>弹性容器与子项目</title>
    7. <style>
    8. * {
    9. box-sizing: border-box;
    10. }
    11. .container {
    12. display: flex;
    13. height: 20em;
    14. padding: 1em;
    15. border: 1em dotted blue;
    16. }
    17. .item {
    18. width: 10em;
    19. height: 6em;
    20. border: 1px solid red;
    21. }
    22. .item:last-of-type {
    23. display: flex;
    24. }
    25. .subitem {
    26. border: 2px dashed green;
    27. }
    28. </style>
    29. </head>
    30. <body>
    31. <div class="container">
    32. <div class="item">item1</div>
    33. <div class="item">item2</div>
    34. <div class="item">
    35. item3
    36. <div class="subitem">subitem1</div>
    37. <div class="subitem">subitem2</div>
    38. <div class="subitem">subitem3</div>
    39. </div>
    40. </div>
    41. </body>
    42. </html>
  6. 项目的排列和换行
    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <title>项目的排列和换行</title>
    7. <style>
    8. .container {
    9. display: flex;
    10. /* flex-direction: row;
    11. flex-wrap: wrap; */
    12. flex-flow: row wrap;
    13. height: 20em;
    14. /* width: 10em; */
    15. padding: 1em;
    16. border: 2px solid green;
    17. }
    18. .item {
    19. width: 5em;
    20. height: 4em;
    21. border: 1px solid red;
    22. }
    23. </style>
    24. </head>
    25. <body>
    26. <div class="container">
    27. <div class="item">item1</div>
    28. <div class="item">item2</div>
    29. <div class="item">item3</div>
    30. <div class="item">item4</div>
    31. <div class="item">item5</div>
    32. <div class="item">item6</div>
    33. <div class="item">item7</div>
    34. <div class="item">item8</div>
    35. </div>
    36. </body>
    37. </html>
  7. 项目在容器内的横纵对齐方式

    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <title>项目在容器内的横纵对齐方式</title>
    7. <!--
    8. 对齐解决的是项目和留白区域的划分
    9. 1.项目在主轴上和交叉轴上有剩余空间则可以使用对齐,调整项目的位置;
    10. -->
    11. <style>
    12. .container {
    13. display: flex;
    14. /* flex-direction: row;
    15. flex-wrap: wrap; */
    16. flex-flow: row wrap;
    17. height: 20em;
    18. /* width: 10em; */
    19. padding: 1em;
    20. border: 2px solid green;
    21. }
    22. .item {
    23. width: 5em;
    24. height: 4em;
    25. border: 1px solid red;
    26. }
    27. .container {
    28. /*主轴上的对齐方式,水平移动*/
    29. /*1.所有项目无间隙排列*/
    30. justify-content: flex-start;
    31. justify-content: flex-end;
    32. justify-content: center;
    33. /*2.每个项目左右都有间隙*/
    34. justify-content: space-around;
    35. justify-content: space-between;
    36. justify-content: space-evenly;
    37. /*交叉轴上的对齐,垂直移动*/
    38. align-items: stretch;
    39. align-items: flex-start;
    40. align-items: flex-end;
    41. align-items: center;
    42. }
    43. </style>
    44. </head>
    45. <body>
    46. <div class="container">
    47. <div class="item">item1</div>
    48. <div class="item">item2</div>
    49. <div class="item">item3</div>
    50. <div class="item">item4</div>
    51. <div class="item">item5</div>
    52. <div class="item">item6</div>
    53. <div class="item">item7</div>
    54. </div>
    55. </body>
    56. </html>
  8. 多行项目在容器内的横纵对齐方式

    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <title>多行项目在容器内的横纵对齐方式</title>
    7. <!--
    8. 对齐解决的是项目和留白区域的划分
    9. 1.项目在主轴上和交叉轴上有剩余空间则可以使用对齐,调整项目的位置;
    10. -->
    11. <style>
    12. .container {
    13. display: flex;
    14. /* flex-direction: row;
    15. flex-wrap: wrap; */
    16. flex-flow: row wrap;
    17. height: 20em;
    18. /* width: 10em; */
    19. padding: 1em;
    20. border: 2px solid green;
    21. }
    22. .item {
    23. width: 5em;
    24. height: 4em;
    25. border: 1px solid red;
    26. }
    27. .container {
    28. /*主轴上的对齐方式,水平移动*/
    29. /*1.所有项目无间隙排列*/
    30. justify-content: flex-start;
    31. justify-content: flex-end;
    32. justify-content: center;
    33. /*2.每个项目左右都有间隙*/
    34. /* justify-content: space-around;
    35. justify-content: space-between;*/
    36. justify-content: space-evenly;
    37. /*交叉轴上的对齐,垂直移动*/
    38. /*align-items先切割成多行,然后处理每行的交叉轴对齐*/
    39. align-items: stretch;
    40. align-items: flex-start;
    41. align-items: flex-end;
    42. /* align-items: center; */
    43. /*多行项目沿交叉轴无间隙,整体移动*/
    44. /* align-content: flex-end;
    45. align-content: flex-start; */
    46. /* align-content: center; */
    47. /*多行项目沿交叉轴有间隙调整*/
    48. /* align-content: space-between;
    49. align-content: space-evenly;*/
    50. align-content: space-around;
    51. }
    52. </style>
    53. </head>
    54. <body>
    55. <div class="container">
    56. <div class="item">item1</div>
    57. <div class="item">item2</div>
    58. <div class="item">item3</div>
    59. <div class="item">item4</div>
    60. <div class="item">item5</div>
    61. <div class="item">item6</div>
    62. <div class="item">item7</div>
    63. <div class="item">item8</div>
    64. <div class="item">item9</div>
    65. <div class="item">item10</div>
    66. <div class="item">item11</div>
    67. <div class="item">item12</div>
    68. </div>
    69. </body>
    70. </html>
  9. 项目的flex属性\order属性\align-self属性

    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <title>项目的flex属性\order属性\align-self属性</title>
    7. <!--改变项目大小\交叉轴对齐\在容器中的顺序-->
    8. <style>
    9. .container {
    10. display: flex;
    11. /* flex-direction: row;
    12. flex-wrap: wrap; */
    13. flex-flow: row nowrap;
    14. height: 20em;
    15. /* width: 10em; */
    16. padding: 1em;
    17. border: 2px solid green;
    18. }
    19. .item {
    20. width: 5em;
    21. /* height: 4em; */
    22. border: 1px solid red;
    23. }
    24. .container .item {
    25. /* 默认值 */
    26. flex: 0 1 auto;
    27. flex: initial;
    28. /*允许放大*/
    29. flex: 1 1 auto;
    30. flex: auto;
    31. flex: 1;
    32. /*禁止放大和缩小*/
    33. flex: 0 0 auto;
    34. flex: none;
    35. /*test*/
    36. flex: 3;
    37. }
    38. .container > .item:last-of-type,
    39. .container > .item:nth-of-type(2) {
    40. flex: 1;
    41. }
    42. /*设置某一个项目在交叉轴上的对齐方式*/
    43. .container > .item:first-child {
    44. align-self: flex-start;
    45. }
    46. /*项目的order属性*/
    47. .container .item:nth-child(2) {
    48. order: -1;
    49. }
    50. </style>
    51. </head>
    52. <body>
    53. <div class="container">
    54. <div class="item">item1</div>
    55. <div class="item">item2</div>
    56. <div class="item">item3</div>
    57. <div class="item">item4</div>
    58. <div class="item">item5</div>
    59. <div class="item">item6</div>
    60. <div class="item">item7</div>
    61. <div class="item">item8</div>
    62. <div class="item">item9</div>
    63. </div>
    64. </body>
    65. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议