CSS Grid Item
Grid Items
A grid container contains one or more grid items.
By default, a container has one grid item for each column, in each row, but you can style the grid items so that they will span multiple columns and/or rows.
The grid-column-start and grid-column-end Properties
The grid-column-start
property specifies where to start
a grid item.
The grid-column-end
property specifies where to
end a grid item.
Example
Place the first grid item at column-line 1, and let it end on column-line 3:
.item1 {
grid-column-start: 1;
grid-column-end: 3;
}
Result:
The grid-column Property
The grid-column
property is a shorthand property for the grid-column-start
and the grid-column-end
properties.
To place an item, you can refer to line numbers, or use the keyword "span" to define how many columns the item will span.
Example
Place the first grid item at column-line 1, and let it span 2 columns:
.item1 {
grid-column: 1 / span
2;
}
Result:
Example
Make "item1" start on column 1 and end before column 4:
.item1 {
grid-column: 1 / 4;
}
Result:
Example
Make "item2" start on column 2 and span 2 columns:
.item2 {
grid-column: 2 / span 2;
}
Result:
The grid-row-start and grid-row-end Property
The grid-row-start
property specifies where to start
a grid item.
The grid-row-end
property specifies where to
end a grid item.
Example
Place the first grid item at row line 1, and let it end on row line 3:
.item1 {
grid-row-start: 1;
grid-row-end: 3;
}
Result:
The grid-row Property
The grid-row
property is a shorthand property for the grid-row-start
and the grid-row-end
properties.
To place an item, you can refer to line numbers, or use the keyword "span" to define how many rows the item will span:
Example
Place the first grid item at row-line 1, and let it span 2 rows:
.item1 {
grid-row: 1 / span 2;
}
Result:
Example
Make "item1" start on row-line 1 and end before row-line 4:
.item1 {
grid-row: 1 / 4;
}
Result:
The grid-area Property
The grid-area
property is a shorthand property for the
grid-row-start
, grid-column-start
, grid-row-end
and the grid-column-end
properties.
The syntax is grid-row-start / grid-column-start / grid-row-end / grid-column-end.
Example
Make "item4" start on row-line 1 and column-line 2, and end on row-line 3 and column line 2:
.item4 {
grid-area: 1 / 2 / 3 / 2;
}
Result:
Example
Make "item4" start on row-line 1 and column-line 1, and span 4 rows and 1 column:
.item8 {
grid-area: 1 / 1 / span 4 / span 1;
}
Result:
Naming Grid Items with grid-area
The grid-area
property can also be used to assign names to grid items.
The named grid items can then be referred to by the grid-template-areas
property
of the grid container.
Example
Item1 gets the name "myArea" and spans all five columns in a five columns grid layout:
.item1 {
grid-area:
myArea;
}
.grid-container {
grid-template-areas: 'myArea myArea
myArea myArea myArea';
}
Result:
Each row is defined by apostrophes (' ').
The named grid items in each row is defined inside the apostrophes, separated by a space.
Example
Let "myArea" span three columns in a five columns grid layout (period signs represent items with no name):
.item1 {
grid-area:
myArea;
}
.grid-container {
grid-template-areas: 'myArea myArea
myArea . .';
}
Result:
Note: A period sign represents a grid item with no name.
To define two rows, define the second row inside another set of apostrophes:
Example
Let "item1" span two columns and two rows:
.item1 {
grid-area:
myArea;
}
.grid-container {
grid-template-areas:
'myArea myArea
. . .'
'myArea myArea . . .';
}
Result:
Example
Name all grid items, and make a ready-to-use webpage template:
.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 main right'
'menu footer footer footer footer
footer';
}
Result:
The Order of the Grid Items
The grid-area
property can also be used to
define the order of the grid items.
The first grid item in the HTML code does not have to appear as the first item in the grid.
Example
Define the order of the grid items:
/* place in row 1 column 3 */
.item1 {grid-area: 1 / 3;}
/* place in
row 2 column 3 */
.item2 {grid-area: 2 / 3;}
/* place in row 1
column 1 */
.item3 {grid-area: 1 / 1;}
/* place in row 1 column 2 */
.item4 {grid-area: 1 / 2;}
/* place in row 2 column 1 */
.item5
{grid-area: 2 / 1;}
/* place in row 2 column 2 */
.item6 {grid-area:
2 / 2;}
Result:
You can also re-arrange the order for certain screen sizes, with media queries:
Example
Re-arrange order on small devices:
@media only screen and (max-width: 500px) {
.item1 {grid-area: 1 /
span 3;}
.item2 {grid-area: 3 / 3;}
.item3 {grid-area: 2 /
1;}
.item4 {grid-area: 2 / 2 / span 2;}
.item5 {grid-area:
3 / 1;}
.item6 {grid-area: 2 / 3;}
}
The justify-self Property
The justify-self
property is used to align the content
of a grid item along the row axis.
Example
.item1 {
justify-self: right;
}
.item6 {
justify-self:
center;
}
Result:
The align-self Property
The align-self
property is used to align the
content of a grid item along the column axis.
Example
.item1 {
align-self: start;
}
.item6 {
align-self: center;
}
Result:
All CSS Grid Item Properties
Property | Description |
---|---|
align-self | Aligns the content for a specific grid item along the column axis |
grid-area | A shorthand property for the grid-row-start, grid-column-start, grid-row-end and the grid-column-end properties |
grid-column | A shorthand property for the grid-column-start and the grid-column-end properties |
grid-column-end | Specifies where to end the grid item |
grid-column-start | Specifies where to start the grid item |
grid-row | A shorthand property for the grid-row-start and the grid-row-end properties |
grid-row-end | Specifies where to end the grid item |
grid-row-start | Specifies where to start the grid item |
justify-self | Aligns the content for a specific grid item along the row axis |
place-self | A shorthand property for the align-self and the justify-self properties |