1e41f4b71Sopenharmony_ci# Flex Layout (Flex) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Overview 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciThe flex layout, implemented using the [\<Flex>](../reference/apis-arkui/arkui-ts/ts-container-flex.md) container component, provides simple and powerful tools for flexibly distributing space and aligning items. The flex layout is widely used in scenarios such as the navigation bar distribution on the page header, page framework setup, and arrangement of multiple lines of data. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ciBy default, the flex container has a main axis and a cross axis, and child elements are arranged along the main axis. The size of a child element along the main axis is referred to as its main axis size. Similarly, the size of a child element along the cross axis is referred to as its cross axis size. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci **Figure 1** Flex container whose main axis runs in the horizontal direction 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## Basic Concepts 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci- Main axis: axis along which child elements are placed in the **\<Flex>** component. Child elements are laid out along the main axis by default. The start point of the main axis is referred to as main-start, and the end point is referred to as main-end. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci- Cross axis: axis that runs perpendicular to the main axis. The start point of the cross axis is referred to as cross-start, and the end point is referred to as cross-end. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci## Layout Direction 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ciIn the flex layout, the child elements can be arranged in any direction. You can set the **direction** parameter to define the direction of the main axis and thereby control the arrangement of child elements. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci **Figure 2** Flex layout direction 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci- **FlexDirection.Row** (default value): The main axis runs along the row horizontally, and the child elements are laid out from the start edge of the main axis. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci ```ts 36e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Row }) { 37e41f4b71Sopenharmony_ci Text('1').width('33%').height(50).backgroundColor(0xF5DEB3) 38e41f4b71Sopenharmony_ci Text('2').width('33%').height(50).backgroundColor(0xD2B48C) 39e41f4b71Sopenharmony_ci Text('3').width('33%').height(50).backgroundColor(0xF5DEB3) 40e41f4b71Sopenharmony_ci } 41e41f4b71Sopenharmony_ci .height(70) 42e41f4b71Sopenharmony_ci .width('90%') 43e41f4b71Sopenharmony_ci .padding(10) 44e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 45e41f4b71Sopenharmony_ci ``` 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci  48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci- **FlexDirection.RowReverse**: The main axis runs along the row horizontally, and the child elements are laid out from the end edge of the main axis, in a direction opposite to **FlexDirection.Row**. 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci ```ts 53e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.RowReverse }) { 54e41f4b71Sopenharmony_ci Text('1').width('33%').height(50).backgroundColor(0xF5DEB3) 55e41f4b71Sopenharmony_ci Text('2').width('33%').height(50).backgroundColor(0xD2B48C) 56e41f4b71Sopenharmony_ci Text('3').width('33%').height(50).backgroundColor(0xF5DEB3) 57e41f4b71Sopenharmony_ci } 58e41f4b71Sopenharmony_ci .height(70) 59e41f4b71Sopenharmony_ci .width('90%') 60e41f4b71Sopenharmony_ci .padding(10) 61e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 62e41f4b71Sopenharmony_ci ``` 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci  65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci- **FlexDirection.Column**: The main axis runs along the column vertically, and the child elements are laid out from the start edge of the main axis. 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci ```ts 70e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column }) { 71e41f4b71Sopenharmony_ci Text('1').width('100%').height(50).backgroundColor(0xF5DEB3) 72e41f4b71Sopenharmony_ci Text('2').width('100%').height(50).backgroundColor(0xD2B48C) 73e41f4b71Sopenharmony_ci Text('3').width('100%').height(50).backgroundColor(0xF5DEB3) 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci .height(70) 76e41f4b71Sopenharmony_ci .width('90%') 77e41f4b71Sopenharmony_ci .padding(10) 78e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 79e41f4b71Sopenharmony_ci ``` 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci  82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci- **FlexDirection.ColumnReverse**: The main axis runs along the column vertically, and the child elements are laid out from the end edge of the main axis, in a direction opposite to **FlexDirection.Column**. 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci ```ts 87e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.ColumnReverse }) { 88e41f4b71Sopenharmony_ci Text('1').width('100%').height(50).backgroundColor(0xF5DEB3) 89e41f4b71Sopenharmony_ci Text('2').width('100%').height(50).backgroundColor(0xD2B48C) 90e41f4b71Sopenharmony_ci Text('3').width('100%').height(50).backgroundColor(0xF5DEB3) 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci .height(70) 93e41f4b71Sopenharmony_ci .width('90%') 94e41f4b71Sopenharmony_ci .padding(10) 95e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 96e41f4b71Sopenharmony_ci ``` 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci  99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci## Wrapping in the Flex Layout 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ciIn the flex layout, child elements can be laid on a single line or on multiple lines. By default, child elements in the flex container are laid out on a single line (also called an axis). You can use the **wrap** attribute to set whether child elements can wrap onto multiple lines when the total main axis size of the child elements is greater than the main axis size of the container. When wrapped onto multiple lines, the child elements on the new line are aligned based on the cross axis direction. 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci- **FlexWrap.NoWrap** (default value): Child elements are laid out on a single line. This may cause the child elements to shrink to fit the container when their total width is greater than the container width. 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci ```ts 109e41f4b71Sopenharmony_ci Flex({ wrap: FlexWrap.NoWrap }) { 110e41f4b71Sopenharmony_ci Text('1').width('50%').height(50).backgroundColor(0xF5DEB3) 111e41f4b71Sopenharmony_ci Text('2').width('50%').height(50).backgroundColor(0xD2B48C) 112e41f4b71Sopenharmony_ci Text('3').width('50%').height(50).backgroundColor(0xF5DEB3) 113e41f4b71Sopenharmony_ci } 114e41f4b71Sopenharmony_ci .width('90%') 115e41f4b71Sopenharmony_ci .padding(10) 116e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 117e41f4b71Sopenharmony_ci ``` 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci  120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci- **FlexWrap.Wrap**: Child elements break into multiple lines and are aligned along the main axis. 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci ```ts 125e41f4b71Sopenharmony_ci Flex({ wrap: FlexWrap.Wrap }) { 126e41f4b71Sopenharmony_ci Text('1').width('50%').height(50).backgroundColor(0xF5DEB3) 127e41f4b71Sopenharmony_ci Text('2').width('50%').height(50).backgroundColor(0xD2B48C) 128e41f4b71Sopenharmony_ci Text('3').width('50%').height(50).backgroundColor(0xD2B48C) 129e41f4b71Sopenharmony_ci } 130e41f4b71Sopenharmony_ci .width('90%') 131e41f4b71Sopenharmony_ci .padding(10) 132e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 133e41f4b71Sopenharmony_ci ``` 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci  136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci- **FlexWrap.WrapReverse**: Child elements break into multiple lines and are aligned in the reverse direction to the main axis. 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci ```ts 141e41f4b71Sopenharmony_ci Flex({ wrap: FlexWrap.WrapReverse}) { 142e41f4b71Sopenharmony_ci Text('1').width('50%').height(50).backgroundColor(0xF5DEB3) 143e41f4b71Sopenharmony_ci Text('2').width('50%').height(50).backgroundColor(0xD2B48C) 144e41f4b71Sopenharmony_ci Text('3').width('50%').height(50).backgroundColor(0xF5DEB3) 145e41f4b71Sopenharmony_ci } 146e41f4b71Sopenharmony_ci .width('90%') 147e41f4b71Sopenharmony_ci .padding(10) 148e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 149e41f4b71Sopenharmony_ci ``` 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci  152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci## Alignment on the Main Axis 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ciUse the **justifyContent** parameter to set alignment of child elements on the main axis. 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci- **FlexAlign.Start** (default value): The child elements are aligned with each other toward the start edge of the container along the main axis. 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci ```ts 165e41f4b71Sopenharmony_ci let PTopBottom:Record<string,number> = { 'top': 10, 'bottom': 10 } 166e41f4b71Sopenharmony_ci Flex({ justifyContent: FlexAlign.Start }) { 167e41f4b71Sopenharmony_ci Text('1').width('20%').height(50).backgroundColor(0xF5DEB3) 168e41f4b71Sopenharmony_ci Text('2').width('20%').height(50).backgroundColor(0xD2B48C) 169e41f4b71Sopenharmony_ci Text('3').width('20%').height(50).backgroundColor(0xF5DEB3) 170e41f4b71Sopenharmony_ci } 171e41f4b71Sopenharmony_ci .width('90%') 172e41f4b71Sopenharmony_ci .padding(PTopBottom) 173e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 174e41f4b71Sopenharmony_ci ``` 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci  177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci- **FlexAlign.Center**: The child elements are aligned with each other toward the center of the container along the main axis. 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci ```ts 182e41f4b71Sopenharmony_ci let PTopBottom:Record<string,number> = { 'top': 10, 'bottom': 10 } 183e41f4b71Sopenharmony_ci Flex({ justifyContent: FlexAlign.Center }) { 184e41f4b71Sopenharmony_ci Text('1').width('20%').height(50).backgroundColor(0xF5DEB3) 185e41f4b71Sopenharmony_ci Text('2').width('20%').height(50).backgroundColor(0xD2B48C) 186e41f4b71Sopenharmony_ci Text('3').width('20%').height(50).backgroundColor(0xF5DEB3) 187e41f4b71Sopenharmony_ci } 188e41f4b71Sopenharmony_ci .width('90%') 189e41f4b71Sopenharmony_ci .padding(PTopBottom) 190e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 191e41f4b71Sopenharmony_ci ``` 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci  194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci- **FlexAlign.End**: The child elements are aligned with each other toward the end edge of the container along the main axis. 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci ```ts 199e41f4b71Sopenharmony_ci let PTopBottom:Record<string,number> = { 'top': 10, 'bottom': 10 } 200e41f4b71Sopenharmony_ci Flex({ justifyContent: FlexAlign.End }) { 201e41f4b71Sopenharmony_ci Text('1').width('20%').height(50).backgroundColor(0xF5DEB3) 202e41f4b71Sopenharmony_ci Text('2').width('20%').height(50).backgroundColor(0xD2B48C) 203e41f4b71Sopenharmony_ci Text('3').width('20%').height(50).backgroundColor(0xF5DEB3) 204e41f4b71Sopenharmony_ci } 205e41f4b71Sopenharmony_ci .width('90%') 206e41f4b71Sopenharmony_ci .padding(PTopBottom) 207e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 208e41f4b71Sopenharmony_ci ``` 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci  211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci- **FlexAlign.SpaceBetween**: The child elements are evenly distributed within the container along the main axis. The first and last child elements are aligned with the edges of the container. 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci ```ts 216e41f4b71Sopenharmony_ci let PTopBottom1:Record<string,number> = { 'top': 10, 'bottom': 10 } 217e41f4b71Sopenharmony_ci Flex({ justifyContent: FlexAlign.SpaceBetween }) { 218e41f4b71Sopenharmony_ci Text('1').width('20%').height(50).backgroundColor(0xF5DEB3) 219e41f4b71Sopenharmony_ci Text('2').width('20%').height(50).backgroundColor(0xD2B48C) 220e41f4b71Sopenharmony_ci Text('3').width('20%').height(50).backgroundColor(0xF5DEB3) 221e41f4b71Sopenharmony_ci } 222e41f4b71Sopenharmony_ci .width('90%') 223e41f4b71Sopenharmony_ci .padding(PTopBottom1) 224e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 225e41f4b71Sopenharmony_ci ``` 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci  228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci- **FlexAlign.SpaceAround**: The child elements are evenly distributed within the container along the main axis. The space between the first child element and main-start, and that between the last child element and main-end are both half of the space between two adjacent child elements. 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci ```ts 233e41f4b71Sopenharmony_ci let PTopBottom:Record<string,number> = { 'top': 10, 'bottom': 10 } 234e41f4b71Sopenharmony_ci Flex({ justifyContent: FlexAlign.SpaceAround }) { 235e41f4b71Sopenharmony_ci Text('1').width('20%').height(50).backgroundColor(0xF5DEB3) 236e41f4b71Sopenharmony_ci Text('2').width('20%').height(50).backgroundColor(0xD2B48C) 237e41f4b71Sopenharmony_ci Text('3').width('20%').height(50).backgroundColor(0xF5DEB3) 238e41f4b71Sopenharmony_ci } 239e41f4b71Sopenharmony_ci .width('90%') 240e41f4b71Sopenharmony_ci .padding(PTopBottom) 241e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 242e41f4b71Sopenharmony_ci ``` 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci  245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci- **FlexAlign.SpaceEvenly**: The child elements are evenly distributed within the container along the main axis. The space between the first child element and main-start, the space between the last child element and main-end, and the space between two adjacent child elements are the same. 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci ```ts 250e41f4b71Sopenharmony_ci let PTopBottom:Record<string,number> = { 'top': 10, 'bottom': 10 } 251e41f4b71Sopenharmony_ci Flex({ justifyContent: FlexAlign.SpaceEvenly }) { 252e41f4b71Sopenharmony_ci Text('1').width('20%').height(50).backgroundColor(0xF5DEB3) 253e41f4b71Sopenharmony_ci Text('2').width('20%').height(50).backgroundColor(0xD2B48C) 254e41f4b71Sopenharmony_ci Text('3').width('20%').height(50).backgroundColor(0xF5DEB3) 255e41f4b71Sopenharmony_ci } 256e41f4b71Sopenharmony_ci .width('90%') 257e41f4b71Sopenharmony_ci .padding(PTopBottom) 258e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 259e41f4b71Sopenharmony_ci ``` 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci  262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci## Alignment on the Cross Axis 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ciAlignment on the cross axis can be set for both the container and child elements, with that set for child elements having a higher priority. 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci### Setting Alignment on the Cross Axis for the Container 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ciUse the **alignItems** parameter of the **\<Flex>** component to set alignment of child elements on the cross axis. 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci- **ItemAlign.Auto**: The child elements are automatically aligned in the flex container. 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci ```ts 278e41f4b71Sopenharmony_ci let SWh:Record<string,number|string> = { 'width': '90%', 'height': 80 } 279e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Auto }) { 280e41f4b71Sopenharmony_ci Text('1').width('33%').height(30).backgroundColor(0xF5DEB3) 281e41f4b71Sopenharmony_ci Text('2').width('33%').height(40).backgroundColor(0xD2B48C) 282e41f4b71Sopenharmony_ci Text('3').width('33%').height(50).backgroundColor(0xF5DEB3) 283e41f4b71Sopenharmony_ci } 284e41f4b71Sopenharmony_ci .size(SWh) 285e41f4b71Sopenharmony_ci .padding(10) 286e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 287e41f4b71Sopenharmony_ci ``` 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci  290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci- **ItemAlign.Start**: The child elements are aligned with the start edge of the container along the cross axis. 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci ```ts 295e41f4b71Sopenharmony_ci let SWh:Record<string,number|string> = { 'width': '90%', 'height': 80 } 296e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Start }) { 297e41f4b71Sopenharmony_ci Text('1').width('33%').height(30).backgroundColor(0xF5DEB3) 298e41f4b71Sopenharmony_ci Text('2').width('33%').height(40).backgroundColor(0xD2B48C) 299e41f4b71Sopenharmony_ci Text('3').width('33%').height(50).backgroundColor(0xF5DEB3) 300e41f4b71Sopenharmony_ci } 301e41f4b71Sopenharmony_ci .size(SWh) 302e41f4b71Sopenharmony_ci .padding(10) 303e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 304e41f4b71Sopenharmony_ci ``` 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci  307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_ci- **ItemAlign.Start**: The child elements are aligned with the center of the container along the cross axis. 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci ```ts 312e41f4b71Sopenharmony_ci let SWh:Record<string,number|string> = { 'width': '90%', 'height': 80 } 313e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Center }) { 314e41f4b71Sopenharmony_ci Text('1').width('33%').height(30).backgroundColor(0xF5DEB3) 315e41f4b71Sopenharmony_ci Text('2').width('33%').height(40).backgroundColor(0xD2B48C) 316e41f4b71Sopenharmony_ci Text('3').width('33%').height(50).backgroundColor(0xF5DEB3) 317e41f4b71Sopenharmony_ci } 318e41f4b71Sopenharmony_ci .size(SWh) 319e41f4b71Sopenharmony_ci .padding(10) 320e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 321e41f4b71Sopenharmony_ci ``` 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ci  324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci- **ItemAlign.End**: The child elements are aligned with the end edge of the container along the cross axis. 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci ```ts 329e41f4b71Sopenharmony_ci let SWh:Record<string,number|string> = { 'width': '90%', 'height': 80 } 330e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.End }) { 331e41f4b71Sopenharmony_ci Text('1').width('33%').height(30).backgroundColor(0xF5DEB3) 332e41f4b71Sopenharmony_ci Text('2').width('33%').height(40).backgroundColor(0xD2B48C) 333e41f4b71Sopenharmony_ci Text('3').width('33%').height(50).backgroundColor(0xF5DEB3) 334e41f4b71Sopenharmony_ci } 335e41f4b71Sopenharmony_ci .size(SWh) 336e41f4b71Sopenharmony_ci .padding(10) 337e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 338e41f4b71Sopenharmony_ci ``` 339e41f4b71Sopenharmony_ci 340e41f4b71Sopenharmony_ci  341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci- **ItemAlign.Stretch**: The child elements are stretched along the cross axis. If no constraints are set, the child elements are stretched to fill the size of the container on the cross axis. 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci ```ts 346e41f4b71Sopenharmony_ci let SWh:Record<string,number|string> = { 'width': '90%', 'height': 80 } 347e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Stretch }) { 348e41f4b71Sopenharmony_ci Text('1').width('33%').backgroundColor(0xF5DEB3) 349e41f4b71Sopenharmony_ci Text('2').width('33%').backgroundColor(0xD2B48C) 350e41f4b71Sopenharmony_ci Text('3').width('33%').backgroundColor(0xF5DEB3) 351e41f4b71Sopenharmony_ci } 352e41f4b71Sopenharmony_ci .size(SWh) 353e41f4b71Sopenharmony_ci .padding(10) 354e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 355e41f4b71Sopenharmony_ci ``` 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci  358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci- **ItemAlign.Baseline**: The child elements are aligned at the baseline of the cross axis. 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci 362e41f4b71Sopenharmony_ci ```ts 363e41f4b71Sopenharmony_ci let SWh:Record<string,number|string> = { 'width': '90%', 'height': 80 } 364e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Baseline }) { 365e41f4b71Sopenharmony_ci Text('1').width('33%').height(30).backgroundColor(0xF5DEB3) 366e41f4b71Sopenharmony_ci Text('2').width('33%').height(40).backgroundColor(0xD2B48C) 367e41f4b71Sopenharmony_ci Text('3').width('33%').height(50).backgroundColor(0xF5DEB3) 368e41f4b71Sopenharmony_ci } 369e41f4b71Sopenharmony_ci .size(SWh) 370e41f4b71Sopenharmony_ci .padding(10) 371e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 372e41f4b71Sopenharmony_ci ``` 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci  375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ci### Setting Alignment on the Cross Axis for Child Elements 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ciUse the [alignSelf](../reference/apis-arkui/arkui-ts/ts-universal-attributes-flex-layout.md#alignself) attribute of child elements to set their alignment in the container on the cross axis. The settings overwrite the default **alignItems** settings in the flex container. The sample code is as follows: 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ci```ts 382e41f4b71Sopenharmony_ciFlex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center }) { // The child elements are aligned with the center of the container. 383e41f4b71Sopenharmony_ci Text('alignSelf Start').width('25%').height(80) 384e41f4b71Sopenharmony_ci .alignSelf(ItemAlign.Start) 385e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 386e41f4b71Sopenharmony_ci Text('alignSelf Baseline') 387e41f4b71Sopenharmony_ci .alignSelf(ItemAlign.Baseline) 388e41f4b71Sopenharmony_ci .width('25%') 389e41f4b71Sopenharmony_ci .height(80) 390e41f4b71Sopenharmony_ci .backgroundColor(0xD2B48C) 391e41f4b71Sopenharmony_ci Text('alignSelf Baseline').width('25%').height(100) 392e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 393e41f4b71Sopenharmony_ci .alignSelf(ItemAlign.Baseline) 394e41f4b71Sopenharmony_ci Text('no alignSelf').width('25%').height(100) 395e41f4b71Sopenharmony_ci .backgroundColor(0xD2B48C) 396e41f4b71Sopenharmony_ci Text('no alignSelf').width('25%').height(100) 397e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci}.width('90%').height(220).backgroundColor(0xAFEEEE) 400e41f4b71Sopenharmony_ci``` 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ciIn the preceding example, the **alignItems** parameter of the **\<Flex>** container and the **alignSelf** attribute of the child element are both set. In this case, the **alignSelf** settings take effect. 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci 409e41f4b71Sopenharmony_ci### Content Alignment 410e41f4b71Sopenharmony_ci 411e41f4b71Sopenharmony_ciUse the [alignContent](../reference/apis-arkui/arkui-ts/ts-container-flex.md#apis) parameter to set how space is distributed between and around child elements along the cross axis. This parameter is effective only for a multi-line flex layout. Its available options are as follows: 412e41f4b71Sopenharmony_ci 413e41f4b71Sopenharmony_ci- **FlexAlign.Start**: The child elements are aligned toward the start edge of the cross axis in the container. 414e41f4b71Sopenharmony_ci 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ci ```ts 417e41f4b71Sopenharmony_ci Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.Start }) { 418e41f4b71Sopenharmony_ci Text('1').width('30%').height(20).backgroundColor(0xF5DEB3) 419e41f4b71Sopenharmony_ci Text('2').width('60%').height(20).backgroundColor(0xD2B48C) 420e41f4b71Sopenharmony_ci Text('3').width('40%').height(20).backgroundColor(0xD2B48C) 421e41f4b71Sopenharmony_ci Text('4').width('30%').height(20).backgroundColor(0xF5DEB3) 422e41f4b71Sopenharmony_ci Text('5').width('20%').height(20).backgroundColor(0xD2B48C) 423e41f4b71Sopenharmony_ci } 424e41f4b71Sopenharmony_ci .width('90%') 425e41f4b71Sopenharmony_ci .height(100) 426e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 427e41f4b71Sopenharmony_ci ``` 428e41f4b71Sopenharmony_ci 429e41f4b71Sopenharmony_ci  430e41f4b71Sopenharmony_ci 431e41f4b71Sopenharmony_ci- **FlexAlign.Center**: The child elements are aligned toward the center of the cross axis in the container. 432e41f4b71Sopenharmony_ci 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci ```ts 435e41f4b71Sopenharmony_ci Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.Center }) { 436e41f4b71Sopenharmony_ci Text('1').width('30%').height(20).backgroundColor(0xF5DEB3) 437e41f4b71Sopenharmony_ci Text('2').width('60%').height(20).backgroundColor(0xD2B48C) 438e41f4b71Sopenharmony_ci Text('3').width('40%').height(20).backgroundColor(0xD2B48C) 439e41f4b71Sopenharmony_ci Text('4').width('30%').height(20).backgroundColor(0xF5DEB3) 440e41f4b71Sopenharmony_ci Text('5').width('20%').height(20).backgroundColor(0xD2B48C) 441e41f4b71Sopenharmony_ci } 442e41f4b71Sopenharmony_ci .width('90%') 443e41f4b71Sopenharmony_ci .height(100) 444e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 445e41f4b71Sopenharmony_ci ``` 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci  448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_ci- **FlexAlign.End**: The child elements are aligned toward the end edge of the cross axis in the container. 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ci ```ts 453e41f4b71Sopenharmony_ci Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.End }) { 454e41f4b71Sopenharmony_ci Text('1').width('30%').height(20).backgroundColor(0xF5DEB3) 455e41f4b71Sopenharmony_ci Text('2').width('60%').height(20).backgroundColor(0xD2B48C) 456e41f4b71Sopenharmony_ci Text('3').width('40%').height(20).backgroundColor(0xD2B48C) 457e41f4b71Sopenharmony_ci Text('4').width('30%').height(20).backgroundColor(0xF5DEB3) 458e41f4b71Sopenharmony_ci Text('5').width('20%').height(20).backgroundColor(0xD2B48C) 459e41f4b71Sopenharmony_ci } 460e41f4b71Sopenharmony_ci .width('90%') 461e41f4b71Sopenharmony_ci .height(100) 462e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 463e41f4b71Sopenharmony_ci ``` 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci  466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci- **FlexAlign.SpaceBetween**: The child elements are evenly distributed within the container along the cross axis. The first and last child elements are aligned with the edges of the container. 468e41f4b71Sopenharmony_ci 469e41f4b71Sopenharmony_ci 470e41f4b71Sopenharmony_ci ```ts 471e41f4b71Sopenharmony_ci Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.SpaceBetween }) { 472e41f4b71Sopenharmony_ci Text('1').width('30%').height(20).backgroundColor(0xF5DEB3) 473e41f4b71Sopenharmony_ci Text('2').width('60%').height(20).backgroundColor(0xD2B48C) 474e41f4b71Sopenharmony_ci Text('3').width('40%').height(20).backgroundColor(0xD2B48C) 475e41f4b71Sopenharmony_ci Text('4').width('30%').height(20).backgroundColor(0xF5DEB3) 476e41f4b71Sopenharmony_ci Text('5').width('20%').height(20).backgroundColor(0xD2B48C) 477e41f4b71Sopenharmony_ci } 478e41f4b71Sopenharmony_ci .width('90%') 479e41f4b71Sopenharmony_ci .height(100) 480e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 481e41f4b71Sopenharmony_ci ``` 482e41f4b71Sopenharmony_ci 483e41f4b71Sopenharmony_ci  484e41f4b71Sopenharmony_ci 485e41f4b71Sopenharmony_ci- **FlexAlign.SpaceAround**: The child elements are evenly distributed within the container along the cross axis. The space between the first child element and cross-start, and that between the last child element and cross-end are both half of the space between two adjacent child elements. 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ci 488e41f4b71Sopenharmony_ci ```ts 489e41f4b71Sopenharmony_ci Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.SpaceAround }) { 490e41f4b71Sopenharmony_ci Text('1').width('30%').height(20).backgroundColor(0xF5DEB3) 491e41f4b71Sopenharmony_ci Text('2').width('60%').height(20).backgroundColor(0xD2B48C) 492e41f4b71Sopenharmony_ci Text('3').width('40%').height(20).backgroundColor(0xD2B48C) 493e41f4b71Sopenharmony_ci Text('4').width('30%').height(20).backgroundColor(0xF5DEB3) 494e41f4b71Sopenharmony_ci Text('5').width('20%').height(20).backgroundColor(0xD2B48C) 495e41f4b71Sopenharmony_ci } 496e41f4b71Sopenharmony_ci .width('90%') 497e41f4b71Sopenharmony_ci .height(100) 498e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 499e41f4b71Sopenharmony_ci ``` 500e41f4b71Sopenharmony_ci 501e41f4b71Sopenharmony_ci  502e41f4b71Sopenharmony_ci 503e41f4b71Sopenharmony_ci- **FlexAlign.SpaceEvenly**: The child elements are evenly distributed within the container along the cross axis. The space between the first child element and cross-start, the space between the last child element and cross-end, and the space between two adjacent child elements are the same. 504e41f4b71Sopenharmony_ci 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ci ```ts 507e41f4b71Sopenharmony_ci Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.SpaceEvenly }) { 508e41f4b71Sopenharmony_ci Text('1').width('30%').height(20).backgroundColor(0xF5DEB3) 509e41f4b71Sopenharmony_ci Text('2').width('60%').height(20).backgroundColor(0xD2B48C) 510e41f4b71Sopenharmony_ci Text('3').width('40%').height(20).backgroundColor(0xD2B48C) 511e41f4b71Sopenharmony_ci Text('4').width('30%').height(20).backgroundColor(0xF5DEB3) 512e41f4b71Sopenharmony_ci Text('5').width('20%').height(20).backgroundColor(0xD2B48C) 513e41f4b71Sopenharmony_ci } 514e41f4b71Sopenharmony_ci .width('90%') 515e41f4b71Sopenharmony_ci .height(100) 516e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 517e41f4b71Sopenharmony_ci ``` 518e41f4b71Sopenharmony_ci 519e41f4b71Sopenharmony_ci  520e41f4b71Sopenharmony_ci 521e41f4b71Sopenharmony_ci 522e41f4b71Sopenharmony_ci## Adaptive Scaling 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_ciWhen the size of the flex container is not large enough, the following attributes of the child element can be used to achieve adaptive layout: 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ci- **flexBasis**: base size of the child element in the container along the main axis. It sets the space occupied by the child element. If this attribute is not set, the space occupied by the child element is the result of width/height. 527e41f4b71Sopenharmony_ci 528e41f4b71Sopenharmony_ci 529e41f4b71Sopenharmony_ci ```ts 530e41f4b71Sopenharmony_ci Flex() { 531e41f4b71Sopenharmony_ci Text('flexBasis("auto")') 532e41f4b71Sopenharmony_ci .flexBasis('auto') // When width is not set and flexBasis is set to auto, the content is at its own width. 533e41f4b71Sopenharmony_ci .height(100) 534e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 535e41f4b71Sopenharmony_ci Text('flexBasis("auto")'+' width("40%")') 536e41f4b71Sopenharmony_ci .width('40%') 537e41f4b71Sopenharmony_ci .flexBasis('auto') // When width is set and flexBasis is set to auto, the value of width is used. 538e41f4b71Sopenharmony_ci .height(100) 539e41f4b71Sopenharmony_ci .backgroundColor(0xD2B48C) 540e41f4b71Sopenharmony_ci 541e41f4b71Sopenharmony_ci Text('flexBasis(100)') // When width is not set and flexBasis is set to 100, the width is 100 vp. 542e41f4b71Sopenharmony_ci .flexBasis(100) 543e41f4b71Sopenharmony_ci .height(100) 544e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 545e41f4b71Sopenharmony_ci 546e41f4b71Sopenharmony_ci Text('flexBasis(100)') 547e41f4b71Sopenharmony_ci .flexBasis(100) 548e41f4b71Sopenharmony_ci .width(200) // When both width and flexBasis are set, flexBasis takes precedence, and the width is 100 vp. 549e41f4b71Sopenharmony_ci .height(100) 550e41f4b71Sopenharmony_ci .backgroundColor(0xD2B48C) 551e41f4b71Sopenharmony_ci }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE) 552e41f4b71Sopenharmony_ci ``` 553e41f4b71Sopenharmony_ci 554e41f4b71Sopenharmony_ci  555e41f4b71Sopenharmony_ci 556e41f4b71Sopenharmony_ci- **flexGrow**: percentage of the flex container's remaining space that is allocated to the child element. In other words, it is the grow factor of the child element. 557e41f4b71Sopenharmony_ci 558e41f4b71Sopenharmony_ci ```ts 559e41f4b71Sopenharmony_ci Flex() { 560e41f4b71Sopenharmony_ci Text('flexGrow(2)') 561e41f4b71Sopenharmony_ci .flexGrow(2) 562e41f4b71Sopenharmony_ci .width(100) 563e41f4b71Sopenharmony_ci .height(100) 564e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 565e41f4b71Sopenharmony_ci Text('flexGrow(3)') 566e41f4b71Sopenharmony_ci .flexGrow(3) 567e41f4b71Sopenharmony_ci .width(100) 568e41f4b71Sopenharmony_ci .height(100) 569e41f4b71Sopenharmony_ci .backgroundColor(0xD2B48C) 570e41f4b71Sopenharmony_ci 571e41f4b71Sopenharmony_ci Text('no flexGrow') 572e41f4b71Sopenharmony_ci .width(100) 573e41f4b71Sopenharmony_ci .height(100) 574e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 575e41f4b71Sopenharmony_ci }.width(420).height(120).padding(10).backgroundColor(0xAFEEEE) 576e41f4b71Sopenharmony_ci ``` 577e41f4b71Sopenharmony_ci 578e41f4b71Sopenharmony_ci  579e41f4b71Sopenharmony_ci 580e41f4b71Sopenharmony_ci In the preceding figure, the width of the flex container is 420 vp, the original width of the three child elements is 100 vp each, and the left and right margins are 20 vp in total. The flex container's remaining 100 vp space is allocated to the child elements based on their **flexGrow** settings. The third child element does not have **flexGrow** set and therefore does not participate in the allocation of remaining space. 581e41f4b71Sopenharmony_ci 582e41f4b71Sopenharmony_ci After receiving their share of remaining space at the 2:3 ratio, the first and second child elements are at a width of 140 vp (100 vp + 100 vp x 2/5) and 160 vp (100 vp + 100 vp x 3/5), respectively. 583e41f4b71Sopenharmony_ci 584e41f4b71Sopenharmony_ci- **flexShrink**: shrink factor of the child element when the size of all child elements is larger than the flex container. 585e41f4b71Sopenharmony_ci 586e41f4b71Sopenharmony_ci 587e41f4b71Sopenharmony_ci ```ts 588e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Row }) { 589e41f4b71Sopenharmony_ci Text('flexShrink(3)') 590e41f4b71Sopenharmony_ci .flexShrink(3) 591e41f4b71Sopenharmony_ci .width(200) 592e41f4b71Sopenharmony_ci .height(100) 593e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 594e41f4b71Sopenharmony_ci 595e41f4b71Sopenharmony_ci Text('no flexShrink') 596e41f4b71Sopenharmony_ci .width(200) 597e41f4b71Sopenharmony_ci .height(100) 598e41f4b71Sopenharmony_ci .backgroundColor(0xD2B48C) 599e41f4b71Sopenharmony_ci 600e41f4b71Sopenharmony_ci Text('flexShrink(2)') 601e41f4b71Sopenharmony_ci .flexShrink(2) 602e41f4b71Sopenharmony_ci .width(200) 603e41f4b71Sopenharmony_ci .height(100) 604e41f4b71Sopenharmony_ci .backgroundColor(0xF5DEB3) 605e41f4b71Sopenharmony_ci }.width(400).height(120).padding(10).backgroundColor(0xAFEEEE) 606e41f4b71Sopenharmony_ci ``` 607e41f4b71Sopenharmony_ci 608e41f4b71Sopenharmony_ci  609e41f4b71Sopenharmony_ci 610e41f4b71Sopenharmony_ci 611e41f4b71Sopenharmony_ci## Example 612e41f4b71Sopenharmony_ci 613e41f4b71Sopenharmony_ciIn this example, child elements are arranged horizontally in the flex layout, aligned at both edges, evenly spaced, and centered in the vertical direction. 614e41f4b71Sopenharmony_ci 615e41f4b71Sopenharmony_ci 616e41f4b71Sopenharmony_ci```ts 617e41f4b71Sopenharmony_ci@Entry 618e41f4b71Sopenharmony_ci@Component 619e41f4b71Sopenharmony_cistruct FlexExample { 620e41f4b71Sopenharmony_ci build() { 621e41f4b71Sopenharmony_ci Column() { 622e41f4b71Sopenharmony_ci Column({ space: 5 }) { 623e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Row, wrap: FlexWrap.NoWrap, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { 624e41f4b71Sopenharmony_ci Text('1').width('30%').height(50).backgroundColor(0xF5DEB3) 625e41f4b71Sopenharmony_ci Text('2').width('30%').height(50).backgroundColor(0xD2B48C) 626e41f4b71Sopenharmony_ci Text('3').width('30%').height(50).backgroundColor(0xF5DEB3) 627e41f4b71Sopenharmony_ci } 628e41f4b71Sopenharmony_ci .height(70) 629e41f4b71Sopenharmony_ci .width('90%') 630e41f4b71Sopenharmony_ci .backgroundColor(0xAFEEEE) 631e41f4b71Sopenharmony_ci }.width('100%').margin({ top: 5 }) 632e41f4b71Sopenharmony_ci }.width('100%') 633e41f4b71Sopenharmony_ci } 634e41f4b71Sopenharmony_ci} 635e41f4b71Sopenharmony_ci``` 636e41f4b71Sopenharmony_ci 637e41f4b71Sopenharmony_ci 638