1e41f4b71Sopenharmony_ci# Flex Layout 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci> **NOTE** 4e41f4b71Sopenharmony_ci> - The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 5e41f4b71Sopenharmony_ci> 6e41f4b71Sopenharmony_ci> - The flex layout is valid only when the parent container is a [Flex](ts-container-flex.md), [Column](ts-container-column.md), [Row](ts-container-row.md), or [GridRow](ts-container-gridrow.md) (only for **alignSelf**) component. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci## flexBasis 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ciflexBasis(value: number | string) 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ciSets the base size of the component. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 9. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci**Parameters** 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 23e41f4b71Sopenharmony_ci| ------ | -------------------------- | ---- | ------------------------------------------------------------ | 24e41f4b71Sopenharmony_ci| value | number \| string | Yes | Base size of the component in the main axis of the parent container.<br>Default value: **'auto'** (indicating that the base size of the component in the main axis is the original size of the component)<br>This attribute cannot be set in percentage.| 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci## flexGrow 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ciflexGrow(value: number) 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ciSets the percentage of the parent container's remaining space that is allocated to the component. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 9. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**Parameters** 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 41e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 42e41f4b71Sopenharmony_ci| value | number | Yes | Percentage of the parent container's remaining space that is allocated to the component.<br>Default value: **0**| 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci## flexShrink 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ciflexShrink(value: number) 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ciSets the percentage of the parent container's shrink size that is allocated to the component. When the parent container is **Column** or **Row**, you must set the size along the main axis. 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 9. 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci**Parameters** 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 59e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 60e41f4b71Sopenharmony_ci| value | number | Yes | Percentage of the parent container's shrink size that is allocated to the component.<br>If the parent container is [Column](ts-container-column.md) or [Row](ts-container-row.md), the default value is **0**.<br> If the parent container is [Flex](ts-container-flex.md), the default value is **1**.| 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci## alignSelf 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_cialignSelf(value: ItemAlign) 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ciSets the alignment mode of the child components along the cross axis of the parent container. 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 9. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci**Parameters** 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 77e41f4b71Sopenharmony_ci| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ | 78e41f4b71Sopenharmony_ci| value | [ItemAlign](ts-appendix-enums.md#itemalign) | Yes | Alignment mode of the child components along the cross axis of the parent container. The setting overwrites the **alignItems** setting of the parent container ([Flex](ts-container-flex.md), [Column](ts-container-column.md), [Row](ts-container-row.md), or [GridRow](ts-container-gridrow.md)).<br>[GridCol](./ts-container-gridcol.md) can have the **alignsSelf** attribute bound to change its own layout along the cross axis.<br>Default value: **ItemAlign.Auto**| 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci## Example 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci```ts 84e41f4b71Sopenharmony_ci// xxx.ets 85e41f4b71Sopenharmony_ci@Entry 86e41f4b71Sopenharmony_ci@Component 87e41f4b71Sopenharmony_cistruct FlexExample { 88e41f4b71Sopenharmony_ci build() { 89e41f4b71Sopenharmony_ci Column({ space: 5 }) { 90e41f4b71Sopenharmony_ci Text('flexBasis').fontSize(9).fontColor(0xCCCCCC).width('90%') 91e41f4b71Sopenharmony_ci // Base size in the main axis 92e41f4b71Sopenharmony_ci // The value of flexBasis() can be 'auto' or a number, which is equivalent to .width()/.height(). 93e41f4b71Sopenharmony_ci Flex() { 94e41f4b71Sopenharmony_ci Text('flexBasis(100)') 95e41f4b71Sopenharmony_ci .flexBasis(100) // The width is 100 vp. 96e41f4b71Sopenharmony_ci .height(100) 97e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 98e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 99e41f4b71Sopenharmony_ci Text(`flexBasis('auto')`) 100e41f4b71Sopenharmony_ci .flexBasis('auto') // The width is 60% of the original width. 101e41f4b71Sopenharmony_ci .width('60%') 102e41f4b71Sopenharmony_ci .height(100) 103e41f4b71Sopenharmony_ci .backgroundColor(0xD2B48C) 104e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 105e41f4b71Sopenharmony_ci }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE) 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci Text('flexGrow').fontSize(9).fontColor(0xCCCCCC).width('90%') 108e41f4b71Sopenharmony_ci // flexGrow() indicates the percentage of the remaining space allocated to the component. 109e41f4b71Sopenharmony_ci Flex() { 110e41f4b71Sopenharmony_ci Text('flexGrow(2)') 111e41f4b71Sopenharmony_ci .flexGrow(2) // The width allocated to the Text component is 2/3 of the remaining width of the parent container. 112e41f4b71Sopenharmony_ci .height(100) 113e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 114e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 115e41f4b71Sopenharmony_ci Text('flexGrow(1)') 116e41f4b71Sopenharmony_ci .flexGrow(1) // The width allocated to the Text component is 1/3 of the remaining width of the parent container. 117e41f4b71Sopenharmony_ci .height(100) 118e41f4b71Sopenharmony_ci .backgroundColor(0xD2B48C) 119e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 120e41f4b71Sopenharmony_ci }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE) 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci Text('flexShrink').fontSize(9).fontColor(0xCCCCCC).width('90%') 123e41f4b71Sopenharmony_ci // flexShrink() indicates the percentage of the shrink size allocated to the component. 124e41f4b71Sopenharmony_ci // The value is 0 for the first Text component and 1 for the other two Text components. This means that, if the components cannot be completely displayed in the parent container, the latter two are shrunk proportionally, while the former is not shrunk. 125e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Row }) { 126e41f4b71Sopenharmony_ci Text('flexShrink(0)') 127e41f4b71Sopenharmony_ci .flexShrink(0) 128e41f4b71Sopenharmony_ci .width('50%') 129e41f4b71Sopenharmony_ci .height(100) 130e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 131e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 132e41f4b71Sopenharmony_ci Text('default flexShrink') // The default value is 1. 133e41f4b71Sopenharmony_ci .width('40%') 134e41f4b71Sopenharmony_ci .height(100) 135e41f4b71Sopenharmony_ci .backgroundColor(0xD2B48C) 136e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 137e41f4b71Sopenharmony_ci Text('flexShrink(1)') 138e41f4b71Sopenharmony_ci .flexShrink(1) 139e41f4b71Sopenharmony_ci .width('40%') 140e41f4b71Sopenharmony_ci .height(100) 141e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 142e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 143e41f4b71Sopenharmony_ci }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE) 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci Text('alignSelf').fontSize(9).fontColor(0xCCCCCC).width('90%') 146e41f4b71Sopenharmony_ci // The alignSelf setting overrides the alignItems setting of the parent container. 147e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center }) { 148e41f4b71Sopenharmony_ci Text('no alignSelf,height:70') 149e41f4b71Sopenharmony_ci .width('33%') 150e41f4b71Sopenharmony_ci .height(70) 151e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 152e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 153e41f4b71Sopenharmony_ci Text('alignSelf End') 154e41f4b71Sopenharmony_ci .alignSelf(ItemAlign.End) 155e41f4b71Sopenharmony_ci .width('33%') 156e41f4b71Sopenharmony_ci .height(70) 157e41f4b71Sopenharmony_ci .backgroundColor(0xD2B48C) 158e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 159e41f4b71Sopenharmony_ci Text('no alignSelf,height:100%') 160e41f4b71Sopenharmony_ci .width('34%') 161e41f4b71Sopenharmony_ci .height('100%') 162e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 163e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 164e41f4b71Sopenharmony_ci }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE) 165e41f4b71Sopenharmony_ci }.width('100%').margin({ top: 5 }) 166e41f4b71Sopenharmony_ci } 167e41f4b71Sopenharmony_ci} 168e41f4b71Sopenharmony_ci``` 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci 171