Flex Flex布局

Flex 是 CSS flex 布局的一个封装。

代码演示

基本示例

基础使用

  1. import { Flex, WhiteSpace } from 'antd-mobile';
  2. const PlaceHolder = ({ className = '', ...restProps }) => (
  3. <div className={`${className} placeholder`} {...restProps}>Block</div>
  4. );
  5. const FlexExample = () => (
  6. <div className="flex-container">
  7. <div className="sub-title">Basic</div>
  8. <Flex>
  9. <Flex.Item><PlaceHolder /></Flex.Item>
  10. <Flex.Item><PlaceHolder /></Flex.Item>
  11. </Flex>
  12. <WhiteSpace size="lg" />
  13. <Flex>
  14. <Flex.Item><PlaceHolder /></Flex.Item>
  15. <Flex.Item><PlaceHolder /></Flex.Item>
  16. <Flex.Item><PlaceHolder /></Flex.Item>
  17. </Flex>
  18. <WhiteSpace size="lg" />
  19. <Flex>
  20. <Flex.Item><PlaceHolder /></Flex.Item>
  21. <Flex.Item><PlaceHolder /></Flex.Item>
  22. <Flex.Item><PlaceHolder /></Flex.Item>
  23. <Flex.Item><PlaceHolder /></Flex.Item>
  24. </Flex>
  25. <WhiteSpace size="lg" />
  26. <div className="sub-title">Wrap</div>
  27. <Flex wrap="wrap">
  28. <PlaceHolder className="inline" />
  29. <PlaceHolder className="inline" />
  30. <PlaceHolder className="inline" />
  31. <PlaceHolder className="inline" />
  32. <PlaceHolder className="inline" />
  33. <PlaceHolder className="inline" />
  34. <PlaceHolder className="inline" />
  35. </Flex>
  36. <WhiteSpace size="lg" />
  37. <div className="sub-title">Align</div>
  38. <Flex justify="center">
  39. <PlaceHolder className="inline" />
  40. <PlaceHolder className="inline" />
  41. <PlaceHolder className="inline" />
  42. </Flex>
  43. <WhiteSpace />
  44. <Flex justify="end">
  45. <PlaceHolder className="inline" />
  46. <PlaceHolder className="inline" />
  47. <PlaceHolder className="inline" />
  48. </Flex>
  49. <WhiteSpace />
  50. <Flex justify="between">
  51. <PlaceHolder className="inline" />
  52. <PlaceHolder className="inline" />
  53. <PlaceHolder className="inline" />
  54. </Flex>
  55. <WhiteSpace />
  56. <Flex align="start">
  57. <PlaceHolder className="inline" />
  58. <PlaceHolder className="inline small" />
  59. <PlaceHolder className="inline" />
  60. </Flex>
  61. <WhiteSpace />
  62. <Flex align="end">
  63. <PlaceHolder className="inline" />
  64. <PlaceHolder className="inline small" />
  65. <PlaceHolder className="inline" />
  66. </Flex>
  67. <WhiteSpace />
  68. <Flex align="baseline">
  69. <PlaceHolder className="inline" />
  70. <PlaceHolder className="inline small" />
  71. <PlaceHolder className="inline" />
  72. </Flex>
  73. </div>
  74. );
  75. ReactDOM.render(<FlexExample />, mountNode);
  1. .flex-container {
  2. margin: 0 15px;
  3. }
  4. .flex-container .inline {
  5. width: 80px!important;
  6. margin: 9px 9px 9px 0;
  7. }
  8. .flex-container .small {
  9. height: 20px!important;
  10. line-height: 20px!important;
  11. }
  12. .sub-title {
  13. color: #888;
  14. font-size: 14px;
  15. padding: 30px 0 18px 0;
  16. }
  17. .placeholder {
  18. background-color: #ebebef;
  19. color: #bbb;
  20. text-align: center;
  21. height: 30px;
  22. line-height: 30px;
  23. width: 100%;
  24. }

FlexFlex布局 - 图1

API

Flex

属性说明类型默认值
direction项目定位方向,值可以为 row,row-reverse,column,column-reverse, RN 仅支持row,columnStringrow
wrap子元素的换行方式,可选nowrap,wrap,wrap-reverse, RN 仅支持nowrap,wrapStringnowrap
justify子元素在主轴上的对齐方式,可选start,end,center,between,aroundStringstart
align子元素在交叉轴上的对齐方式,可选start,center,end,baseline,stretch RN仅支持start,end,center,stretchStringcenter
alignContent有多根轴线时的对齐方式,可选start,end,center,between,around,stretchStringstretch

Flex.Item

Flex.Item 组件默认加上了样式flex:1,保证所有 item 平均分宽度, Flex 容器的 children 不一定是 Flex.Item