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

CSS Grid Container

CSS Grid Container

Try it »


Grid container

To turn an HTML element into a grid container, you can set the display property to grid or inline-grid.

A grid container holds grid items composed of columns and rows.

grid-template-columns property

The grid-template-columns property defines the number of columns in a grid layout, and it can also set the width of each column.

The property value is a space-separated list, in which each value defines the width of the corresponding column.

If you want the grid layout to contain 4 columns, you need to set the widths of the 4 columns. If all columns have the same width, you can set them to auto.

The following example sets up a 4-column grid layout:

Example

.grid-container

{

display:

grid

;

grid-template-columns:

auto

auto

auto

auto

;

}

Try it »

**Note:** If you have more than 4 grid items in a 4-column grid, the grid layout will generate a new row to place the extra item.

The grid-template-columns property can also be used to specify column widths.

Example

.grid-container

{

display:

grid

;

grid-template-columns:

80

px

200

px

auto

40

px

;

}

Try it »

grid-template-rows property

The grid-template-rows property sets the height of each row.

The property value is a space-separated list, in which each value defines the height of the corresponding row:

Example

.grid-container

{

display:

grid

;

grid-template-rows:

80

px

200

px

;

}

Try it »

justify-content Property

The justify-content property is used to align the grid within the container, setting how to distribute the space between and around items along the main axis of the flex container (or the grid row axis).

**Note:** The total width of the grid must be less than the container's width for the justify-content property to take effect.

justify-content For details, refer to:CSS justify-content property

Example

.grid-container

{

display:

grid

;

justify-content:

space-evenly

;

}

Try it »

Example

.grid-container

{

display:

grid

;

justify-content:

space-around

;

}

Try it »

Example

.grid-container

{

display:

grid

;

justify-content:

space-between

;

}

Try it »

Example

.grid-container

{

display:

grid

;

justify-content:

center

;

}

Try it »

Example

.grid-container

{

display:

grid

;

justify-content:

start

;

}

Try it »

Example

.grid-container

{

display:

grid

;

justify-content:

end

;

}

Try it »

align-content Property

The align-content property is used to set the alignment of grid items in the container along the vertical direction.

**Note:** The total height of the grid items must be less than the container's height for the align-content property to take effect.

Example

.grid-container

{

display:

grid

;

height:

400

px

;

align-content:

center

;

}

Try it »

Example

.grid-container

{

display:

grid

;

height:

400

px

;

align-content:

space-evenly

;

}

Try it »

Example

.grid-container

{

display:

grid

;

height:

400

px

;

align-content:

space-around

;

}

Try it »

Example

.grid-container

{

display:

grid

;

height:

400

px

;

align-content:

space-between

;

}

Try it »

Example

.grid-container

{

display:

grid

;

height:

400

px

;

align-content:

start

;

}

Try it »

Example

.grid-container

{

display:

grid

;

height:

400

px

;

align-content:

end

;

}

Try it »