Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS Grid Items

CSS Grid Items

Try it »


CSS Grid Items

A grid container contains one or more grid items.

By default, each column and each row of a grid container has one grid item. We can also set a grid item to span multiple columns or rows, where rows and columns are referred to by line numbers.

grid-column property

The grid-column property defines the start and end positions of a grid item's column.

Note: grid-column is the shorthand for the grid-column-start and grid-column-end properties.

We can set grid items by referring to line numbers, or use the keyword "span" to define how many columns the item will span.

The following example sets "item1" to start at column 1 and end before column 5:

Example

.item1

{

grid-column:

1

/

5

;

}

Try it »

The following example sets "item1" to span 3 columns:

Example

.item1

{

grid-column:

1

/

span

3

;

}

Try it »

The following example sets "item2" to span 3 columns:

Example

.item2

{

grid-column:

2

/

span

3

;

}

Try it »

grid-row property

The grid-row property defines the start and end positions of a grid item's rows.

Note: grid-row is a shorthand property for grid-row-start and grid-row-end.

We can refer to line numbers to place grid items, or use the keyword "span" to define how many rows an item will span.

The following example sets "item1" to start at row 1 and end before row 4:

Example

.item1

{

grid-row:

1

/

4

;

}

Try it »

The following example sets "item1" to span two rows:

Example

.item1

{

grid-row:

1

/

span

2

;

}

Try it »

grid-area property

The grid-area property is a shorthand for the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties.

The following example sets "item8" to start at row 1 and column 2, and end at row 5 and column 6.

Example

.item8

{

grid-area:

1

/

2

/

5

/

6

;

}

Try it »

The following example sets "item8" to start at row 2 and column 1, spanning 2 rows and 3 columns.

Example

.item8

{

grid-area:

2

/

1

/

span

2

/

span

3

;

}

Try it »


Naming grid items

The grid-area property can be used to name grid items.

Named grid items can be referenced through the container's grid-template-areas property.

The following example names item1 "myArea" and spans five columns:

Example

.item1

{

grid-area:

myArea

;

}

.grid-container

{

grid-template-areas:

'

myArea

myArea

myArea

myArea

myArea

'

;

}

Try it »

Each row is defined within single quotes ' ', separated by spaces.

A . indicates a grid item with no name.

Example

.item1

{

grid-area:

myArea

;

}

.grid-container

{

grid-template-areas:

'

myArea

myArea

. . .'

;

}

Try it »

To define two rows, define the columns of the second row in another set of single quotes ' '.

The following example sets "item1" to span 2 rows and 2 columns:

Example

.item1

{

grid-area:

myArea

;

}

.grid-container

{

grid-template-areas:

'

myArea

myArea

. . .'

;

}

Try it »

Name all grid items and create a web page template:

Example

.item1

{

grid-area:

header

;

}

.item2

{

grid-area:

menu

;

}

.item3

{

grid-area:

main

;

}

.item4

{

grid-area:

right

;

}

.item5

{

grid-area:

footer

;

}

.grid-container

{

grid-template-areas:

'

header

header

header

header

header

header

' '

menu

main

main

main

right

right

' '

menu

footer

footer

footer

footer

footer

'

;

}

Try it »

The order of grid items

Grid layout allows us to place grid items anywhere we like.

The first element in the HTML code does not necessarily have to appear as the first item in the grid.

Example

.item1

{

grid-area:

1

/

3

/

2

/

4

;

}

.item2

{

grid-area:

2

/

3

/

3

/

4

;

}

.item3

{

grid-area:

1

/

1

/

2

/

2

;

}

.item4

{

grid-area:

1

/

2

/

2

/

3

;

}

.item5

{

grid-area:

2

/

1

/

3

/

2

;

}

.item6

{

grid-area:

2

/

2

/

3

/

3

;

}

Try it »

You can use media queries to rearrange the order at specified screen sizes:

Example

@media

only

screen

and

(max-width: 500

px

)

{

.item1

{

grid-area:

1

/

span

3

/

2

/

4

;

}

.item2

{

grid-area:

3

/

3

/

4

/

4

;

}

.item3

{

grid-area:

2

/

1

/

3

/

2

;

}

.item4

{

grid-area:

2

/

2

/

span

2

/

3

;

}

.item5

{

grid-area:

3

/

1

/

4

/

2

;

}

.item6

{

grid-area:

2

/

3

/

3

/

4

;

}

}

Try it »