1# Flex Layout 2 3> **NOTE** 4> - The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 5> 6> - 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. 7 8## flexBasis 9 10flexBasis(value: number | string) 11 12Sets the base size of the component. 13 14**Widget capability**: This API can be used in ArkTS widgets since API version 9. 15 16**Atomic service API**: This API can be used in atomic services since API version 11. 17 18**System capability**: SystemCapability.ArkUI.ArkUI.Full 19 20**Parameters** 21 22| Name| Type | Mandatory| Description | 23| ------ | -------------------------- | ---- | ------------------------------------------------------------ | 24| 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>For the string type, the value must be a string that can be converted into a number (for example, **'10'**), a string that includes a length unit (for example, **'10px'**), or the literal string **'auto'**; percentage-based strings are not supported.<br>For the number type, the value range is (0, +∞), and the unit is vp.<br>Invalid values are treated as the default value **'auto'**.| 25 26## flexGrow 27 28flexGrow(value: number) 29 30Sets the percentage of the parent container's remaining space that is allocated to the component. 31 32**Widget capability**: This API can be used in ArkTS widgets since API version 9. 33 34**Atomic service API**: This API can be used in atomic services since API version 11. 35 36**System capability**: SystemCapability.ArkUI.ArkUI.Full 37 38**Parameters** 39 40| Name| Type | Mandatory| Description | 41| ------ | ------ | ---- | ------------------------------------------------------------ | 42| value | number | Yes | Percentage of the parent container's remaining space that is allocated to the component.<br>Default value: **0**| 43 44## flexShrink 45 46flexShrink(value: number) 47 48Sets 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. 49 50**Widget capability**: This API can be used in ArkTS widgets since API version 9. 51 52**Atomic service API**: This API can be used in atomic services since API version 11. 53 54**System capability**: SystemCapability.ArkUI.ArkUI.Full 55 56**Parameters** 57 58| Name| Type | Mandatory| Description | 59| ------ | ------ | ---- | ------------------------------------------------------------ | 60| 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**.<br>When [constraintSize](ts-universal-attributes-size.md#constraintsize) is applied to the [Column](ts-container-column.md) and [Row](ts-container-row.md) components without the main axis size specified (through [width](ts-universal-attributes-size.md#width), [height](ts-universal-attributes-size.md#height), or [size](ts-universal-attributes-size.md#size)), these components use their default layout behavior, adapting to the size of their child component along the main axis. In this case, **flexShrink** does not take effect.| 61 62## alignSelf 63 64alignSelf(value: ItemAlign) 65 66Sets the alignment mode of the child components along the cross axis of the parent container. 67 68**Widget capability**: This API can be used in ArkTS widgets since API version 9. 69 70**Atomic service API**: This API can be used in atomic services since API version 11. 71 72**System capability**: SystemCapability.ArkUI.ArkUI.Full 73 74**Parameters** 75 76| Name| Type | Mandatory| Description | 77| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ | 78| 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**| 79 80 81## Example 82 83This example shows how to set up a flex layout through the **flexBasis**, **flexGrow**, **flexShrink**, and **alignSelf** attributes. 84 85```ts 86// xxx.ets 87@Entry 88@Component 89struct FlexExample { 90 build() { 91 Column({ space: 5 }) { 92 Text('flexBasis').fontSize(9).fontColor(0xCCCCCC).width('90%') 93 // Base size in the main axis 94 // The value of flexBasis() can be 'auto' or a number, which is equivalent to .width()/.height(). 95 Flex() { 96 Text('flexBasis(100)') 97 .flexBasis(100) // The width is 100 vp. 98 .height(100) 99 .backgroundColor(0xF5DEB3) 100 .textAlign(TextAlign.Center) 101 Text(`flexBasis('auto')`) 102 .flexBasis('auto') // The width is 60% of the original width. 103 .width('60%') 104 .height(100) 105 .backgroundColor(0xD2B48C) 106 .textAlign(TextAlign.Center) 107 }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE) 108 109 Text('flexGrow').fontSize(9).fontColor(0xCCCCCC).width('90%') 110 // flexGrow() indicates the percentage of the remaining space allocated to the component. 111 Flex() { 112 Text('flexGrow(2)') 113 .flexGrow(2) // The width allocated to the Text component is 2/3 of the remaining width of the parent container. 114 .height(100) 115 .backgroundColor(0xF5DEB3) 116 .textAlign(TextAlign.Center) 117 Text('flexGrow(1)') 118 .flexGrow(1) // The width allocated to the Text component is 1/3 of the remaining width of the parent container. 119 .height(100) 120 .backgroundColor(0xD2B48C) 121 .textAlign(TextAlign.Center) 122 }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE) 123 124 Text('flexShrink').fontSize(9).fontColor(0xCCCCCC).width('90%') 125 // flexShrink() indicates the percentage of the shrink size allocated to the component. 126 // 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. 127 Flex({ direction: FlexDirection.Row }) { 128 Text('flexShrink(0)') 129 .flexShrink(0) 130 .width('50%') 131 .height(100) 132 .backgroundColor(0xF5DEB3) 133 .textAlign(TextAlign.Center) 134 Text('default flexShrink') // The default value is 1. 135 .width('40%') 136 .height(100) 137 .backgroundColor(0xD2B48C) 138 .textAlign(TextAlign.Center) 139 Text('flexShrink(1)') 140 .flexShrink(1) 141 .width('40%') 142 .height(100) 143 .backgroundColor(0xF5DEB3) 144 .textAlign(TextAlign.Center) 145 }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE) 146 147 Text('alignSelf').fontSize(9).fontColor(0xCCCCCC).width('90%') 148 // The alignSelf setting overrides the alignItems setting of the parent container. 149 Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center }) { 150 Text('no alignSelf,height:70') 151 .width('33%') 152 .height(70) 153 .backgroundColor(0xF5DEB3) 154 .textAlign(TextAlign.Center) 155 Text('alignSelf End') 156 .alignSelf(ItemAlign.End) 157 .width('33%') 158 .height(70) 159 .backgroundColor(0xD2B48C) 160 .textAlign(TextAlign.Center) 161 Text('no alignSelf,height:100%') 162 .width('34%') 163 .height('100%') 164 .backgroundColor(0xF5DEB3) 165 .textAlign(TextAlign.Center) 166 }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE) 167 }.width('100%').margin({ top: 5 }) 168 } 169} 170``` 171 172 173