1# Stack Layout (Stack) 2 3 4## Overview 5 6The stack layout reserves an area on the screen to display elements in a component and allows the elements to be stacked. You can implement a stack layout through the [\<Stack>](../reference/arkui-ts/ts-container-stack.md) component, which provides a stack container where positioned or non-positioned child elements are pushed successively and the latter one sits on top of the previous one. 7 8The stack layout excels at page stacking and positioning, and is widely used in ad and widget arrangement. 9 10In the **\<Stack>** component shown in Figure 1, the sequence of child elements is Item1 -> Item2 -> Item3. 11 12 13 **Figure 1** Stack layout 14 15 16 17 18## How to Develop 19 20The **\<Stack>** component can contain various child elements, which are stacked in the center by default. While respecting the constraints of **\<Stack>**, child elements are laid out in their respective style. 21 22 23 24```ts 25let MTop:Record<string,number> = { 'top': 50 } 26Column(){ 27 Stack({ }) { 28 Column(){}.width('90%').height('100%').backgroundColor('#ff58b87c') 29 Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa') 30 Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000') 31 }.width('100%').height(150).margin(MTop) 32} 33``` 34 35 36 37 38 39## Alignment 40 41Alignment of elements in the **\<Stack>** component is set through the [alignContent](../reference/arkui-ts/ts-appendix-enums.md#alignment) parameter. As shown in Figure 2, nine alignment modes are supported. 42 43 **Figure 2** Alignment modes in the \<Stack> component 44 45 46 47 48## Z-order Control 49 50The stacking order of child elements in the **\<Stack>** component is set through the **[zIndex](../reference/arkui-ts/ts-universal-attributes-z-order.md)** attribute. A larger **zIndex** value indicates a higher display level. 51 52 In the stack layout, if the size of an element is greater than that of the one before it, the one before it is hidden. 53 54```ts 55let MTopM1:Record<string,number> = { 'top': 100 } 56Stack({ alignContent: Alignment.BottomStart }) { 57 Column() { 58 Text ('Stacked element 1').textAlign (TextAlign.End).fontSize (20) 59 }.width(100).height(100).backgroundColor(0xffd306) 60 61 Column() { 62 Text ('Stacked element 2').fontSize (20) 63 }.width(150).height(150).backgroundColor(Color.Pink) 64 65 Column() { 66 Text ('Stacked element 3').fontSize (20) 67 }.width(200).height(200).backgroundColor(Color.Grey) 68}.margin(MTopM1).width(350).height(350).backgroundColor(0xe0e0e0) 69``` 70 71 72 73In the following figure, the size of the stacked element 3 is greater than that of all the elements before it. Therefore, the first two elements are completely hidden. To show these elements, modify their **zIndex** attribute settings. 74 75 76```ts 77let MTopM:Record<string,number> = { 'top': 100 } 78Stack({ alignContent: Alignment.BottomStart }) { 79 Column() { 80 Text ('Stacked element 1').fontSize (20) 81 }.width(100).height(100).backgroundColor(0xffd306).zIndex(2) 82 83 Column() { 84 Text ('Stacked element 2').fontSize (20) 85 }.width(150).height(150).backgroundColor(Color.Pink).zIndex(1) 86 87 Column() { 88 Text ('Stacked element 3').fontSize (20) 89 }.width(200).height(200).backgroundColor(Color.Grey) 90}.margin(MTopM).width(350).height(350).backgroundColor(0xe0e0e0) 91``` 92 93 94 95 96## Example Scenario 97 98In this example, the stack layout is used to quickly set up a page. 99 100 101```ts 102@Entry 103@Component 104struct StackSample { 105 private arr: string[] = ['APP1', 'APP2', 'APP3', 'APP4', 'APP5', 'APP6', 'APP7', 'APP8']; 106 107 build() { 108 Stack({ alignContent: Alignment.Bottom }) { 109 Flex({ wrap: FlexWrap.Wrap }) { 110 ForEach(this.arr, (item:string) => { 111 Text(item) 112 .width(100) 113 .height(100) 114 .fontSize(16) 115 .margin(10) 116 .textAlign(TextAlign.Center) 117 .borderRadius(10) 118 .backgroundColor(0xFFFFFF) 119 }, (item:string):string => item) 120 }.width('100%').height('100%') 121 122 Flex({ justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) { 123 Text ('Contacts').fontSize (16) 124 Text ('Settings').fontSize (16) 125 Text ('Messaging').fontSize (16) 126 } 127 .width('50%') 128 .height(50) 129 .backgroundColor('#16302e2e') 130 .margin({ bottom: 15 }) 131 .borderRadius(15) 132 }.width('100%').height('100%').backgroundColor('#CFD0CF') 133 } 134} 135``` 136 137 138 139