1e41f4b71Sopenharmony_ci# Stack Layout (Stack) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Overview 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciThe 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/apis-arkui/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. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ciThe stack layout excels at page stacking and positioning, and is widely used in ad and widget arrangement. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ciIn the **Stack** component shown in Figure 1, the sequence of child elements is Item1 -> Item2 -> Item3. 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci **Figure 1** Stack layout 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci## How to Develop 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ciThe **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. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci```ts 23e41f4b71Sopenharmony_ci// xxx.ets 24e41f4b71Sopenharmony_cilet MTop:Record<string,number> = { 'top': 50 } 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci@Entry 27e41f4b71Sopenharmony_ci@Component 28e41f4b71Sopenharmony_cistruct StackExample { 29e41f4b71Sopenharmony_ci build() { 30e41f4b71Sopenharmony_ci Column(){ 31e41f4b71Sopenharmony_ci Stack({ }) { 32e41f4b71Sopenharmony_ci Column(){}.width('90%').height('100%').backgroundColor('#ff58b87c') 33e41f4b71Sopenharmony_ci Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa') 34e41f4b71Sopenharmony_ci Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000') 35e41f4b71Sopenharmony_ci }.width('100%').height(150).margin(MTop) 36e41f4b71Sopenharmony_ci } 37e41f4b71Sopenharmony_ci } 38e41f4b71Sopenharmony_ci} 39e41f4b71Sopenharmony_ci``` 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci## Alignment 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ciAlignment of elements in the **Stack** component is set through the [alignContent](../reference/apis-arkui/arkui-ts/ts-container-stack.md#aligncontent) parameter. As shown in Figure 2, nine alignment modes are supported. 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci **Figure 2** Alignment modes in the Stack component 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci```ts 54e41f4b71Sopenharmony_ci// xxx.ets 55e41f4b71Sopenharmony_ci@Entry 56e41f4b71Sopenharmony_ci@Component 57e41f4b71Sopenharmony_cistruct StackExample { 58e41f4b71Sopenharmony_ci build() { 59e41f4b71Sopenharmony_ci Stack({ alignContent: Alignment.TopStart }) { 60e41f4b71Sopenharmony_ci Text('Stack').width('90%').height('100%').backgroundColor('#e1dede').align(Alignment.BottomEnd) 61e41f4b71Sopenharmony_ci Text('Item 1').width('70%').height('80%').backgroundColor(0xd2cab3).align(Alignment.BottomEnd) 62e41f4b71Sopenharmony_ci Text('Item 2').width('50%').height('60%').backgroundColor(0xc1cbac).align(Alignment.BottomEnd) 63e41f4b71Sopenharmony_ci }.width('100%').height(150).margin({ top: 5 }) 64e41f4b71Sopenharmony_ci } 65e41f4b71Sopenharmony_ci} 66e41f4b71Sopenharmony_ci``` 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci## Z-order Control 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ciThe stacking order of child elements in the **Stack** component is set through the [zIndex](../reference/apis-arkui/arkui-ts/ts-universal-attributes-z-order.md) attribute. A larger **zIndex** value indicates a higher display level. 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci 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. 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci```ts 75e41f4b71Sopenharmony_ciStack({ alignContent: Alignment.BottomStart }) { 76e41f4b71Sopenharmony_ci Column() { 77e41f4b71Sopenharmony_ci Text ('Stacked element 1').textAlign (TextAlign.End).fontSize (20) 78e41f4b71Sopenharmony_ci }.width(100).height(100).backgroundColor(0xffd306) 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci Column() { 81e41f4b71Sopenharmony_ci Text ('Stacked element 2').fontSize (20) 82e41f4b71Sopenharmony_ci }.width(150).height(150).backgroundColor(Color.Pink) 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci Column() { 85e41f4b71Sopenharmony_ci Text ('Stacked element 3').fontSize (20) 86e41f4b71Sopenharmony_ci }.width(200).height(200).backgroundColor(Color.Grey) 87e41f4b71Sopenharmony_ci}.width(350).height(350).backgroundColor(0xe0e0e0) 88e41f4b71Sopenharmony_ci``` 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ciIn the preceding 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. 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci```ts 96e41f4b71Sopenharmony_ciStack({ alignContent: Alignment.BottomStart }) { 97e41f4b71Sopenharmony_ci Column() { 98e41f4b71Sopenharmony_ci Text ('Stacked element 1').fontSize (20) 99e41f4b71Sopenharmony_ci }.width(100).height(100).backgroundColor(0xffd306).zIndex(2) 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci Column() { 102e41f4b71Sopenharmony_ci Text ('Stacked element 2').fontSize (20) 103e41f4b71Sopenharmony_ci }.width(150).height(150).backgroundColor(Color.Pink).zIndex(1) 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci Column() { 106e41f4b71Sopenharmony_ci Text ('Stacked element 3').fontSize (20) 107e41f4b71Sopenharmony_ci }.width(200).height(200).backgroundColor(Color.Grey) 108e41f4b71Sopenharmony_ci}.width(350).height(350).backgroundColor(0xe0e0e0) 109e41f4b71Sopenharmony_ci``` 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci## Example Scenario 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ciIn this example, the stack layout is used to quickly set up a page. 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci```ts 120e41f4b71Sopenharmony_ci@Entry 121e41f4b71Sopenharmony_ci@Component 122e41f4b71Sopenharmony_cistruct StackSample { 123e41f4b71Sopenharmony_ci private arr: string[] = ['APP1', 'APP2', 'APP3', 'APP4', 'APP5', 'APP6', 'APP7', 'APP8']; 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci build() { 126e41f4b71Sopenharmony_ci Stack({ alignContent: Alignment.Bottom }) { 127e41f4b71Sopenharmony_ci Flex({ wrap: FlexWrap.Wrap }) { 128e41f4b71Sopenharmony_ci ForEach(this.arr, (item:string) => { 129e41f4b71Sopenharmony_ci Text(item) 130e41f4b71Sopenharmony_ci .width(100) 131e41f4b71Sopenharmony_ci .height(100) 132e41f4b71Sopenharmony_ci .fontSize(16) 133e41f4b71Sopenharmony_ci .margin(10) 134e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 135e41f4b71Sopenharmony_ci .borderRadius(10) 136e41f4b71Sopenharmony_ci .backgroundColor(0xFFFFFF) 137e41f4b71Sopenharmony_ci }, (item:string):string => item) 138e41f4b71Sopenharmony_ci }.width('100%').height('100%') 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci Flex({ justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) { 141e41f4b71Sopenharmony_ci Text ('Contacts').fontSize (16) 142e41f4b71Sopenharmony_ci Text ('Settings').fontSize (16) 143e41f4b71Sopenharmony_ci Text ('Messaging').fontSize (16) 144e41f4b71Sopenharmony_ci } 145e41f4b71Sopenharmony_ci .width('50%') 146e41f4b71Sopenharmony_ci .height(50) 147e41f4b71Sopenharmony_ci .backgroundColor('#16302e2e') 148e41f4b71Sopenharmony_ci .margin({ bottom: 15 }) 149e41f4b71Sopenharmony_ci .borderRadius(15) 150e41f4b71Sopenharmony_ci }.width('100%').height('100%').backgroundColor('#CFD0CF') 151e41f4b71Sopenharmony_ci } 152e41f4b71Sopenharmony_ci} 153e41f4b71Sopenharmony_ci``` 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci 157