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