CSS3 flex布局和box布局详解

电玩女神 2022-05-21 09:50 413阅读 0赞

弹性盒子布局是现在十分流行的布局方式,能让我们更好地操作子元素的布局。但是要注意,元素设置为flex布局以后,子元素的float、clear、vertical-align属性会失效

flex的布局很多人都用过,但是对于box可能大多数人不了解,其实这两个都是弹性盒子的布局,各阶段的草案命名而已,用法上也是大同小异,可以根据个人习惯任选一种直接复制进flex文件,希望能够方便各位小伙伴

flex布局常用属性.

  1. .g-flex {
  2. display: flex;
  3. }
  4. .g-flex-item {
  5. flex: 1;
  6. }
  7. /*垂直居中*/
  8. .g-vcenter {
  9. display: flex;
  10. align-items: center;
  11. }
  12. /*下对齐*/
  13. .g-bottom {
  14. display: flex;
  15. align-items: flex-end;
  16. }
  17. /*上对齐*/
  18. .g-top {
  19. display: flex;
  20. align-items: flex-start;
  21. }
  22. /*水平居中*/
  23. .g-hcenter {
  24. display: flex;
  25. justify-content: center;
  26. }
  27. /*垂直并且水平居中*/
  28. .g-center {
  29. display: flex;
  30. justify-content: center;
  31. align-items: center;
  32. }
  33. /*模块居于末尾*/
  34. .g-end {
  35. display: flex;
  36. justify-content: flex-end;
  37. }
  38. /*模块两端对齐*/
  39. .g-between {
  40. display: flex;
  41. justify-content: space-between;
  42. }
  43. /*模块两端对齐并且垂直居中*/
  44. .g-between-center {
  45. display: flex;
  46. justify-content: space-between;
  47. align-items: center;
  48. }
  49. /*模块两端对齐并且上对齐*/
  50. .g-between-top {
  51. display: flex;
  52. justify-content: space-between;
  53. align-items: start;
  54. }
  55. /*模块两端对齐并且下对齐*/
  56. .g-between-bottom {
  57. display: flex;
  58. justify-content: space-between;
  59. align-items: flex-end;
  60. }
  61. /*弹性布局 纵向*/
  62. .g-flex-column {
  63. flex-direction: column;
  64. }
  65. /*弹性布局 横向*/
  66. .g-flex-row {
  67. flex-direction: row;
  68. }
  69. /*换行*/
  70. .g-flex-wrap {
  71. flex-wrap: wrap;
  72. }
  73. /*转化成行元素*/
  74. .g-inlineblock {
  75. display: inline-block;
  76. }
  77. /*转化成块元素*/
  78. .g-block {
  79. display: block;
  80. }
  81. /*文字居中*/
  82. .g-text-center {
  83. text-align: center;
  84. }
  85. /*绝对定位 垂直居中*/
  86. .g-absolute-vcenter {
  87. position: absolute;
  88. top: 50%;
  89. left: 0;
  90. transform: translateY(-50%);
  91. }
  92. /*绝对定位 水平居中*/
  93. .g-absolute-hcenter {
  94. position: absolute;
  95. left: 50%;
  96. transform: translateX(-50%);
  97. }
  98. /*绝对定位 居中*/
  99. .g-absolute-center {
  100. position: absolute;
  101. top: 50%;
  102. left: 50%;
  103. transform: translate(-50%, -50%);
  104. }
  105. /*禁止用户选中*/
  106. .g-not-select {
  107. -webkit-user-select: none;
  108. }

box布局常用属性

  1. .ub
  2. {
  3. display: -webkit-box !important;
  4. display: box !important;
  5. position:relative;
  6. }
  7. .ub-rev
  8. {
  9. -webkit-box-direction:reverse;
  10. box-direction:reverse;
  11. }
  12. .ub-ac
  13. {
  14. -webkit-box-align:center;
  15. box-align:center;
  16. }
  17. .ub-ae
  18. {
  19. -webkit-box-align:end;
  20. box-align:end;
  21. }
  22. .ub-pc
  23. {
  24. -webkit-box-pack:center;
  25. box-pack:center;
  26. }
  27. .ub-pe
  28. {
  29. -webkit-box-pack:end;
  30. box-pack:end;
  31. }
  32. .ub-pj
  33. {
  34. -webkit-box-pack:justify;
  35. box-pack:justify;
  36. }
  37. .ub-ver
  38. {
  39. -webkit-box-orient:vertical;
  40. box-orient:vertical;
  41. }
  42. .ub-f1
  43. {
  44. position:relative;
  45. -webkit-box-flex: 1;
  46. box-flex: 1;
  47. }
  48. .ub-f2
  49. {
  50. position:relative;
  51. -webkit-box-flex: 2;
  52. box-flex: 2;
  53. }

发表评论

表情:
评论列表 (有 0 条评论,413人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CSS3--Flex布局

    Flex布局 CSS2传统的盒模型采用float – position布局方式。【PC端】 CSS3的弹性盒模型采用Flex布局方式。【移动端、响应式Bootst...

    相关 css3-flex布局

    第1章 flex布局 Flex布局(伸缩布局盒模型)是CSS3提出的一种新的布局方案,可以简便、完整、响应式地实现各种页面布局。 任何一个容器都可以指定为F