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" 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 margin-top: 500px; 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" 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 margin-top: 400px; 71 justify-content: center; 72 align-items: center; 73} 74``` 75 76 77```js 78// index.js 79import promptAction from '@ohos.promptAction'; 80export default { 81 data:{ 82 gutterWidth:'', 83 columnWidth:'', 84 columns:'', 85 }, 86 getColumns(){ 87 this.$element('mygrid').getColumnWidth((result)=>{ 88 this.columnWidth = result; 89 }) 90 this.$element('mygrid').getGutterWidth((result)=>{ 91 this.gutterWidth = result; 92 }) 93 this.$element('mygrid').getColumns((result)=>{ 94 this.columns= result; 95 }) 96 setTimeout(()=>{ 97 promptAction.showToast({duration:5000,message:'columnWidth:'+this.columnWidth+',gutterWidth:'+ 98 this.gutterWidth+',getColumns:'+this.columns}) 99 }) 100 }, 101 getSizeType(){ 102 this.$element('mygrid').getSizeType((result)=>{ 103 promptAction.showToast({duration:2000,message:'get size type:'+result}) 104 }) 105 }, 106} 107``` 108 109 110 111 112## Adding \<grid-col> 113 114After adding a **\<grid-row>** child component to **\<grid-container>**, add a **\<grid-col>** child component to **\<grid-row>** to form a layout. 115 116 117```html 118<!-- index.hml --> 119<div class="container"> 120 <grid-container id="mygrid" columns="4" gutter="0" style="background-color: pink;" onclick="getColumns" onlongpress="getSizeType"> 121 <grid-row style="height: 100px;justify-content: space-around;background-color: #4cbff3;width: 100%;"> 122 <grid-col span="0"> 123 <div style="align-items: center;justify-content: center;height: 100%;width: 100%;"> 124 <text style="color: dodgerblue;" onclick="getCol">top</text> 125 </div> 126 </grid-col> 127 </grid-row> 128 <grid-row style="height:500px;justify-content:space-around;background-color: #3b55ef;width: 100%;"> 129 <grid-col span="0" style="width: 20%;"> 130 <div style="align-items: center;justify-content: center;height: 100%;width: 100%;"> 131 <text style="color: dodgerblue;">left</text> 132 </div> 133 </grid-col> 134 <grid-col span="0" style="background-color:orange;width: 80%;"> 135 <div style="width: 100%;height: 100%;align-items: center;justify-content: center;"> 136 <text>right</text> 137 </div> 138 </grid-col> 139 </grid-row> 140 <grid-row style="height: 100px;justify-content: space-around;background-color: #4cbff3;width: 100%;"> 141 <grid-col style="background-color:#c075ef;" span="0"> 142 <div style="width: 100%;height: 100%;padding: 20px;align-items: center;justify-content: center;"> 143 <text>bottom</text> 144 </div> 145 </grid-col> 146 </grid-row> 147 </grid-container> 148</div> 149``` 150 151 152```css 153/* xxx.css */ 154.container{ 155 flex-direction: column; 156 background-color: #F1F3F5; 157 width: 100%; 158 height: 100%; 159 justify-content: center; 160 align-items: center; 161} 162text{ 163 color: white; 164 font-size: 40px; 165} 166``` 167 168 169 170> **NOTE** 171> 172> **\<grid-row>** supports only **\<grid-col>** as a child component. You can add content only to **\<grid-col>**. 173 174 175## Example Scenario 176 177In 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. 178 179 180```html 181<!-- index.hml --> 182<div class="container"> 183 <refresh refreshing="{{fresh}}" onrefresh="refresh"> 184 <grid-container id="mygrid" gutter="20" style="margin: 10px;"> 185 <grid-row style="height:200px;width: 100%;background-color: #e7e7e2;margin-top: 50px; padding: 0px 20px;border-radius: 15px;" for="item in list"> 186 <grid-col span="0" style="width: 40%;"> 187 <div style="align-items: center;justify-content: center"> 188 <image src="{{item.src}}" style="object-fit: contain;border-radius: 30px;"></image> 189 </div> 190 </grid-col> 191 <grid-col span="0" style="width: 60%;"> 192 <div style="align-items: center;justify-content: center;width: 100%;height: 100%;text-align: center;"> 193 <text>image{{item.id}}</text> 194 </div> 195 </grid-col> 196 </grid-row> 197 </grid-container> 198 </refresh> 199</div> 200``` 201 202 203```css 204/* xxx.css */ 205.container{ 206 flex-direction: column; 207 background-color: #F1F3F5; 208 width: 100%; 209 height: 100%; 210} 211text{ 212 color: #0a0aef; 213 font-size: 60px; 214} 215``` 216 217 218```js 219// index.js 220import promptAction from '@ohos.promptAction'; 221export default { 222 data:{ 223 list:[ 224 {src:'common/images/1.png',id:'1'}, 225 {src:'common/images/2.png',id:'2'}, 226 {src:'common/images/3.png',id:'3'} 227 ], 228 fresh:false 229 }, 230 refresh(e) { 231 promptAction.showToast({ 232 message: 'refreshing' 233 }) 234 var that = this; 235 that.fresh = e.refreshing; 236 setTimeout(function () { 237 that.fresh = false; 238 that.list.unshift({src: 'common/images/4.png',id:'4'}); 239 promptAction.showToast({ 240 message: 'succeed' 241 }) 242 }, 2000) 243 } 244} 245``` 246 247 248