1# div 2 3The **\<div>** component is a basic container that is used as the root node of the page structure or is used to group the content. 4 5> **NOTE** 6> 7> This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version. 8 9 10## Required Permissions 11 12None 13 14 15## Child Components 16 17Supported 18 19 20## Attributes 21 22The [universal attributes](../arkui-js/js-components-common-attributes.md) are supported. 23 24 25## Styles 26 27In addition to the [universal styles](../arkui-js/js-components-common-styles.md), the following styles are supported. 28 29| Name | Type | Default Value | Mandatory| Description | 30| ------------------------------ | -------------- | ------------ | ---- | ------------------------------------------------------------ | 31| flex-direction | string | row | No | Main axis direction of the flex container, which defines how items are placed in the container. Available values are as follows:<br>- **column**: Items are placed vertically from top to bottom.<br>- **row**: Items are placed horizontally from left to right.| 32| flex-wrap | string | nowrap | No | Whether items in the flex container are displayed in a single line or multiple lines. The value cannot be dynamically updated. Available values are as follows:<br>- **nowrap**: Flex items are displayed in a single line.<br>- **wrap**: Flex items are displayed in multiple lines.| 33| justify-content | string | flex-start | No | How items are aligned along the main axis of the flex container. Available values are as follows:<br>- **flex-start**: Items are packed toward the start edge of the container along the main axis.<br>- **flex-end**: Items are packed toward the end edge of the container along the main axis.<br>- **center**: Items are packed toward the center of the container along the main axis.<br>- **space-between**: Items are positioned with space between the rows.<br>- **space-around**: Items are positioned with space before, between, and after the rows.<br>- **space-evenly**<sup>5+</sup>: Items are distributed within the container along the main axis, with even space between each two.| 34| align-items | string | stretch<br>| No | How items are aligned along the cross axis of the flex container. Available values are as follows:<br>- **stretch**: Items are stretched to the same height or width as the container along the cross axis.<br>- **flex-start**: Items are packed toward the start edge of the cross axis.<br>- **flex-end**: Items are packed toward the end edge of the cross axis.<br>- **center**: Items are packed toward the center of the cross axis.<br>- **baseline**: In a vertical layout, items are aligned to the start of the cross axis, which means that this value is equivalent of **flex-start**. In a horizontal layout, items are aligned with the text baseline if there is text involved, and aligned to the bottom otherwise.| 35| align-content | string | flex-start | No | Multi-row alignment mode when there is extra space in the cross axis. Available values are as follows:<br>- **flex-start**: All rows are packed toward the start edge of the cross axis. The start edge of the cross axis of the first row is aligned with the start edge of the cross axis of the container. All subsequent rows are aligned with the previous row.<br>- **flex-end**: All rows are packed toward the end edge of the cross axis. The end of the cross axis of the last row is aligned with the end of the cross axis of the container. All subsequent rows are aligned with the previous row.<br>- **center**: All rows are packed toward the center of the cross axis. Rows are close to each other and aligned with the center of the container. The spacing between the start edge of the container's cross axis and the first row is equal to the spacing between the end edge of the container's cross axis and the last row.<br>- **space-between**: All rows are evenly distributed in the container. The spacing between two adjacent rows is the same. The start and end edges of the container's cross axis are aligned with the edges of the first and last rows, respectively.<br>- **space-around**: All rows are evenly distributed in the container. The spacing between two adjacent rows is the same. The spacing between the start edge of the container's cross axis and the first row and that between the end edge and the last row are half of the spacing between two adjacent rows.| 36| display | string | flex | No | Type of the view box of the item. The value cannot be dynamically updated. Available values are as follows:<br>- **flex**: flexible layout<br>- **grid**: grid layout<br>- **none**: not rendered<br>- **inline-flex**<sup>9+</sup>: layout with the **flex** and **inline-block** effects.| 37| grid-template-[columns\|rows] | string | 1 row, 1 column | No | Number of rows and columns in the current grid layout. If this attribute is not set, one row and one column are displayed by default. This attribute is valid only when **display** is set to **grid**.<br>Below are some example values of **grid-template-columns**:<br>- **50px 100px 60px**: There are three columns. The first column is 50 px, the second column is 100 px, and the third column is 60 px.<br>- **1fr 1fr 2fr**: There are three columns, and the width allowed by the parent component is divided into four equal shares. The first column occupies one share, the second column occupies one share, and the third column occupies two shares.<br>- **30% 20% 50%**: There are three columns. The first column occupies 30% of the total width allowed by the parent component, the second column occupies 20%, and the third column occupies 50%.<br>- **repeat (2,100px)**: There are two columns. The first column is 100 px, and the second column is 100 px.<br>- **repeat(auto-fill,100px)**<sup>5+</sup>: Each column is 100 px and repeats to fill the cross axis. The number of columns is calculated based on the column size and the cross axis size.<br>- **auto 1fr 1fr**: There are three columns. The first column is adaptive to the width required by its child components. The remaining space is divided into two equal shares, one share occupied by each of the rest two columns.| 38| grid-[columns\|rows]-gap | <length> | 0 | No | Size of the gap between two consecutive rows or columns in a grid layout. You can also use **grid-gap** to set the same size of the gap between rows and columns. This attribute is valid only when **display** is set to **grid**.| 39| grid-row-[start\|end] | number | - | No | Start and end row numbers of the current item in the grid layout. This attribute is valid only when the item's parent component is a **\<div>** container whose **display** style is set to **grid**.| 40| grid-column-[start\|end] | number | - | No | Start and end column numbers of the current item in the grid layout. This attribute is valid only when the item's parent component is a **\<div>** container whose **display** style is set to **grid**.| 41| grid-auto-flow<sup>5+</sup> | string | - | No | How grid items are laid out automatically. Available values are as follows:<br>- **row**: Elements are filled row by row. When there is no horizontal space in a row, a new row is added.<br>- **column**: Elements are filled column by column. When there is no vertical space in a column, a new column is added.| 42| overflow<sup>6+</sup> | string | visible | No | Display mode when the content exceeds the container size. Available values are as follows:<br>- **visible**: Displays the excess content outside the container.<br>- **hidden**: Truncates the excess content.<br>- **scroll**: Scrolls the content vertically, with a scrollbar provided.<br>**scroll** works for elements whose size is fixed. By default, the scrolling direction is the same as the container direction.| 43| align-items<sup>6+</sup> | string | - | No | How items are aligned along the cross axis in a flex container. Available values are as follows:<br>- **stretch**: Items are stretched to the same height or width as the flex container along the cross axis.<br>- **flex-start**: Items are aligned to the start of the cross axis.<br>- **flex-end**: Items are aligned to the end of the cross axis.<br>- **center**: Items are aligned in the center of the cross axis.<br>- **baseline**: In a vertical layout, items are aligned to the start of the cross axis, which means that this value is equivalent of **flex-start**. In a horizontal layout, items are aligned with the text baseline if there is text involved, and aligned to the bottom otherwise.| 44| scrollbar-color<sup>6+</sup> | <color> | - | No | Color of the scrollbar. | 45| scrollbar-width<sup>6+</sup> | <length> | - | No | Width of the scrollbar. | 46| overscroll-effect<sup>6+</sup> | string | - | No | How the scrollbar behaves when it reaches the edge of the scrolling area. Available values are as follows:<br>- **spring**: Similar to the physical dynamic effect of a spring. When the scrollbar reaches the edge, it can continue to scroll for a distance based on the initial speed or a touch event. It rebounds after being released.<br>- **fade**: Similar to the physical dynamic effect of fade. When the scrollbar reaches the edge, a wave shape fades. The fade changes according to the speed and scrolling distance.<br>- **none**: No effect when the scrollbar reaches the edge.| 47 48 49## Events 50 51In addition to the [universal events](../arkui-js/js-components-common-events.md), the following events are supported. 52 53| Name | Parameter | Description | 54| ------------------------ | ---- | ---------------------------------------- | 55| reachstart<sup>6+</sup> | - | Triggered when the page scrolls to the beginning. This event is triggered only when **flex-direction** is **row**.| 56| reachend<sup>6+</sup> | - | Triggered when the page scrolls to the end. This event is triggered only when **flex-direction** is **row**.| 57| reachtop<sup>6+</sup> | - | Triggered when the page scrolls to the top. This event is triggered only when **flex-direction** is **column**.| 58| reachbottom<sup>6+</sup> | - | Triggered when the page scrolls to the bottom. This event is triggered only when **flex-direction** is **column**.| 59 60## Methods 61 62In addition to the [universal methods](js-components-common-methods.md), the following methods are supported. 63 64| Name | Parameter | Return Value | Description | 65| ---------------------------- | ----------- | ------------ | --------------------------------------- | 66| getScrollOffset<sup>6+</sup> | - | ScrollOffset | Obtains the scrolling offset of the element content.<br>To use this method, **overflow** must be set to **scroll**.| 67| scrollBy<sup>6+</sup> | ScrollParam | - | Sets the scrolling offset of the element content.<br>To use this method, **overflow** must be set to **scroll**.| 68 69**Table 1** ScrollOffset<sup>6+</sup> 70 71| Name | Type | Description | 72| ---- | ------ | --------------- | 73| x | number | Offset in the x-axis direction, in px.| 74| y | number | Offset in the y-axis direction, in px.| 75 76**Table 2** ScrollParam<sup>6+</sup> 77 78| Name | Type | Description | 79| ------ | ------- | ---------------- | 80| dx | number | Offset for scrolling in the horizontal direction, in px.| 81| dy | number | Offset for scrolling in the vertical direction, in px.| 82| smooth | boolean | Whether to perform smooth processing. | 83 84 85## Example 86 871. Flex style 88 ```html 89 <!-- xxx.hml --> 90 <div class="container"> 91 <div class="flex-box"> 92 <div class="flex-item color-primary"></div> 93 <div class="flex-item color-warning"></div> 94 <div class="flex-item color-success"></div> 95 </div> 96 </div> 97 ``` 98 99 ```css 100 /* xxx.css */ 101 .container { 102 flex-direction: column; 103 justify-content: center; 104 align-items: center; 105 width: 454px; 106 height: 454px; 107 } 108 .flex-box { 109 justify-content: space-around; 110 align-items: center; 111 width: 400px; 112 height: 140px; 113 background-color: #ffffff; 114 } 115 .flex-item { 116 width: 120px; 117 height: 120px; 118 border-radius: 16px; 119 } 120 .color-primary { 121 background-color: #007dff; 122 } 123 .color-warning { 124 background-color: #ff7500; 125 } 126 .color-success { 127 background-color: #41ba41; 128 } 129 ``` 130 131  132 1332. Flex wrap style 134 ```html 135 <!-- xxx.hml --> 136 <div class="container"> 137 <div class="flex-box"> 138 <div class="flex-item color-primary"></div> 139 <div class="flex-item color-warning"></div> 140 <div class="flex-item color-success"></div> 141 </div> 142 </div> 143 ``` 144 145 ```css 146 /* xxx.css */ 147 .container { 148 flex-direction: column; 149 justify-content: center; 150 align-items: center; 151 width: 454px; 152 height: 454px; 153 } 154 .flex-box { 155 justify-content: space-around; 156 align-items: center; 157 flex-wrap: wrap; 158 width: 300px; 159 height: 250px; 160 background-color: #ffffff; 161 } 162 .flex-item { 163 width: 120px; 164 height: 120px; 165 border-radius: 16px; 166 } 167 .color-primary { 168 background-color: #007dff; 169 } 170 .color-warning { 171 background-color: #ff7500; 172 } 173 .color-success { 174 background-color: #41ba41; 175 } 176 ``` 177 178  179 1803. Grid style 181 182 ```html 183 <!-- xxx.hml --> 184 <div class="common grid-parent"> 185 <div class="grid-child grid-left-top"></div> 186 <div class="grid-child grid-left-bottom"></div> 187 <div class="grid-child grid-right-top"></div> 188 <div class="grid-child grid-right-bottom"></div> 189 </div> 190 ``` 191 192 ```css 193 /* xxx.css */ 194 .common { 195 width: 400px; 196 height: 400px; 197 background-color: #ffffff; 198 align-items: center; 199 justify-content: center; 200 margin: 24px; 201 } 202 .grid-parent { 203 display: grid; 204 grid-template-columns: 35% 35%; 205 grid-columns-gap: 24px; 206 grid-rows-gap: 24px; 207 grid-template-rows: 35% 35%; 208 } 209 .grid-child { 210 width: 100%; 211 height: 100%; 212 border-radius: 8px; 213 } 214 .grid-left-top { 215 grid-row-start: 0; 216 grid-column-start: 0; 217 grid-row-end: 0; 218 grid-column-end: 0; 219 background-color: #3f56ea; 220 } 221 .grid-left-bottom { 222 grid-row-start: 1; 223 grid-column-start: 0; 224 grid-row-end: 1; 225 grid-column-end: 0; 226 background-color: #00aaee; 227 } 228 .grid-right-top { 229 grid-row-start: 0; 230 grid-column-start: 1; 231 grid-row-end: 0; 232 grid-column-end: 1; 233 background-color: #00bfc9; 234 } 235 .grid-right-bottom { 236 grid-row-start: 1; 237 grid-column-start: 1; 238 grid-row-end: 1; 239 grid-column-end: 1; 240 background-color: #47cc47; 241 } 242 ``` 243 244  245 2464. Dragging<sup>7+</sup> 247 ```html 248 <!-- xxx.hml --> 249 <div class="container"> 250 <div class="content" ondragstart="dragstart" ondrag="drag" ondragend="dragend" style="position: absolute;left: {{left}};top:{{top}};"> 251 </div> 252 </div> 253 ``` 254 255 ```css 256 /* xxx.css */ 257 .container { 258 flex-direction: column; 259 } 260 .content{ 261 width: 200px; 262 height: 200px; 263 background-color: red; 264 } 265 ``` 266 267 ```js 268 // xxx.js 269 import prompt from '@system.prompt'; 270 export default { 271 data:{ 272 left:0, 273 top:0, 274 }, 275 dragstart(e){ 276 prompt.showToast({ 277 message: 'Start to be dragged' 278 }) 279 }, 280 drag(e){ 281 this.left = e.globalX; 282 this.top = e.globalY; 283 }, 284 dragend(e){ 285 prompt.showToast({ 286 message: 'End Drag' 287 }) 288 }, 289 } 290 ``` 291 292  293 294 ```html 295 <!-- xxx.hml --> 296 <div class="container"> 297 <div class="content" ondrag="drag" style="position: absolute;left: {{left}};top: {{top}};"></div> 298 <div style="width: 500px; height: 500px; background-color: yellow; position: fixed; left: 15%; top: 30%; opacity:0.3" 299 ondragenter="dragenter" ondragover="dragover" ondragleave="dragleave" ondrop="drop"> 300 </div> 301 </div> 302 ``` 303 304 ```css 305 /* xxx.css */ 306 .container { 307 flex-direction: column; 308 width: 100%; 309 position: relative; 310 max-width: 100%; 311 } 312 .content{ 313 width: 200px; 314 height: 200px; 315 background-color: red; 316 position: absolute; 317 } 318 ``` 319 320 ```js 321 // xxx.js 322 import prompt from '@system.prompt'; 323 export default { 324 data:{ 325 left:0, 326 top:0, 327 }, 328 drag(e){ 329 this.left = e.globalX; 330 this.top = e.globalY; 331 }, 332 dragenter(e){ 333 prompt.showToast({ 334 message: 'enter' 335 }) 336 }, 337 dragover(e){ 338 prompt.showToast({ 339 message: 'over' 340 }) 341 }, 342 dragleave(e){ 343 prompt.showToast({ 344 message: 'leave' 345 }) 346 }, 347 drop(e){ 348 prompt.showToast({ 349 message: 'drop' 350 }) 351 } 352 } 353 ``` 354 355  356 3575. Pinching<sup>7+</sup> 358 ```html 359 <!-- xxx.hml --> 360 <div class="container"> 361 <div class="content "onpinchstart="pinchstart" onpinchend="pinchend" onpinchupdate="pinchupdate" 362 onpinchcancel=" pinchcancel"> 363 </div> 364 </div> 365 ``` 366 367 ```css 368 /* xxx.css */ 369 .container { 370 flex-direction: column; 371 justify-content: center; 372 align-items: center; 373 width: 454px; 374 height: 454px;} 375 .content{ 376 width: 400px; 377 height: 400px; 378 background-color: aqua; 379 margin:30px 380 } 381 ``` 382 383 ```js 384 // xxx.js 385 import prompt from '@system.prompt'; 386 export default { 387 pinchstart(e){ 388 prompt.showToast({ 389 message: 'pinchstart!!!' 390 }) 391 }, 392 pinchupdate(e){ 393 prompt.showToast({ 394 message: 'Two-finger pinch update' 395 }) 396 }, 397 pinchend(e){ 398 prompt.showToast({ 399 message: 'Finished with two fingers pinching' 400 }) 401 }, 402 pinchcancel(e){ 403 prompt.showToast({ 404 message: 'Finger pinching is interrupted' 405 }) 406 } 407 } 408 ``` 409 410  411