Flex Flex布局

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

代码演示

基本示例

基础使用

  1. import { Flex, WhiteSpace } from 'antd-mobile';
  2. const PlaceHolder = props => (
  3. <div
  4. style={{
  5. backgroundColor: '#ebebef',
  6. color: '#bbb',
  7. textAlign: 'center',
  8. height: '0.6rem',
  9. lineHeight: '0.6rem',
  10. width: '100%',
  11. }}
  12. {...props}
  13. >Item</div>
  14. );
  15. const FlexExample = () => (
  16. <div className="flex-container">
  17. <div className="sub-title">基本</div>
  18. <Flex>
  19. <Flex.Item><PlaceHolder /></Flex.Item>
  20. <Flex.Item><PlaceHolder /></Flex.Item>
  21. </Flex>
  22. <WhiteSpace size="lg" />
  23. <Flex>
  24. <Flex.Item><PlaceHolder /></Flex.Item>
  25. <Flex.Item><PlaceHolder /></Flex.Item>
  26. <Flex.Item><PlaceHolder /></Flex.Item>
  27. </Flex>
  28. <WhiteSpace size="lg" />
  29. <Flex>
  30. <Flex.Item><PlaceHolder /></Flex.Item>
  31. <Flex.Item><PlaceHolder /></Flex.Item>
  32. <Flex.Item><PlaceHolder /></Flex.Item>
  33. <Flex.Item><PlaceHolder /></Flex.Item>
  34. </Flex>
  35. <WhiteSpace size="lg" />
  36. <div className="sub-title">wrap 换行</div>
  37. <Flex wrap="wrap">
  38. <PlaceHolder className="inline" />
  39. <PlaceHolder className="inline" />
  40. <PlaceHolder className="inline" />
  41. <PlaceHolder className="inline" />
  42. <PlaceHolder className="inline" />
  43. <PlaceHolder className="inline" />
  44. <PlaceHolder className="inline" />
  45. </Flex>
  46. <WhiteSpace size="lg" />
  47. <div className="sub-title">轴对齐方式</div>
  48. <Flex justify="center">
  49. <PlaceHolder className="inline" />
  50. <PlaceHolder className="inline" />
  51. <PlaceHolder className="inline" />
  52. </Flex>
  53. <WhiteSpace />
  54. <Flex justify="end">
  55. <PlaceHolder className="inline" />
  56. <PlaceHolder className="inline" />
  57. <PlaceHolder className="inline" />
  58. </Flex>
  59. <WhiteSpace />
  60. <Flex justify="between">
  61. <PlaceHolder className="inline" />
  62. <PlaceHolder className="inline" />
  63. <PlaceHolder className="inline" />
  64. </Flex>
  65. <WhiteSpace />
  66. <Flex align="start">
  67. <PlaceHolder className="inline" />
  68. <PlaceHolder className="inline small" />
  69. <PlaceHolder className="inline" />
  70. </Flex>
  71. <WhiteSpace />
  72. <Flex align="end">
  73. <PlaceHolder className="inline" />
  74. <PlaceHolder className="inline small" />
  75. <PlaceHolder className="inline" />
  76. </Flex>
  77. <WhiteSpace />
  78. <Flex align="baseline">
  79. <PlaceHolder className="inline" />
  80. <PlaceHolder className="inline small" />
  81. <PlaceHolder className="inline" />
  82. </Flex>
  83. </div>
  84. );
  85. ReactDOM.render(<FlexExample />, mountNode);
  1. .flex-container {
  2. margin: 0 0.30rem;
  3. }
  4. .flex-container .inline {
  5. width: 1.6rem!important;
  6. margin: 0.18rem 0.18rem 0.18rem 0;
  7. }
  8. .flex-container .small {
  9. height: 0.4rem!important;
  10. line-height: 0.4rem!important;
  11. }
  12. .sub-title {
  13. color: #888;
  14. font-size: .28rem;
  15. padding: 30px 0 18px 0;
  16. }
  17. ```

FlexFlex布局 - 图1

API

适用平台:WEB、React-Native

Flex

属性说明类型默认值
directionhow flex items are placed in the flex container,value could be 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(web only)有多根轴线时的对齐方式,可选start,end,center,between,around,stretchStringstretch

Flex.Item

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