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