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/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.
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![stack-layout](figures/stack-layout.png)
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```ts
23// xxx.ets
24let MTop:Record<string,number> = { 'top': 50 }
25
26@Entry
27@Component
28struct StackExample {
29  build() {
30    Column(){
31      Stack({ }) {
32        Column(){}.width('90%').height('100%').backgroundColor('#ff58b87c')
33        Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa')
34        Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')
35      }.width('100%').height(150).margin(MTop)
36    }
37  }
38}
39```
40
41
42![stack-layout-sample](figures/stack-layout-sample.png)
43
44
45## Alignment
46
47Alignment 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.
48
49  **Figure 2** Alignment modes in the Stack component
50
51![en-us_image_0000001562940621](figures/en-us_image_0000001562940621.png)
52
53```ts
54// xxx.ets
55@Entry
56@Component
57struct StackExample {
58  build() {
59    Stack({ alignContent: Alignment.TopStart }) {
60      Text('Stack').width('90%').height('100%').backgroundColor('#e1dede').align(Alignment.BottomEnd)
61      Text('Item 1').width('70%').height('80%').backgroundColor(0xd2cab3).align(Alignment.BottomEnd)
62      Text('Item 2').width('50%').height('60%').backgroundColor(0xc1cbac).align(Alignment.BottomEnd)
63    }.width('100%').height(150).margin({ top: 5 })
64  }
65}
66```
67
68## Z-order Control
69
70The 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.
71
72  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.
73
74```ts
75Stack({ alignContent: Alignment.BottomStart }) {
76  Column() {
77    Text('Stacked element 1').textAlign(TextAlign.End).fontSize(20)
78  }.width(100).height(100).backgroundColor(0xffd306)
79
80  Column() {
81    Text('Stacked element 2').fontSize(20)
82  }.width(150).height(150).backgroundColor(Color.Pink)
83
84  Column() {
85    Text('Stacked element 3').fontSize(20)
86  }.width(200).height(200).backgroundColor(Color.Grey)
87}.width(350).height(350).backgroundColor(0xe0e0e0)
88```
89
90![en-us_image_0000001511900544](figures/en-us_image_0000001511900544.png)
91
92In 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.
93
94
95```ts
96Stack({ alignContent: Alignment.BottomStart }) {
97  Column() {
98    Text('Stacked element 1').fontSize(20)
99  }.width(100).height(100).backgroundColor(0xffd306).zIndex(2)
100
101  Column() {
102    Text('Stacked element 2').fontSize(20)
103  }.width(150).height(150).backgroundColor(Color.Pink).zIndex(1)
104
105  Column() {
106    Text('Stacked element 3').fontSize(20)
107  }.width(200).height(200).backgroundColor(Color.Grey)
108}.width(350).height(350).backgroundColor(0xe0e0e0)
109```
110
111![en-us_image_0000001563060797](figures/en-us_image_0000001563060797.png)
112
113
114## Example Scenario
115
116In this example, the stack layout is used to quickly set up a page.
117
118
119```ts
120@Entry
121@Component
122struct StackSample {
123  private arr: string[] = ['APP1', 'APP2', 'APP3', 'APP4', 'APP5', 'APP6', 'APP7', 'APP8'];
124
125  build() {
126    Stack({ alignContent: Alignment.Bottom }) {
127      Flex({ wrap: FlexWrap.Wrap }) {
128        ForEach(this.arr, (item:string) => {
129          Text(item)
130            .width(100)
131            .height(100)
132            .fontSize(16)
133            .margin(10)
134            .textAlign(TextAlign.Center)
135            .borderRadius(10)
136            .backgroundColor(0xFFFFFF)
137        }, (item:string):string => item)
138      }.width('100%').height('100%')
139
140      Flex({ justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) {
141        Text('Contacts').fontSize(16)
142        Text('Settings').fontSize(16)
143        Text('Messaging').fontSize(16)
144      }
145      .width('50%')
146      .height(50)
147      .backgroundColor('#16302e2e')
148      .margin({ bottom: 15 })
149      .borderRadius(15)
150    }.width('100%').height('100%').backgroundColor('#CFD0CF')
151  }
152}
153```
154
155
156![en-us_image_0000001511421368](figures/en-us_image_0000001511421368.png)
157<!--RP1--><!--RP1End-->
158