博客列表 >实例演示flex常用的6个属性

实例演示flex常用的6个属性

P粉314265155
P粉314265155原创
2022年07月13日 13:24:54531浏览

flex必须知道的三个属性

页面布局

  1. @import '../css/reset.css';
  2. /* 页眉页脚 */
  3. /* body header,
  4. body footer{
  5. background-color: aqua;
  6. height: 50px;
  7. } */
  8. body header,
  9. body footer {
  10. height: 50px;
  11. background-color: lightgreen;
  12. }
  13. body .wrap {
  14. max-width: 1000px;
  15. min-height: 1500px;
  16. margin: 10px auto;
  17. background-color: blue;
  18. /* 相对定位 */
  19. position: relative;
  20. top: 50px;
  21. }
  22. body header{
  23. position: fixed;
  24. right: 0;
  25. left: 0;
  26. top: 0;
  27. /* 让导航栏固定在首页,不会受到主体挡住 */
  28. z-index: 100;
  29. }
  30. body .wrap .left,
  31. body .wrap .right,
  32. body .wrap .content{
  33. background-color: olive;
  34. min-width: 200px;
  35. /* 继承父级高度 */
  36. min-height: inherit;
  37. position: absolute;
  38. }
  39. body .wrap .left{
  40. top:0;
  41. left: 0;
  42. }
  43. body .wrap .right{
  44. top:0;
  45. right: 0;
  46. }
  47. body .wrap .content{
  48. top: 0;
  49. left: calc(200px + 10px);
  50. right: calc(200px + 10px);
  51. margin: 0 auto ;
  52. }
  53. body footer{
  54. position: fixed;
  55. left: 0;
  56. right: 0;
  57. bottom: 0;
  58. }

剩余空间的分配

  1. @import '../css/reset.css';
  2. /* 页眉页脚 */
  3. /* body header,
  4. body footer{
  5. background-color: aqua;
  6. height: 50px;
  7. } */
  8. body header,
  9. body footer {
  10. height: 50px;
  11. background-color: lightgreen;
  12. }
  13. body{
  14. display: flex;
  15. /* flex-direction: column; */
  16. /* flex-direction: row; =flex-flow */
  17. /* flex-flow: column; */
  18. flex-flow: nowrap;
  19. flex-flow: column;
  20. flex-flow: nowrap;
  21. /* flex-flow: column nowrap; */
  22. }
  23. body .wrap{
  24. margin: 10px auto;
  25. width: 1000px;
  26. min-width: 200px;
  27. display: flex;
  28. place-content: space-between;
  29. min-height: 200px;
  30. /* width: 1000px;
  31. min-width: 600px;
  32. display: flex;
  33. place-content: space-between;
  34. min-height: 1500px; */
  35. }

项目间的主轴平均分配

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=ww, initial-scale=1.0">
  7. <title>flex 布局术语、容器、项目(子元素)</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. border: 2px solid yellow;
  14. }
  15. .contatiner{
  16. /* flex 弹性盒子 从垂直排列改为水平排列主轴排列
  17. block 块级盒子
  18. inline 内联盒子
  19. */
  20. display: flex;
  21. background-color: aquamarine;
  22. border: 2px solid red ;
  23. }
  24. .contatiner > .item {
  25. background-color: hotpink;
  26. width: 50px;
  27. border: 1px solid olive;
  28. }
  29. .contatiner {
  30. /* 主轴方向 默认是行排版,支持列排版 column */
  31. flex-direction: row;
  32. /* 项目在主轴排列是否允许换行,默认是不换行的 nowrap
  33. 不允许自动放到、缩小
  34. */
  35. /* flex-wrap: wrap; */
  36. flex-wrap: nowrap;
  37. /* 简写 */
  38. /* flex-flow: =flex-wrap flex-direction;
  39. 默认值 row nowrap
  40. */
  41. /* flex-flow: column;
  42. flex-flow: row; */
  43. /* flex-flow: row-reverse; */
  44. /* flex-flow: row wrap; */
  45. flex-flow: row nowrap;
  46. /* 剩余空间的分配 左右分 左边 start 右边end*/
  47. place-content: start;
  48. place-content: end;
  49. /*剩余空间在 左右 两边平均分配 */
  50. place-content: center;
  51. /* 剩余空间在所有项目(子集)间分配 ,左右两边不分配*/
  52. place-content: space-between;
  53. /* 剩余空间在所有项目(子集)间分配 ,左右两边分配\分散对齐*/
  54. place-content: space-around;
  55. /* 剩余空间在所有项目(子集)间分配 ,左右两边分配\分散对齐
  56. 两边相同 平均分配*/
  57. place-content: space-evenly;
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <div class="contatiner">
  63. <div class="item">item1</div>
  64. <div class="item">item2</div>
  65. <div class="item">item3</div>
  66. </div>
  67. </body>
  68. </html>

![](https://img.php.cn/upload/image/771/526/448/1657688273994704.jpg)

![![](https://img.php.cn/upload/image/359/180/962/1657688308897611.jpg)](https://img.php.cn/upload/image/217/202/192/1657688301543510.jpg)

项目在交叉轴上的对齐

  1. /* place-items: 交叉轴对齐;
  2. */
  3. /* 默认值,自动伸展 */
  4. /* height: 5em; */
  5. /* place-items: stretch; */
  6. /* 上对其start 下对齐end */
  7. place-items: start;

项目在页面的布局,响应或者不响应

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>flex 项目子集上的常用属性</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. border: 2px solid yellow;
  14. }
  15. .contatiner{
  16. /* flex 弹性盒子 从垂直排列改为水平排列主轴排列
  17. block 块级盒子
  18. inline 内联盒子
  19. */
  20. height: 10em;
  21. display: flex;
  22. background-color: aquamarine;
  23. border: 2px solid red ;
  24. }
  25. .contatiner > .item {
  26. background-color: hotpink;
  27. width: 5em;
  28. border: 1px solid olive;
  29. min-width: 8em;
  30. /* flex 放大因子、收缩因子 计算宽度 */
  31. /* 因为宽度设置值,所以auto引用父级 */
  32. /* flex:0 1 auto; m默认值 */
  33. /* flex:0 1 auto; */
  34. /* flex:0 1 7em; */
  35. /* min-width > flex.width> item.width */
  36. flex:inherit;
  37. /* flex:1 1 auto;浏览器自动计算大小
  38. 完全响应式flex:1 1 auto; =flex :auto
  39. */
  40. flex:1 1 auto;
  41. /* pc失去响应,禁止放大缩小 flex: 0 0 auto; =flex :none*/
  42. flex: 0 0 auto;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <body>
  48. <div class="contatiner">
  49. <div class="item">item1</div>
  50. <div class="item">item2</div>
  51. <div class="item">item3</div>
  52. </div>
  53. </body>
  54. </html>

项目子集排列顺序

  1. /* 改变项目、子集展示顺序 order 默认值是1
  2. 越小越靠前*/
  3. .contatiner > .item:first-of-type{
  4. background-color: brown;
  5. order: 2;
  6. }
  7. .contatiner > .item:last-of-type{
  8. background-color: blue;
  9. order: 5;
  10. }

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议