1# Grid Layout 2 3As a tool to provide layout auxiliary lines, the grid system is handy in graphic and website design. When employed in the UI design of mobile devices, the grid system exhibits the following advantages: 4 51. Provides rules for layout design and resolves issues of dynamic layout across devices with different sizes. 62. Provides a unified positioning method for the system to ensure layout consistency across layouts on different devices. 73. Provides a flexible spacing adjustment method for applications to keep up with layout in special scenarios. 8 9To implement a grid layout, **[GridRow](../reference/arkui-ts/ts-container-gridrow.md)** and **[GridCol](../reference/arkui-ts/ts-container-gridcol.md)** are prioritized 10over the obsolete **[GridContainer](../reference/arkui-ts/ts-container-gridcontainer.md)**, because the former provides more flexibility and functionality. **GridRow** is a grid container item and must be used only with **GridCol** in the grid container. 11 12 13## GridRow 14 15 16The grid container works in terms of **columns**, **gutter**, **direction**, and **breakpoints**. 17- **columns**: total number of columns in the grid container. This attribute is the main tool for placing items in the grid layout. 18- **gutter**: spacing between elements. This attribute determines how close the content tracks are with each other. 19- **direction**: alignment of child components in the grid container. 20- **breakpoints**: ranges of application window widths, which are set based on the device screen widths. You can use the breakpoints to meet specific layout requirements. 21 22 23After you set the breakpoints, the layout listens for changes in the application window size, determines which breakpoint range the application window is located, and adjusts to match the window size accordingly. 24 25### Grid Breakpoints 26 27The grid system defines breakpoints, which are screen width types in effect, based on the horizontal width (screen density pixels, in vp) of the screens. You can use the breakpoints to meet specific layout requirements. 28By default, the grid system provides four breakpoints: xs, sm, md, and lg. 29 30| Breakpoint | Value Range (vp)| 31| --------| ------ | 32| xs | [0, 320) | 33| sm | [320, 520) | 34| md | [520, 840) | 35| lg | [840, +∞) | 36 37In the **\<GridRow>** component, you can use **breakpoints** to customize the value range of breakpoints. A maximum of six breakpoints are supported. 38In addition to the four default breakpoints, you can also enable the xl and xxl breakpoints for your application window layout. 39 40| Breakpoint| Device Description | 41| ----- | ---------------------------------------- | 42| xs | Device of the minimum size. | 43| sm | Small-sized device. | 44| md | Medium-sized device. | 45| lg | Large-sized device. | 46| xl | Extra-large-sized device. | 47| xxl | Ultra-large-sized device. | 48 49- Set **breakpoints** with a monotonically increasing array based on the use case. Because **breakpoints** supports a maximum of six breakpoints, the maximum length of the monotonically increasing array is 5. 50 51 ```ts 52 breakpoints: {value: ["100vp", "200vp"]} 53 ``` 54 55 Enables three breakpoints: xs, sm, and md. If the value is less than 100 vp, the breakpoint is xs. If the value is 100–200 vp, the breakpoint is sm. If the value is greater than 200 vp, the breakpoint is md. 56 57 ```ts 58 breakpoints: {value: ["320vp", "520vp", "840vp", "1080vp"]} 59 ``` 60 61 Enables five breakpoints: xs, sm, md, lg, and xl. If the value is less than 320 vp, the breakpoint is xs. If the value is 320–520 vp, the breakpoint is sm. If the value is 520–840 vp, the breakpoint is md. If the value is 840–1080vp, the breakpoint is lg. If the value is greater than 1080 vp, the breakpoint is xl. 62 63 64- The grid system implements breakpoints by listening for the changes in the window or container size, and sets the breakpoint references through **reference**. Considering that the application may be displayed in non-full-screen mode, design the breakpoints with the application window width as the reference. 65 66In the following example, the default number of columns of a grid is 12. Breakpoints are used to divide the application window width into six ranges, where different grid items occupy a different number of columns. The following figure shows the effect. 67 ```ts 68GridRow({ 69 breakpoints: { 70 value: ['200vp', '300vp', '400vp', '500vp', '600vp'], 71 reference: BreakpointsReference.WindowSize 72 } 73}) { 74 ForEach(this.bgColors, (color, index) => { 75 GridCol({ 76 span: { 77 xs: 2, 78 sm: 3, 79 md: 4, 80 lg: 6, 81 xl: 8, 82 xxl: 12 83 } 84 }) { 85 Row() { 86 Text(`${index}`) 87 }.width("100%").height("50vp") 88 }.backgroundColor(color) 89 }) 90} 91 ``` 92 93 94 95 96 97### Columns 98 99In the **\<GridRow>**, **columns** is used to set the total number of columns in the grid layout. 100 101- The default value of **columns** is 12. If **columns** is not set, the grid layout is divided into 12 columns at any breakpoint. 102 ```ts 103 GridRow() { 104 ForEach(this.bgColors, (item, index) => { 105 GridCol() { 106 Row() { 107 Text(`${index + 1}`) 108 }.width("100%").height("50") 109 }.backgroundColor(item) 110 }) 111 } 112 ``` 113 114  115 116- When **columns** is set to a number, the grid layout is divided into the specified number of columns regardless of the screen size. The following example sets the number of grid layout columns to 4 and 8 in sequence, where a child component occupies one column by default. 117 118 ```ts 119 Row() { 120 GridRow({ columns: 4 }) { 121 ForEach(this.bgColors, (item, index) => { 122 GridCol() { 123 Row() { 124 Text(`${index + 1}`) 125 }.width("100%").height("50") 126 }.backgroundColor(item) 127 }) 128 } 129 .width("100%").height("100%") 130 .onBreakpointChange((breakpoint) => { 131 this.currentBp = breakpoint 132 }) 133 } 134 .height(160) 135 .border({ color: Color.Blue, width: 2 }) 136 .width('90%') 137 138 Row() { 139 GridRow({ columns: 8 }) { 140 ForEach(this.bgColors, (item, index) => { 141 GridCol() { 142 Row() { 143 Text(`${index + 1}`) 144 }.width("100%").height("50") 145 }.backgroundColor(item) 146 }) 147 } 148 .width("100%").height("100%") 149 .onBreakpointChange((breakpoint) => { 150 this.currentBp = breakpoint 151 }) 152 } 153 .height(160) 154 .border({ color: Color.Blue, width: 2 }) 155 .width('90%') 156 ``` 157 158  159 160 161- When **columns** is set to a value of the **GridRowColumnOption** type, you can assign values specific to the screen size (xs, sm, md, lg, xl, xxl). 162 163 ```ts 164 GridRow({ columns: { sm: 4, md: 8 }, breakpoints: { value: ['200vp', '300vp', '400vp', '500vp', '600vp'] } }) { 165 ForEach(this.bgColors, (item, index) => { 166 GridCol() { 167 Row() { 168 Text(`${index + 1}`) 169 }.width("100%").height("50") 170 }.backgroundColor(item) 171 }) 172 } 173 ``` 174  175 176 As shown above, if **columns** is only set for the sm and md screen size types, screen sizes smaller than sm use the default value **12**, and screen sizes larger than md (lg, xl, and xxl) use the value of **columns** of the md type. 177 178### Gutters 179 180In the **\<GridRow>**, **gutter** is used to set the spacing between adjacent child components in the horizontal and vertical directions. 181 182- When **gutter** is set to a number, the number applies to both the horizontal and vertical directions. In the following example, the horizontal and vertical spacing between adjacent child components are set to **10**. 183 184 ```ts 185 GridRow({ gutter: 10 }){} 186 ``` 187 188  189 190 191 192- When **gutter** is set to a value of the **GutterOption** type, the **x** attribute of the value indicates the horizontal gutter, and the **y** attribute indicates the vertical gutter. 193 194 ```ts 195 GridRow({ gutter: { x: 20, y: 50 } }){} 196 ``` 197 198  199 200 201 202### Alignment 203 204In the **\<GridRow>**, **direction** is used to set the alignment of child components in the grid container. 205 206- Child components are arranged from left to right by default. 207 208 ```ts 209 GridRow({ direction: GridRowDirection.Row }){} 210 ``` 211  212 213- Child components are arranged from right to left. 214 215 ```ts 216 GridRow({ direction: GridRowDirection.RowReverse }){} 217 ``` 218 219  220 221 222 223## GridCol 224 225The **\<GridCol>** component is a child component of the **\<GridRow>** component. You can set the **span**, **offset**, and **order** attributes of this component by passing parameters or using setters. 226 227- Setting **span** 228 229 ```ts 230 GridCol({ span: 2 }){} 231 GridCol({ span: { xs: 1, sm: 2, md: 3, lg: 4 } }){} 232 GridCol(){}.span(2) 233 GridCol(){}.span({ xs: 1, sm: 2, md: 3, lg: 4 }) 234 ``` 235 236- Setting **offset** 237 238 ```ts 239 GridCol({ offset: 2 }){} 240 GridCol({ offset: { xs: 2, sm: 2, md: 2, lg: 2 } }){} 241 GridCol(){}.offset(2) 242 GridCol(){}.offset({ xs: 1, sm: 2, md: 3, lg: 4 }) 243 ``` 244 245- Setting **order** 246 247 ```ts 248 GridCol({ order: 2 }){} 249 GridCol({ order: { xs: 1, sm: 2, md: 3, lg: 4 } }){} 250 GridCol(){}.order(2) 251 GridCol(){}.order({ xs: 1, sm: 2, md: 3, lg: 4 }) 252 ``` 253 254 The following describes how to set the attributes by passing parameters. 255 256### span 257 258Sets the number of columns occupied by a child component in the grid layout, which determines the child component width. The default value is **1**. 259 260- When the value type is number, the number of columns occupied by the child component is the same across screen sizes. 261 262 ```ts 263 GridRow({ columns: 8 }) { 264 ForEach(this.bgColors, (color, index) => { 265 GridCol({ span: 2 }) { 266 Row() { 267 Text(`${index}`) 268 }.width("100%").height("50vp") 269 } 270 .backgroundColor(color) 271 }) 272 } 273 ``` 274 275  276 277- When the value type is **GridColColumnOption**, you can assign values specific to the screen size (xs, sm, md, lg, xl, xxl). 278 279 ```ts 280 GridRow({ columns: 8 }) { 281 ForEach(this.bgColors, (color, index) => { 282 GridCol({ span: { xs: 1, sm: 2, md: 3, lg: 4 } }) { 283 Row() { 284 Text(`${index}`) 285 }.width("100%").height("50vp") 286 } 287 .backgroundColor(color) 288 }) 289 } 290 ``` 291 292  293 294### offset 295 296Sets the column offset of a child component relative to the previous child component. The default value is **0**. 297- When the value type is number, the column offset of the child component is the same across screen sizes. 298 299 ```ts 300 GridRow() { 301 ForEach(this.bgColors, (color, index) => { 302 GridCol({ offset: 2 }) { 303 Row() { 304 Text("" + index) 305 }.width("100%").height("50vp") 306 } 307 .backgroundColor(color) 308 }) 309 } 310 ``` 311 312  313 314 By default, a grid is divided into 12 columns and each child component occupies one column with an offset of two columns. Each row holds four child components, with three columns per child component plus the gutter. 315 316 317- When the value type is **GridColColumnOption**, you can assign values specific to the screen size (xs, sm, md, lg, xl, xxl). 318 319 ```ts 320 GridRow() { 321 ForEach(this.bgColors, (color, index) => { 322 GridCol({ offset: { xs: 1, sm: 2, md: 3, lg: 4 } }) { 323 Row() { 324 Text("" + index) 325 }.width("100%").height("50vp") 326 } 327 .backgroundColor(color) 328 }) 329 } 330 ``` 331 332  333 334### order 335 336Sets the sequence number of a child component in the grid layout. If a child component shares an **order** value with another child component or does not have **order** set, it is displayed based on its code sequence number. A child components with a smaller **order** value is placed before the one with a larger **order** value. 337If **order** is not set for all child components, those that have **order** set are displayed after those that do not have **order** set and are sorted in ascending order based on the value. 338 339 340- When the value type is number, child components are sorted in the same order across screen sizes. 341 342 ```ts 343 GridRow() { 344 GridCol({ order: 5 }) { 345 Row() { 346 Text("1") 347 }.width("100%").height("50vp") 348 }.backgroundColor(Color.Red) 349 GridCol({ order: 4 }) { 350 Row() { 351 Text("2") 352 }.width("100%").height("50vp") 353 }.backgroundColor(Color.Orange) 354 GridCol({ order: 3 }) { 355 Row() { 356 Text("3") 357 }.width("100%").height("50vp") 358 }.backgroundColor(Color.Yellow) 359 GridCol({ order: 2 }) { 360 Row() { 361 Text("4") 362 }.width("100%").height("50vp") 363 }.backgroundColor(Color.Green) 364 } 365 ``` 366 367  368- When the value type is **GridColColumnOption**, you can assign values specific to the screen size (xs, sm, md, lg, xl, xxl). 369 370 ```ts 371 GridRow() { 372 GridCol({ order: { xs:1, sm:5, md:3, lg:7}}) { 373 Row() { 374 Text("1") 375 }.width("100%").height("50vp") 376 }.backgroundColor(Color.Red) 377 GridCol({ order: { xs:2, sm:2, md:6, lg:1} }) { 378 Row() { 379 Text("2") 380 }.width("100%").height("50vp") 381 }.backgroundColor(Color.Orange) 382 GridCol({ order: { xs:3, sm:3, md:1, lg:6} }) { 383 Row() { 384 Text("3") 385 }.width("100%").height("50vp") 386 }.backgroundColor(Color.Yellow) 387 GridCol({ order: { xs:4, sm:4, md:2, lg:5} }) { 388 Row() { 389 Text("4") 390 }.width("100%").height("50vp") 391 }.backgroundColor(Color.Green) 392 } 393 ``` 394 395  396