1e41f4b71Sopenharmony_ci# Relative Layout (RelativeContainer) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Overview 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciDuring application development, nesting components – same or different – is common in page layout, especially when the target page is complicated. Yet, nesting components too deeply, or nesting too many components, can be especially expensive. Naturally, optimizing the layout hierarchies can effectively lead to better performance and less time overhead. <!--Del-->For details about how the relative container is more performance-efficient than the list, see [Improving Layout Performance](../performance/reduce-view-nesting-levels.md).<!--DelEnd--> 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ciThe relative layout, implemented using the **RelativeContainer** container component, is used to lay out child elements in relative positions. It is applicable to element alignment in complex scenarios. A child element can use the container or another child element as the anchor, based on which its relative position is determined. Below shows a relative layout. The dotted lines in the figure indicate the position dependency. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci **Figure 1** Relative layout 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ciA child element does not necessarily adopt the dependency shown above to determine its relative position. For example, Item4 may use Item2 or the **RelativeContainer** parent container as a dependency anchor. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci## Basic Concepts 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci- Anchor: element relative to which an element's position is specified. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci- Alignment mode: how the current element is aligned with the anchor, which can be top-, center-, or bottom-aligned in the vertical direction or left-, center-, and right-aligned in the horizontal direction. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci## Setting the Dependency 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci### Setting the Anchor 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciBy setting the anchor, you set a position dependency relationship between a child element and its parent element or sibling elements. In the horizontal direction, you can set the left, middle, and right anchors. In the vertical direction, you can set the top, center, and bottom anchors. 32e41f4b71Sopenharmony_ciTo specify anchors, you must set IDs for the **RelativeContainer** component and its child elements. The default ID is **\_\_container\_\_**, and the IDs for the remaining child elements are set through the **id** attribute. Components without **id** set can be displayed but cannot be used as anchors by other child components; the relative layout container will generate an ID for them, and the pattern of this ID is not predictable by the application logic. When a mutual or circular dependency occurs, none of the child components in the container are drawn. If anchors are set for more than two positions in a single direction but the anchor positions are reversed, the size of the child component is 0, which means that the child component is not drawn. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci>**NOTE** 35e41f4b71Sopenharmony_ci> 36e41f4b71Sopenharmony_ci>When using anchors, pay attention to the relative positions of child elements to avoid misplacement or blocking. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci- The ID of the **RelativeContainer** parent component is **__container__**. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci ```ts 41e41f4b71Sopenharmony_ci let AlignRus:Record<string,Record<string,string|VerticalAlign|HorizontalAlign>> = { 42e41f4b71Sopenharmony_ci 'top': { 'anchor': '__container__', 'align': VerticalAlign.Top }, 43e41f4b71Sopenharmony_ci 'left': { 'anchor': '__container__', 'align': HorizontalAlign.Start } 44e41f4b71Sopenharmony_ci } 45e41f4b71Sopenharmony_ci let AlignRue:Record<string,Record<string,string|VerticalAlign|HorizontalAlign>> = { 46e41f4b71Sopenharmony_ci 'top': { 'anchor': '__container__', 'align': VerticalAlign.Top }, 47e41f4b71Sopenharmony_ci 'right': { 'anchor': '__container__', 'align': HorizontalAlign.End } 48e41f4b71Sopenharmony_ci } 49e41f4b71Sopenharmony_ci let Mleft:Record<string,number> = { 'left': 20 } 50e41f4b71Sopenharmony_ci let BWC:Record<string,number|string> = { 'width': 2, 'color': '#6699FF' } 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci @Entry 53e41f4b71Sopenharmony_ci @Component 54e41f4b71Sopenharmony_ci struct Index { 55e41f4b71Sopenharmony_ci build() { 56e41f4b71Sopenharmony_ci RelativeContainer() { 57e41f4b71Sopenharmony_ci Row(){Text('row1')}.justifyContent(FlexAlign.Center).width(100).height(100) 58e41f4b71Sopenharmony_ci .backgroundColor("#FF3333") 59e41f4b71Sopenharmony_ci .alignRules(AlignRus) 60e41f4b71Sopenharmony_ci .id("row1") 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci Row(){Text('row2')}.justifyContent(FlexAlign.Center).width(100).height(100) 63e41f4b71Sopenharmony_ci .backgroundColor("#FFCC00") 64e41f4b71Sopenharmony_ci .alignRules(AlignRue) 65e41f4b71Sopenharmony_ci .id("row2") 66e41f4b71Sopenharmony_ci }.width(300).height(300) 67e41f4b71Sopenharmony_ci .margin(Mleft) 68e41f4b71Sopenharmony_ci .border(BWC) 69e41f4b71Sopenharmony_ci } 70e41f4b71Sopenharmony_ci } 71e41f4b71Sopenharmony_ci ``` 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci  74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci- Example of using a sibling element as the anchor: 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci ```ts 78e41f4b71Sopenharmony_ci let AlignRus:Record<string,Record<string,string|VerticalAlign|HorizontalAlign>> = { 79e41f4b71Sopenharmony_ci 'top': { 'anchor': '__container__', 'align': VerticalAlign.Top }, 80e41f4b71Sopenharmony_ci 'left': { 'anchor': '__container__', 'align': HorizontalAlign.Start } 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci let RelConB:Record<string,Record<string,string|VerticalAlign|HorizontalAlign>> = { 83e41f4b71Sopenharmony_ci 'top': { 'anchor': 'row1', 'align': VerticalAlign.Bottom }, 84e41f4b71Sopenharmony_ci 'left' : { 'anchor': 'row1', 'align': HorizontalAlign.Start } 85e41f4b71Sopenharmony_ci } 86e41f4b71Sopenharmony_ci let Mleft:Record<string,number> = { 'left': 20 } 87e41f4b71Sopenharmony_ci let BWC:Record<string,number|string> = { 'width': 2, 'color': '#6699FF' } 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci @Entry 90e41f4b71Sopenharmony_ci @Component 91e41f4b71Sopenharmony_ci struct Index { 92e41f4b71Sopenharmony_ci build() { 93e41f4b71Sopenharmony_ci RelativeContainer() { 94e41f4b71Sopenharmony_ci Row(){Text('row1')}.justifyContent(FlexAlign.Center).width(100).height(100) 95e41f4b71Sopenharmony_ci .backgroundColor("#FF3333") 96e41f4b71Sopenharmony_ci .alignRules(AlignRus) 97e41f4b71Sopenharmony_ci .id("row1") 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci Row(){Text('row2')}.justifyContent(FlexAlign.Center).width(100).height(100) 100e41f4b71Sopenharmony_ci .backgroundColor("#FFCC00") 101e41f4b71Sopenharmony_ci .alignRules(RelConB) 102e41f4b71Sopenharmony_ci .id("row2") 103e41f4b71Sopenharmony_ci }.width(300).height(300) 104e41f4b71Sopenharmony_ci .margin(Mleft) 105e41f4b71Sopenharmony_ci .border(BWC) 106e41f4b71Sopenharmony_ci } 107e41f4b71Sopenharmony_ci } 108e41f4b71Sopenharmony_ci ``` 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci  111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci- Make sure the anchors of a child component do not depend on each other. 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci ```ts 115e41f4b71Sopenharmony_ci @Entry 116e41f4b71Sopenharmony_ci @Component 117e41f4b71Sopenharmony_ci struct Index { 118e41f4b71Sopenharmony_ci build() { 119e41f4b71Sopenharmony_ci Row() { 120e41f4b71Sopenharmony_ci RelativeContainer() { 121e41f4b71Sopenharmony_ci Row(){Text('row1')}.justifyContent(FlexAlign.Center).width(100).height(100) 122e41f4b71Sopenharmony_ci .backgroundColor('#ff3339ff') 123e41f4b71Sopenharmony_ci .alignRules({ 124e41f4b71Sopenharmony_ci top: {anchor: "__container__", align: VerticalAlign.Top}, 125e41f4b71Sopenharmony_ci left: {anchor: "__container__", align: HorizontalAlign.Start} 126e41f4b71Sopenharmony_ci }) 127e41f4b71Sopenharmony_ci .id("row1") 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci Row(){Text('row2')}.justifyContent(FlexAlign.Center).width(100) 130e41f4b71Sopenharmony_ci .backgroundColor('#ff298e1e') 131e41f4b71Sopenharmony_ci .alignRules({ 132e41f4b71Sopenharmony_ci top: {anchor: "__container__", align: VerticalAlign.Top}, 133e41f4b71Sopenharmony_ci right: {anchor: "__container__", align: HorizontalAlign.End}, 134e41f4b71Sopenharmony_ci bottom: {anchor: "row1", align: VerticalAlign.Center}, 135e41f4b71Sopenharmony_ci }) 136e41f4b71Sopenharmony_ci .id("row2") 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci Row(){Text('row3')}.justifyContent(FlexAlign.Center).height(100) 139e41f4b71Sopenharmony_ci .backgroundColor('#ffff6a33') 140e41f4b71Sopenharmony_ci .alignRules({ 141e41f4b71Sopenharmony_ci top: {anchor: "row1", align: VerticalAlign.Bottom}, 142e41f4b71Sopenharmony_ci left: {anchor: "row1", align: HorizontalAlign.Start}, 143e41f4b71Sopenharmony_ci right: {anchor: "row2", align: HorizontalAlign.Start} 144e41f4b71Sopenharmony_ci }) 145e41f4b71Sopenharmony_ci .id("row3") 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci Row(){Text('row4')}.justifyContent(FlexAlign.Center) 148e41f4b71Sopenharmony_ci .backgroundColor('#ffff33fd') 149e41f4b71Sopenharmony_ci .alignRules({ 150e41f4b71Sopenharmony_ci top: {anchor: "row3", align: VerticalAlign.Bottom}, 151e41f4b71Sopenharmony_ci left: {anchor: "row1", align: HorizontalAlign.Center}, 152e41f4b71Sopenharmony_ci right: {anchor: "row2", align: HorizontalAlign.End}, 153e41f4b71Sopenharmony_ci bottom: {anchor: "__container__", align: VerticalAlign.Bottom} 154e41f4b71Sopenharmony_ci }) 155e41f4b71Sopenharmony_ci .id("row4") 156e41f4b71Sopenharmony_ci } 157e41f4b71Sopenharmony_ci .width(300).height(300) 158e41f4b71Sopenharmony_ci .margin({left: 50}) 159e41f4b71Sopenharmony_ci .border({width:2, color: "#6699FF"}) 160e41f4b71Sopenharmony_ci } 161e41f4b71Sopenharmony_ci .height('100%') 162e41f4b71Sopenharmony_ci } 163e41f4b71Sopenharmony_ci } 164e41f4b71Sopenharmony_ci ``` 165e41f4b71Sopenharmony_ci  166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci### Setting Alignment Relative to the Anchor 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ciAfter an anchor is set, you can use **align** to set the alignment mode relative to the anchor. 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ciAlignment modes in the horizontal direction can be left, center, or right, achieved by the **HorizontalAlign.Start**, **HorizontalAlign.Center**, and **HorizontalAlign.End** attributes, respectively. 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ciAlignment modes in the vertical direction can be top, center, or bottom, achieved by the **VerticalAlign.Top**, **VerticalAlign.Center**, and **VerticalAlign.Bottom** attributes, respectively. 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci### Setting Offset 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ciAfter being aligned relative to the anchor, a child component may be still not at its target position. In this case, you can set the offset. 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci ```ts 184e41f4b71Sopenharmony_ci @Entry 185e41f4b71Sopenharmony_ci @Component 186e41f4b71Sopenharmony_ci struct Index { 187e41f4b71Sopenharmony_ci build() { 188e41f4b71Sopenharmony_ci Row() { 189e41f4b71Sopenharmony_ci RelativeContainer() { 190e41f4b71Sopenharmony_ci Row(){Text('row1')}.justifyContent(FlexAlign.Center).width(100).height(100) 191e41f4b71Sopenharmony_ci .backgroundColor("#FF3333") 192e41f4b71Sopenharmony_ci .alignRules({ 193e41f4b71Sopenharmony_ci top: {anchor: "__container__", align: VerticalAlign.Top}, 194e41f4b71Sopenharmony_ci left: {anchor: "__container__", align: HorizontalAlign.Start} 195e41f4b71Sopenharmony_ci }) 196e41f4b71Sopenharmony_ci .id("row1") 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci Row(){Text('row2')}.justifyContent(FlexAlign.Center).width(100) 199e41f4b71Sopenharmony_ci .backgroundColor("#FFCC00") 200e41f4b71Sopenharmony_ci .alignRules({ 201e41f4b71Sopenharmony_ci top: {anchor: "__container__", align: VerticalAlign.Top}, 202e41f4b71Sopenharmony_ci right: {anchor: "__container__", align: HorizontalAlign.End}, 203e41f4b71Sopenharmony_ci bottom: {anchor: "row1", align: VerticalAlign.Center}, 204e41f4b71Sopenharmony_ci }) 205e41f4b71Sopenharmony_ci .offset({ 206e41f4b71Sopenharmony_ci x:-40, 207e41f4b71Sopenharmony_ci y:-20 208e41f4b71Sopenharmony_ci }) 209e41f4b71Sopenharmony_ci .id("row2") 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci Row(){Text('row3')}.justifyContent(FlexAlign.Center).height(100) 212e41f4b71Sopenharmony_ci .backgroundColor("#FF6633") 213e41f4b71Sopenharmony_ci .alignRules({ 214e41f4b71Sopenharmony_ci top: {anchor: "row1", align: VerticalAlign.Bottom}, 215e41f4b71Sopenharmony_ci left: {anchor: "row1", align: HorizontalAlign.End}, 216e41f4b71Sopenharmony_ci right: {anchor: "row2", align: HorizontalAlign.Start} 217e41f4b71Sopenharmony_ci }) 218e41f4b71Sopenharmony_ci .offset({ 219e41f4b71Sopenharmony_ci x:-10, 220e41f4b71Sopenharmony_ci y:-20 221e41f4b71Sopenharmony_ci }) 222e41f4b71Sopenharmony_ci .id("row3") 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci Row(){Text('row4')}.justifyContent(FlexAlign.Center) 225e41f4b71Sopenharmony_ci .backgroundColor("#FF9966") 226e41f4b71Sopenharmony_ci .alignRules({ 227e41f4b71Sopenharmony_ci top: {anchor: "row3", align: VerticalAlign.Bottom}, 228e41f4b71Sopenharmony_ci bottom: {anchor: "__container__", align: VerticalAlign.Bottom}, 229e41f4b71Sopenharmony_ci left: {anchor: "__container__", align: HorizontalAlign.Start}, 230e41f4b71Sopenharmony_ci right: {anchor: "row1", align: HorizontalAlign.End} 231e41f4b71Sopenharmony_ci }) 232e41f4b71Sopenharmony_ci .offset({ 233e41f4b71Sopenharmony_ci x:-10, 234e41f4b71Sopenharmony_ci y:-30 235e41f4b71Sopenharmony_ci }) 236e41f4b71Sopenharmony_ci .id("row4") 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci Row(){Text('row5')}.justifyContent(FlexAlign.Center) 239e41f4b71Sopenharmony_ci .backgroundColor("#FF66FF") 240e41f4b71Sopenharmony_ci .alignRules({ 241e41f4b71Sopenharmony_ci top: {anchor: "row3", align: VerticalAlign.Bottom}, 242e41f4b71Sopenharmony_ci bottom: {anchor: "__container__", align: VerticalAlign.Bottom}, 243e41f4b71Sopenharmony_ci left: {anchor: "row2", align: HorizontalAlign.Start}, 244e41f4b71Sopenharmony_ci right: {anchor: "row2", align: HorizontalAlign.End} 245e41f4b71Sopenharmony_ci }) 246e41f4b71Sopenharmony_ci .offset({ 247e41f4b71Sopenharmony_ci x:10, 248e41f4b71Sopenharmony_ci y:20 249e41f4b71Sopenharmony_ci }) 250e41f4b71Sopenharmony_ci .id("row5") 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci Row(){Text('row6')}.justifyContent(FlexAlign.Center) 253e41f4b71Sopenharmony_ci .backgroundColor('#ff33ffb5') 254e41f4b71Sopenharmony_ci .alignRules({ 255e41f4b71Sopenharmony_ci top: {anchor: "row3", align: VerticalAlign.Bottom}, 256e41f4b71Sopenharmony_ci bottom: {anchor: "row4", align: VerticalAlign.Bottom}, 257e41f4b71Sopenharmony_ci left: {anchor: "row3", align: HorizontalAlign.Start}, 258e41f4b71Sopenharmony_ci right: {anchor: "row3", align: HorizontalAlign.End} 259e41f4b71Sopenharmony_ci }) 260e41f4b71Sopenharmony_ci .offset({ 261e41f4b71Sopenharmony_ci x:-15, 262e41f4b71Sopenharmony_ci y:10 263e41f4b71Sopenharmony_ci }) 264e41f4b71Sopenharmony_ci .backgroundImagePosition(Alignment.Bottom) 265e41f4b71Sopenharmony_ci .backgroundImageSize(ImageSize.Cover) 266e41f4b71Sopenharmony_ci .id("row6") 267e41f4b71Sopenharmony_ci } 268e41f4b71Sopenharmony_ci .width(300).height(300) 269e41f4b71Sopenharmony_ci .margin({left: 50}) 270e41f4b71Sopenharmony_ci .border({width:2, color: "#6699FF"}) 271e41f4b71Sopenharmony_ci } 272e41f4b71Sopenharmony_ci .height('100%') 273e41f4b71Sopenharmony_ci } 274e41f4b71Sopenharmony_ci } 275e41f4b71Sopenharmony_ci ``` 276e41f4b71Sopenharmony_ci  277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci## Aligning Components in Multiple Layouts 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ciYou can set components in multiple layout components, such as **Row**, **Column**, **Flex**, and **Stack**, to be aligned based on the relative layout rules. 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci ```ts 283e41f4b71Sopenharmony_ci @Entry 284e41f4b71Sopenharmony_ci @Component 285e41f4b71Sopenharmony_ci struct Index { 286e41f4b71Sopenharmony_ci @State value: number = 0 287e41f4b71Sopenharmony_ci build() { 288e41f4b71Sopenharmony_ci Row() { 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci RelativeContainer() { 291e41f4b71Sopenharmony_ci Row().width(100).height(100) 292e41f4b71Sopenharmony_ci .backgroundColor('#ff33ffcc') 293e41f4b71Sopenharmony_ci .alignRules({ 294e41f4b71Sopenharmony_ci top: {anchor: "__container__", align: VerticalAlign.Top}, 295e41f4b71Sopenharmony_ci left: {anchor: "__container__", align: HorizontalAlign.Start} 296e41f4b71Sopenharmony_ci }) 297e41f4b71Sopenharmony_ci .id("row1") 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci Column().width('50%').height(30).backgroundColor(0xAFEEEE) 300e41f4b71Sopenharmony_ci .alignRules({ 301e41f4b71Sopenharmony_ci top: {anchor: "__container__", align: VerticalAlign.Top}, 302e41f4b71Sopenharmony_ci left: {anchor: "__container__", align: HorizontalAlign.Center} 303e41f4b71Sopenharmony_ci }).id("row2") 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Row }) { 306e41f4b71Sopenharmony_ci Text('1').width('20%').height(50).backgroundColor(0xF5DEB3) 307e41f4b71Sopenharmony_ci Text('2').width('20%').height(50).backgroundColor(0xD2B48C) 308e41f4b71Sopenharmony_ci Text('3').width('20%').height(50).backgroundColor(0xF5DEB3) 309e41f4b71Sopenharmony_ci Text('4').width('20%').height(50).backgroundColor(0xD2B48C) 310e41f4b71Sopenharmony_ci } 311e41f4b71Sopenharmony_ci .padding(10) 312e41f4b71Sopenharmony_ci .backgroundColor('#ffedafaf') 313e41f4b71Sopenharmony_ci .alignRules({ 314e41f4b71Sopenharmony_ci top: {anchor: "row2", align: VerticalAlign.Bottom}, 315e41f4b71Sopenharmony_ci left: {anchor: "__container__", align: HorizontalAlign.Start}, 316e41f4b71Sopenharmony_ci bottom: {anchor: "__container__", align: VerticalAlign.Center}, 317e41f4b71Sopenharmony_ci right: {anchor: "row2", align: HorizontalAlign.Center} 318e41f4b71Sopenharmony_ci }) 319e41f4b71Sopenharmony_ci .id("row3") 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci Stack({ alignContent: Alignment.Bottom }) { 322e41f4b71Sopenharmony_ci Text('First child, show in bottom').width('90%').height('100%').backgroundColor(0xd2cab3).align(Alignment.Top) 323e41f4b71Sopenharmony_ci Text('Second child, show in top').width('70%').height('60%').backgroundColor(0xc1cbac).align(Alignment.Top) 324e41f4b71Sopenharmony_ci } 325e41f4b71Sopenharmony_ci .margin({ top: 5 }) 326e41f4b71Sopenharmony_ci .alignRules({ 327e41f4b71Sopenharmony_ci top: {anchor: "row3", align: VerticalAlign.Bottom}, 328e41f4b71Sopenharmony_ci left: {anchor: "__container__", align: HorizontalAlign.Start}, 329e41f4b71Sopenharmony_ci bottom: {anchor: "__container__", align: VerticalAlign.Bottom}, 330e41f4b71Sopenharmony_ci right: {anchor: "row3", align: HorizontalAlign.End} 331e41f4b71Sopenharmony_ci }) 332e41f4b71Sopenharmony_ci .id("row4") 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_ci } 335e41f4b71Sopenharmony_ci .width(300).height(300) 336e41f4b71Sopenharmony_ci .margin({left: 50}) 337e41f4b71Sopenharmony_ci .border({width:2, color: "#6699FF"}) 338e41f4b71Sopenharmony_ci } 339e41f4b71Sopenharmony_ci .height('100%') 340e41f4b71Sopenharmony_ci } 341e41f4b71Sopenharmony_ci } 342e41f4b71Sopenharmony_ci ``` 343e41f4b71Sopenharmony_ci  344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci## Component Size 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ciThe size of a child component is not affected by the relative layout rules. If two or more **alignRule** values are set for a child component in one direction, avoid setting the size in this direction. Otherwise, the component size determined by **alignRule** may conflict with the size you set. 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci ```ts 350e41f4b71Sopenharmony_ci @Entry 351e41f4b71Sopenharmony_ci @Component 352e41f4b71Sopenharmony_ci struct Index { 353e41f4b71Sopenharmony_ci build() { 354e41f4b71Sopenharmony_ci Row() { 355e41f4b71Sopenharmony_ci RelativeContainer() { 356e41f4b71Sopenharmony_ci Row(){Text('row1')}.justifyContent(FlexAlign.Center) 357e41f4b71Sopenharmony_ci .width(100).height(100) 358e41f4b71Sopenharmony_ci .backgroundColor("#FF3333") 359e41f4b71Sopenharmony_ci .alignRules({ 360e41f4b71Sopenharmony_ci top: {anchor: "__container__", align: VerticalAlign.Top}, 361e41f4b71Sopenharmony_ci left: {anchor: "__container__", align: HorizontalAlign.Start} 362e41f4b71Sopenharmony_ci }) 363e41f4b71Sopenharmony_ci .id("row1") 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci Row(){Text('row2')}.justifyContent(FlexAlign.Center).width(100) 366e41f4b71Sopenharmony_ci .backgroundColor("#FFCC00") 367e41f4b71Sopenharmony_ci .alignRules({ 368e41f4b71Sopenharmony_ci top: {anchor: "__container__", align: VerticalAlign.Top}, 369e41f4b71Sopenharmony_ci right: {anchor: "__container__", align: HorizontalAlign.End}, 370e41f4b71Sopenharmony_ci bottom: {anchor: "row1", align: VerticalAlign.Center}, 371e41f4b71Sopenharmony_ci }) 372e41f4b71Sopenharmony_ci .id("row2") 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci Row(){Text('row3')}.justifyContent(FlexAlign.Center).height(100) 375e41f4b71Sopenharmony_ci .backgroundColor("#FF6633") 376e41f4b71Sopenharmony_ci .alignRules({ 377e41f4b71Sopenharmony_ci top: {anchor: "row1", align: VerticalAlign.Bottom}, 378e41f4b71Sopenharmony_ci left: {anchor: "row1", align: HorizontalAlign.End}, 379e41f4b71Sopenharmony_ci right: {anchor: "row2", align: HorizontalAlign.Start} 380e41f4b71Sopenharmony_ci }) 381e41f4b71Sopenharmony_ci .id("row3") 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci Row(){Text('row4')}.justifyContent(FlexAlign.Center) 384e41f4b71Sopenharmony_ci .backgroundColor("#FF9966") 385e41f4b71Sopenharmony_ci .alignRules({ 386e41f4b71Sopenharmony_ci top: {anchor: "row3", align: VerticalAlign.Bottom}, 387e41f4b71Sopenharmony_ci bottom: {anchor: "__container__", align: VerticalAlign.Bottom}, 388e41f4b71Sopenharmony_ci left: {anchor: "__container__", align: HorizontalAlign.Start}, 389e41f4b71Sopenharmony_ci right: {anchor: "row1", align: HorizontalAlign.End} 390e41f4b71Sopenharmony_ci }) 391e41f4b71Sopenharmony_ci .id("row4") 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci Row(){Text('row5')}.justifyContent(FlexAlign.Center) 394e41f4b71Sopenharmony_ci .backgroundColor("#FF66FF") 395e41f4b71Sopenharmony_ci .alignRules({ 396e41f4b71Sopenharmony_ci top: {anchor: "row3", align: VerticalAlign.Bottom}, 397e41f4b71Sopenharmony_ci bottom: {anchor: "__container__", align: VerticalAlign.Bottom}, 398e41f4b71Sopenharmony_ci left: {anchor: "row2", align: HorizontalAlign.Start}, 399e41f4b71Sopenharmony_ci right: {anchor: "row2", align: HorizontalAlign.End} 400e41f4b71Sopenharmony_ci }) 401e41f4b71Sopenharmony_ci .id("row5") 402e41f4b71Sopenharmony_ci 403e41f4b71Sopenharmony_ci Row(){Text('row6')}.justifyContent(FlexAlign.Center) 404e41f4b71Sopenharmony_ci .backgroundColor('#ff33ffb5') 405e41f4b71Sopenharmony_ci .alignRules({ 406e41f4b71Sopenharmony_ci top: {anchor: "row3", align: VerticalAlign.Bottom}, 407e41f4b71Sopenharmony_ci bottom: {anchor: "row4", align: VerticalAlign.Bottom}, 408e41f4b71Sopenharmony_ci left: {anchor: "row3", align: HorizontalAlign.Start}, 409e41f4b71Sopenharmony_ci right: {anchor: "row3", align: HorizontalAlign.End} 410e41f4b71Sopenharmony_ci }) 411e41f4b71Sopenharmony_ci .id("row6") 412e41f4b71Sopenharmony_ci .backgroundImagePosition(Alignment.Bottom) 413e41f4b71Sopenharmony_ci .backgroundImageSize(ImageSize.Cover) 414e41f4b71Sopenharmony_ci } 415e41f4b71Sopenharmony_ci .width(300).height(300) 416e41f4b71Sopenharmony_ci .margin({left: 50}) 417e41f4b71Sopenharmony_ci .border({width:2, color: "#6699FF"}) 418e41f4b71Sopenharmony_ci } 419e41f4b71Sopenharmony_ci .height('100%') 420e41f4b71Sopenharmony_ci } 421e41f4b71Sopenharmony_ci } 422e41f4b71Sopenharmony_ci ``` 423e41f4b71Sopenharmony_ci  424