1# Grid Layout 2 3 4The **\<grid-container>** component is the root container of the grid layout. Within the root container, you can use **\<grid-row>** and **\<grid-col>** for the grid layout. For details, see [Grid-container](../reference/arkui-js/js-components-grid-container.md). 5 6 7## Creating a \<grid-container> Component 8 9Create a **\<grid-container>** component in the .hml file under **pages/index** and add a [\<Grid-row>](../reference/arkui-js/js-components-grid-row.md) child component. 10 11 12```html 13<!-- index.hml --> 14<div class="container"> 15 <grid-container id="mygrid" columns="5" gutter="20px" style="background-color: pink;"> 16 <grid-row style="height:100px;justify-content:space-around;width: 80%;background-color: #f67002;margin-left: 17 10%;"></grid-row> 18 <grid-row style="height:300px;justify-content:space-around;background-color: #ffcf00;width: 100%;"></grid-row> 19 <grid-row style="height:150px;justify-content:space-around;background-color: #032cf8;width: 100%;"></grid-row> 20 </grid-container> 21</div> 22``` 23 24 25```css 26/* xxx.css */ 27.container{ 28 flex-direction: column; 29 background-color: #F1F3F5; 30 width: 100%; 31 justify-content: center; 32 align-items: center; 33} 34``` 35 36 37 38> **NOTE** 39> 40> **\<grid-container>** supports only **\<grid-row>** as a child component. 41 42 43## Methods 44 45Touch the **\<grid-container>** component to call the **getColumns**, **getColumnWidth**, and **getGutterWidth** methods to return the number of columns in the grid container, and column width and gutter width of the grid container. Press and hold the component to call the **getSizeType** method to return the size-responsive type of the grid container (**xs**|**sm**|**md**|**lg**). 46 47 48```html 49<!-- index.hml --> 50<div class="container"> 51 <grid-container id="mygrid" columns="6" gutter="20px" style="background-color: pink;padding-top: 100px;" 52 onclick="getColumns" onlongpress="getSizeType"> 53 <grid-row style="height:100px;justify-content:space-around;background-color: #4cedf3;width: 20%;margin-left: 54 40%;"></grid-row> 55 <grid-row style="height:150px;justify-content:space-around;background-color: #4cbff3;width: 50%;margin-left: 56 25%;"></grid-row> 57 <grid-row style="height:200px;justify-content:space-around;background-color: #465ff6;width: 80%;margin-left: 58 10%;"></grid-row> 59 <grid-row style="height:200px;justify-content:space-around;background-color: #5011ec;width: 100%;"></grid-row> 60 </grid-container> 61</div> 62``` 63 64 65```css 66/* xxx.css */ 67.container{ 68 flex-direction: column; 69 background-color: #F1F3F5; 70 width: 100%; 71 height: 100%; 72 justify-content: center; 73 align-items: center; 74} 75``` 76 77 78```js 79// index.js 80import prompt from '@system.prompt'; 81export default { 82 data:{ 83 gutterWidth:'', 84 columnWidth:'', 85 columns:'', 86 }, 87 getColumns(){ 88 this.$element('mygrid').getColumnWidth((result)=>{ 89 this.columnWidth = result; 90 }) 91 this.$element('mygrid').getGutterWidth((result)=>{ 92 this.gutterWidth = result; 93 }) 94 this.$element('mygrid').getColumns((result)=>{ 95 this.columns= result; 96 }) 97 setTimeout(()=>{ 98 prompt.showToast({duration:5000,message:'columnWidth:'+this.columnWidth+',gutterWidth:'+ 99 this.gutterWidth+',getColumns:'+this.columns}) 100 }) 101 }, 102 getSizeType(){ 103 this.$element('mygrid').getSizeType((result)=>{ 104 prompt.showToast({duration:2000,message:'get size type:'+result}) 105 }) 106 }, 107} 108``` 109 110 111 112 113## Adding \<grid-col> 114 115After adding a **\<grid-row>** child component to **\<grid-container>**, add a **\<grid-col>** child component to **\<grid-row>** to form a layout. 116 117 118```html 119<!-- index.hml --> 120<div class="container"> 121 <grid-container id="mygrid" columns="4" gutter="0" style="background-color: pink;" onclick="getColumns" onlongpress="getSizeType"> 122 <grid-row style="height: 100px;justify-content: space-around;background-color: #4cbff3;width: 100%;"> 123 <grid-col span="0"> 124 <div style="align-items: center;justify-content: center;height: 100%;width: 100%;"> 125 <text style="color: dodgerblue;" onclick="getCol">top</text> 126 </div> 127 </grid-col> 128 </grid-row> 129 <grid-row style="height:500px;justify-content:space-around;background-color: #3b55ef;width: 100%;"> 130 <grid-col span="0" style="width: 20%;"> 131 <div style="align-items: center;justify-content: center;height: 100%;width: 100%;"> 132 <text style="color: dodgerblue;">left</text> 133 </div> 134 </grid-col> 135 <grid-col span="0" style="background-color:orange;width: 80%;"> 136 <div style="width: 100%;height: 100%;align-items: center;justify-content: center;"> 137 <text>right</text> 138 </div> 139 </grid-col> 140 </grid-row> 141 <grid-row style="height: 100px;justify-content: space-around;background-color: #4cbff3;width: 100%;"> 142 <grid-col style="background-color:#c075ef;" span="0"> 143 <div style="width: 100%;height: 100%;padding: 20px;align-items: center;justify-content: center;"> 144 <text>bottom</text> 145 </div> 146 </grid-col> 147 </grid-row> 148 </grid-container> 149</div> 150``` 151 152 153```css 154/* xxx.css */ 155.container{ 156 flex-direction: column; 157 background-color: #F1F3F5; 158 width: 100%; 159 height: 100%; 160 justify-content: center; 161 align-items: center; 162} 163text{ 164 color: white; 165 font-size: 40px; 166 167``` 168 169 170 171> **NOTE** 172> 173> **\<grid-row>** supports only **\<grid-col>** as a child component. You can add content only to **\<grid-col>**. 174 175 176## Example Scenario 177 178In this example, the content in the list is output cyclically to create a grid layout. When the user pulls down the screen, the **refresh** method is triggered. In this case, a piece of data is added to the list and **setTimeout** is set to refresh the request data. 179 180 181```html 182<!-- index.hml --> 183<div class="container"> 184 <refresh refreshing="{{fresh}}" onrefresh="refresh"> 185 <grid-container id="mygrid" gutter="20" style="margin: 10px;"> 186 <grid-row style="height:200px;width: 100%;background-color: #e7e7e2;margin-top: 50px; padding: 0px 20px;border-radius: 15px;" for="item in list"> 187 <grid-col span="0" style="width: 40%;"> 188 <div style="align-items: center;justify-content: center"> 189 <image src="{{item.src}}" style="object-fit: contain;border-radius: 30px;"></image> 190 </div> 191 </grid-col> 192 <grid-col span="0" style="width: 60%;"> 193 <div style="align-items: center;justify-content: center;width: 100%;height: 100%;text-align: center;"> 194 <text>image{{item.id}}</text> 195 </div> 196 </grid-col> 197 </grid-row> 198 </grid-container> 199 </refresh> 200</div> 201``` 202 203 204```css 205/* xxx.css */ 206.container{ 207 flex-direction: column; 208 background-color: #F1F3F5; 209 width: 100%; 210 height: 100%; 211} 212text{ 213 color: #0a0aef; 214 font-size: 60px; 215} 216``` 217 218 219```js 220// index.js 221import prompt from '@system.prompt'; 222export default { 223 data:{ 224 list:[ 225 {src:'common/images/1.png',id:'1'}, 226 {src:'common/images/2.png',id:'2'}, 227 {src:'common/images/3.png',id:'3'} 228 ], 229 fresh:false 230 }, 231 refresh(e) { 232 prompt.showToast({ 233 message: 'refreshing' 234 }) 235 var that = this; 236 that.fresh = e.refreshing; 237 setTimeout(function () { 238 that.fresh = false; 239 that.list.unshift({src: 'common/images/4.png',id:'4'}); 240 prompt.showToast({ 241 message: 'succeed' 242 }) 243 }, 2000) 244 } 245} 246``` 247 248 249