Flex Flex布局

Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。 如果不熟悉推荐学习下这个 Flex 布局教程## 代码演示

示例集合

  1. import { Flex, WhiteSpace } from 'antd-mobile';
  2. const PlaceHolder = (props) => (
  3. <div style={{
  4. backgroundColor: '#ebebef',
  5. color: '#bbb',
  6. textAlign: 'center',
  7. height: '0.6rem',
  8. lineHeight: '0.6rem',
  9. width: '100%',
  10. }} {...props}
  11. >Item</div>
  12. );
  13. const FlexExample = React.createClass({
  14. render() {
  15. return (
  16. <div className="flex-container">
  17. <div className="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="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="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. },
  86. });
  87. ReactDOM.render(<FlexExample />, mountNode);
  1. .flex-container {
  2. padding: 0.18rem;
  3. }
  4. .flex-container .title {
  5. color: #888;
  6. margin: 0.20rem 0;
  7. }
  8. .flex-container .inline {
  9. width: 1.6rem!important;
  10. margin: 0.18rem 0.18rem 0.18rem 0;
  11. }
  12. .flex-container .small {
  13. height: 0.4rem!important;
  14. line-height: 0.4rem!important;
  15. }
  16. ```

FlexFlex布局 - 图1

API ( 适用平台:WEB、React-Native )

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/top,center/middle,end/bottom,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