1# Responsive Grid Layout (GridRow/GridCol)
2
3
4## Overview
5
6As an auxiliary positioning tool, the responsive grid layout is handy in UI design on mobile devices. It exhibits the following advantages:
7
81. Provides rules for layout design and resolves issues of dynamic layout across devices with different sizes. By dividing a page into equal-width columns and rows, you can easily locate and typeset page elements.
9
102. Provides a unified positioning method for the system to ensure layout consistency across layouts on different devices. This can reduce the complexity of design and development and improve work efficiency.
11
123. Provides a flexible spacing adjustment method for applications to accommodate special layout requirements. You can adjust the spacing between columns and between rows to control the typesetting of the entire page.
13
144. Completes the wrapping and adaptation automatically when overflow occurs. When the number of page elements exceeds the capacity of a row or column, they automatically wrap to a new row or column and adapt the typesetting to different devices.
15
16The [GridRow](../reference/apis-arkui/arkui-ts/ts-container-gridrow.md) component is a responsive grid container and must have [GridCol](../reference/apis-arkui/arkui-ts/ts-container-gridcol.md) as its child component.
17
18
19## GridRow
20
21
22### Grid Breakpoints
23
24The grid system defines breakpoints, which are screen width types in effect, based on the horizontal width ([screen density pixels](../reference/apis-arkui/arkui-ts/ts-pixel-units.md), in vp) of the screens. You can use the breakpoints to meet specific layout requirements. You can use the breakpoints to meet specific layout requirements.
25
26By default, the grid system provides four breakpoints: xs, sm, md, and lg.
27
28| Breakpoint| Value Range (vp)       | Device Description     |
29| ---- | --------------- | --------- |
30| xs   | [0, 320)  | Minimum-width device.|
31| sm   | [320, 520) | Small-width device. |
32| md   | [520, 840) | Medium-width device.|
33| lg   | [840, +∞)  | Large-width device. |
34
35In the **GridRow** component, you can use **breakpoints** to customize the value range of breakpoints. A maximum of six breakpoints are supported. In addition to the four default breakpoints, you can also enable the xl and xxl breakpoints for your application window layout.
36
37| Breakpoint| Device Description     |
38| ---- | --------- |
39| xs   | Minimum-width device.|
40| sm   | Small-width device. |
41| md   | Medium-width device.|
42| lg   | Large-width device. |
43| xl   | Extra-large-width device.|
44| xxl  | Extra-extra-large-width device.|
45
46- Set **breakpoints** with a monotonically increasing array based on the use case. As **breakpoints** supports a maximum of six breakpoints, the maximum length of the monotonically increasing array is 5.
47
48
49    ```ts
50  breakpoints: {value: ['100vp', '200vp']}
51    ```
52
53   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.
54
55
56    ```ts
57  breakpoints: {value: ['320vp', '520vp', '840vp', '1080vp']}
58    ```
59
60  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–1080 vp, the breakpoint is lg. If the value is greater than 1080 vp, the breakpoint is xl.
61
62- The grid system implements breakpoints by listening for the changes in the window or container size, and sets the breakpoint references through **reference**. Since the application may be displayed in non-full-screen mode, it is better to design the breakpoints with the application window width as the reference.
63
64In the following example, the default number of grid columns is 12. Breakpoints are used to divide the application window width into six ranges. In different ranges, the **GridCol** child component occupies a different number of columns.
65
66
67```ts
68@State bgColors: ResourceColor[] =
69    ['rgb(213,213,213)', 'rgb(150,150,150)', 'rgb(0,74,175)', 'rgb(39,135,217)', 'rgb(61,157,180)', 'rgb(23,169,141)',
70      'rgb(255,192,0)', 'rgb(170,10,33)'];
71...
72GridRow({
73  breakpoints: {
74    value: ['200vp', '300vp', '400vp', '500vp', '600vp'],
75    reference: BreakpointsReference.WindowSize
76  }
77}) {
78   ForEach(this.bgColors, (color:ResourceColor, index?:number|undefined) => {
79     GridCol({
80       span: {
81         xs: 2, // The GridCol component occupies two grid columns on the minimum-width device.
82         sm: 3, // The GridCol component occupies three grid columns on the small-width device.
83         md: 4, // The GridCol component occupies four grid columns on the medium-width device.
84         lg: 6, // The GridCol component occupies six grid columns on the large-width device.
85         xl: 8, // The GridCol component occupies eight grid columns on the extra-large-width device.
86         xxl: 12 // The GridCol component occupies 12 grid columns on the extra-extra-large-width device.
87       }
88     }) {
89       Row() {
90         Text(`${index}`)
91       }.width("100%").height('50vp')
92     }.backgroundColor(color)
93   })
94}
95```
96
97![en-us_image_0000001511421272](figures/en-us_image_0000001511421272.gif)
98
99
100### Columns
101
102In the **GridRow**, **columns** is used to set the total number of columns in the responsive grid layout.
103
104- The default value of **columns** is 12. If **columns** is not set, the responsive grid layout is divided into 12 columns at any breakpoint.
105
106
107    ```ts
108  @State bgColors: ResourceColor[] =
109      ['rgb(213,213,213)', 'rgb(150,150,150)', 'rgb(0,74,175)', 'rgb(39,135,217)', 'rgb(61,157,180)', 'rgb(23,169,141)',
110        'rgb(255,192,0)', 'rgb(170,10,33)', 'rgb(213,213,213)', 'rgb(150,150,150)', 'rgb(0,74,175)', 'rgb(39,135,217)'];
111    ...
112    GridRow() {
113      ForEach(this.bgColors, (item:ResourceColor, index?:number|undefined) => {
114        GridCol() {
115          Row() {
116              Text(`${index}`)
117          }.width('100%').height('50')
118        }.backgroundColor(item)
119      })
120    }
121    ```
122
123    ![en-us_image_0000001563060709](figures/en-us_image_0000001563060709.png)
124
125- When **columns** is set to a number, the responsive 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.
126
127  ```ts
128  class CurrTmp{
129    currentBp: string = 'unknown';
130    set(val:string){
131      this.currentBp = val
132    }
133  }
134  let BorderWH:Record<string,Color|number> = { 'color': Color.Blue, 'width': 2 }
135  @State bgColors: ResourceColor[] =
136      ['rgb(213,213,213)', 'rgb(150,150,150)', 'rgb(0,74,175)', 'rgb(39,135,217)', 'rgb(61,157,180)', 'rgb(23,169,141)',
137        'rgb(255,192,0)', 'rgb(170,10,33)'];
138  @State currentBp: string = 'unknown';
139  ...
140  Row() {
141    GridRow({ columns: 4 }) {
142      ForEach(this.bgColors, (item: ResourceColor, index?:number|undefined) => {
143        GridCol() {
144          Row() {
145            Text(`${index}`)
146          }.width('100%').height('50')
147        }.backgroundColor(item)
148      })
149    }
150    .width('100%').height('100%')
151    .onBreakpointChange((breakpoint:string) => {
152      let CurrSet:CurrTmp = new CurrTmp()
153      CurrSet.set(breakpoint)
154    })
155  }
156  .height(160)
157  .border(BorderWH)
158  .width('90%')
159
160  Row() {
161    GridRow({ columns: 8 }) {
162      ForEach(this.bgColors, (item: ResourceColor, index?:number|undefined) => {
163          GridCol() {
164            Row() {
165              Text(`${index}`)
166            }.width('100%').height('50')
167          }.backgroundColor(item)
168      })
169    }
170    .width('100%').height('100%')
171    .onBreakpointChange((breakpoint:string) => {
172      let CurrSet:CurrTmp = new CurrTmp()
173      CurrSet.set(breakpoint)
174    })
175  }
176  .height(160)
177  .border(BorderWH)
178  .width('90%')
179  ```
180
181    ![en-us_image_0000001511421268](figures/en-us_image_0000001511421268.png)
182
183- 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).
184
185  ```ts
186  @State bgColors: ResourceColor[] =
187      ['rgb(213,213,213)', 'rgb(150,150,150)', 'rgb(0,74,175)', 'rgb(39,135,217)', 'rgb(61,157,180)', 'rgb(23,169,141)',
188        'rgb(255,192,0)', 'rgb(170,10,33)'];
189  GridRow({ columns: { sm: 4, md: 8 }, breakpoints: { value: ['200vp', '300vp', '400vp', '500vp', '600vp'] } }) {
190    ForEach(this.bgColors, (item: ResourceColor, index?:number|undefined) => {
191      GridCol() {
192        Row() {
193          Text(`${index}`)
194        }.width('100%').height('50')
195      }.backgroundColor(item)
196    })
197  }
198  ```
199
200    ![en-us_image_0000001563060689](figures/en-us_image_0000001563060689.gif)
201
202If **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.
203
204
205### Alignment
206
207In the responsive grid layout, you can set the **direction** attribute of **GridRow** to define the direction in which child components are arranged. The options are **GridRowDirection.Row** (from left to right) or **GridRowDirection.RowReverse** (from right to left). An appropriate **direction** value can make the page layout more flexible and meet the design requirements.
208
209- When child components are arranged from left to right (default):
210
211
212    ```ts
213  GridRow({ direction: GridRowDirection.Row }){}
214    ```
215
216    ![en-us_image_0000001511740488](figures/en-us_image_0000001511740488.png)
217
218- When child components are arranged from right to left (default):
219
220
221    ```ts
222  GridRow({ direction: GridRowDirection.RowReverse }){}
223    ```
224
225    ![en-us_image_0000001562940517](figures/en-us_image_0000001562940517.png)
226
227
228### Gutters
229
230In the **GridRow** component, **gutter** is used to set the spacing between adjacent child components in the horizontal and vertical directions.
231
232- 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 is set to **10**.
233
234
235    ```ts
236  GridRow({ gutter: 10 }){}
237    ```
238
239    ![en-us_image_0000001511740476](figures/en-us_image_0000001511740476.png)
240
241- 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.
242
243
244    ```ts
245  GridRow({ gutter: { x: 20, y: 50 } }){}
246    ```
247
248    ![en-us_image_0000001511900456](figures/en-us_image_0000001511900456.png)
249
250
251## GridCol
252
253The **\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.
254
255- Setting **span**
256
257
258    ```ts
259  let Gspan:Record<string,number> = { 'xs': 1, 'sm': 2, 'md': 3, 'lg': 4 }
260  GridCol({ span: 2 }){}
261  GridCol({ span: { xs: 1, sm: 2, md: 3, lg: 4 } }){}
262  GridCol(){}.span(2)
263  GridCol(){}.span(Gspan)
264    ```
265
266- Setting **offset**
267
268
269    ```ts
270  let Goffset:Record<string,number> = { 'xs': 1, 'sm': 2, 'md': 3, 'lg': 4 }
271  GridCol({ offset: 2 }){}
272  GridCol({ offset: { xs: 2, sm: 2, md: 2, lg: 2 } }){}
273  GridCol(){}.offset(Goffset)
274    ```
275
276- Setting **order**
277
278
279    ```ts
280  let Gorder:Record<string,number> = { 'xs': 1, 'sm': 2, 'md': 3, 'lg': 4 }
281  GridCol({ order: 2 }){}
282  GridCol({ order: { xs: 1, sm: 2, md: 3, lg: 4 } }){}
283  GridCol(){}.order(2)
284  GridCol(){}.order(Gorder)
285    ```
286
287
288### span
289
290Sets the number of columns occupied by a child component in the grid layout, which determines the child component width. The default value is **1**.
291
292- When the value type is number, the number of columns occupied by the child component is the same across screen sizes.
293
294
295    ```ts
296  @State bgColors: ResourceColor[] =
297      ['rgb(213,213,213)', 'rgb(150,150,150)', 'rgb(0,74,175)', 'rgb(39,135,217)', 'rgb(61,157,180)', 'rgb(23,169,141)',
298        'rgb(255,192,0)', 'rgb(170,10,33)'];
299    ...
300    GridRow({ columns: 8 }) {
301      ForEach(this.bgColors, (color:ResourceColor, index?:number|undefined) => {
302        GridCol({ span: 2 }) {
303          Row() {
304            Text(`${index}`)
305          }.width('100%').height('50vp')
306        }
307        .backgroundColor(color)
308      })
309    }
310    ```
311
312    ![en-us_image_0000001511421264](figures/en-us_image_0000001511421264.png)
313
314- When the value type is **GridColColumnOption**, you can assign values specific to the screen size (xs, sm, md, lg, xl, xxl).
315
316
317    ```ts
318  @State bgColors: ResourceColor[] =
319      ['rgb(213,213,213)', 'rgb(150,150,150)', 'rgb(0,74,175)', 'rgb(39,135,217)', 'rgb(61,157,180)', 'rgb(23,169,141)',
320        'rgb(255,192,0)', 'rgb(170,10,33)'];
321    ...
322    GridRow({ columns: 8 }) {
323      ForEach(this.bgColors, (color:ResourceColor, index?:number|undefined) => {
324        GridCol({ span: { xs: 1, sm: 2, md: 3, lg: 4 } }) {
325          Row() {
326            Text(`${index}`)
327          }.width('100%').height('50vp')
328        }
329        .backgroundColor(color)
330      })
331    }
332    ```
333
334    ![en-us_image_0000001511740492](figures/en-us_image_0000001511740492.gif)
335
336
337### offset
338
339Sets the column offset of a child component relative to the previous child component. The default value is **0**.
340
341- When the value type is number, the column offset of the child component is the same across screen sizes.
342
343
344    ```ts
345  @State bgColors: ResourceColor[] =
346      ['rgb(213,213,213)', 'rgb(150,150,150)', 'rgb(0,74,175)', 'rgb(39,135,217)', 'rgb(61,157,180)', 'rgb(23,169,141)',
347        'rgb(255,192,0)', 'rgb(170,10,33)'];
348    ...
349    GridRow() {
350      ForEach(this.bgColors, (color:ResourceColor, index?:number|undefined) => {
351        GridCol({ offset: 2 }) {
352          Row() {
353            Text('' + index)
354          }.width('100%').height('50vp')
355        }
356        .backgroundColor(color)
357      })
358    }
359    ```
360
361    ![en-us_image_0000001563060705](figures/en-us_image_0000001563060705.png)
362
363  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.
364
365- When the value type is **GridColColumnOption**, you can assign values specific to the screen size (xs, sm, md, lg, xl, xxl).
366
367
368    ```ts
369  @State bgColors: ResourceColor[] =
370      ['rgb(213,213,213)', 'rgb(150,150,150)', 'rgb(0,74,175)', 'rgb(39,135,217)', 'rgb(61,157,180)', 'rgb(23,169,141)',
371        'rgb(255,192,0)', 'rgb(170,10,33)'];
372    ...
373
374    GridRow() {
375      ForEach(this.bgColors, (color:ResourceColor, index?:number|undefined) => {
376        GridCol({ offset: { xs: 1, sm: 2, md: 3, lg: 4 } }) {
377          Row() {
378            Text('' + index)
379          }.width('100%').height('50vp')
380        }
381        .backgroundColor(color)
382      })
383    }
384    ```
385
386    ![en-us_image_0000001562700433](figures/en-us_image_0000001562700433.gif)
387
388
389### order
390
391Sets 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 component with a smaller **order** value is placed before the one with a larger **order** value.
392
393If **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.
394
395- When the value type is number, child components are sorted in the same order across screen sizes.
396
397
398    ```ts
399  GridRow() {
400    GridCol({ order: 4 }) {
401      Row() {
402        Text('1')
403      }.width('100%').height('50vp')
404    }.backgroundColor('rgb(213,213,213)')
405    GridCol({ order: 3 }) {
406      Row() {
407        Text('2')
408      }.width('100%').height('50vp')
409    }.backgroundColor('rgb(150,150,150)')
410    GridCol({ order: 2 }) {
411      Row() {
412        Text('3')
413      }.width('100%').height('50vp')
414    }.backgroundColor('rgb(0,74,175)')
415    GridCol({ order: 1 }) {
416      Row() {
417        Text('4')
418      }.width('100%').height('50vp')
419    }.backgroundColor('rgb(39,135,217)')
420  }
421    ```
422
423    ![en-us_image_0000001511580892](figures/en-us_image_0000001511580892.png)
424
425- When the value type is **GridColColumnOption**, you can assign values specific to the screen size (xs, sm, md, lg, xl, xxl). You can set 1234 for xs, 2341 for sm, 3412 for md, and 2431 for lg.
426
427
428    ```ts
429    GridRow() {
430      GridCol({ order: { xs:1, sm:5, md:3, lg:7}}) {
431        Row() {
432          Text('1')
433        }.width('100%').height('50vp')
434      }.backgroundColor(Color.Red)
435      GridCol({ order: { xs:2, sm:2, md:6, lg:1} }) {
436        Row() {
437          Text('2')
438        }.width('100%').height('50vp')
439      }.backgroundColor(Color.Orange)
440      GridCol({ order: { xs:3, sm:3, md:1, lg:6} }) {
441        Row() {
442          Text('3')
443        }.width('100%').height('50vp')
444      }.backgroundColor(Color.Yellow)
445      GridCol({ order: { xs:4, sm:4, md:2, lg:5} }) {
446        Row() {
447          Text('4')
448        }.width('100%').height('50vp')
449      }.backgroundColor(Color.Green)
450    }
451    ```
452
453    ![en-us_image_0000001511900444](figures/en-us_image_0000001511900444.gif)
454
455
456## Nesting of Responsive Grid Components
457
458Responsive grid components can be contained in other responsive grid components.
459
460In the following example, the responsive grid divides the entire space into 12 parts. At the first layer, **\GridCol** is nested in **GridRow**, and the space is divided into the large area in the center and the footer area. At the second layer, **\GridCol** is nested in **GridRow**, and the space is divided into the left and right areas. The child components take up the space allocated by the parent component at the upper layer. In this example, the pink area is made up of 12 columns of the screen space, and the green and blue areas take up the 12 columns of the parent component proportionally.
461
462```ts
463@Entry
464@Component
465struct GridRowExample {
466  build() {
467    GridRow() {
468      GridCol({ span: { sm: 12 } }) {
469        GridRow() {
470          GridCol({ span: { sm: 2 } }) {
471            Row() {
472              Text('left').fontSize(24)
473            }
474            .justifyContent(FlexAlign.Center)
475            .height('90%')
476          }.backgroundColor('#ff41dbaa')
477
478          GridCol({ span: { sm: 10 } }) {
479            Row() {
480              Text('right').fontSize(24)
481            }
482            .justifyContent(FlexAlign.Center)
483            .height('90%')
484          }.backgroundColor('#ff4168db')
485        }
486        .backgroundColor('#19000000')
487      }
488
489      GridCol({ span: { sm: 12 } }) {
490        Row() {
491          Text('footer').width('100%').textAlign(TextAlign.Center)
492        }.width('100%').height('10%').backgroundColor(Color.Pink)
493      }
494    }.width('100%').height(300)
495  }
496}
497```
498
499
500![en-us_image_0000001563060697](figures/en-us_image_0000001563060697.png)
501
502
503To sum up, the responsive grid components are powerful tools with a wide range of customization capabilities. With the required attributes set at different breakpoints, such as **Columns**, **Margin**, **Gutter**, and **span**, the layout is created automatically. You do not need to pay attention to the specific device type and device state (such as landscape and portrait).
504