Grid Item in CSS
? CSS Grid Item
A Grid Item is any direct child of a CSS Grid Container. These items are placed into the grid cells (the intersection of rows and columns) and can be precisely controlled using various CSS Grid properties.
Key Properties for Grid Items
| Property | Description | Example |
|---|---|---|
grid-column-start | Specifies the starting grid column line | grid-column-start: 2; |
grid-column-end | Specifies the ending grid column line | grid-column-end: 4; |
grid-row-start | Specifies the starting grid row line | grid-row-start: 1; |
grid-row-end | Specifies the ending grid row line | grid-row-end: 3; |
grid-column | Shorthand for grid-column-start / grid-column-end | grid-column: 2 / 4; |
grid-row | Shorthand for grid-row-start / grid-row-end | grid-row: 1 / 3; |
grid-area | Assigns a grid item to a named area or spans lines | grid-area: 1 / 2 / 3 / 4; |
justify-self | Aligns item horizontally within its grid cell | start, end, center, stretch |
align-self | Aligns item vertically within its grid cell | start, end, center, stretch |
Positioning Grid Items by Lines
You can position a grid item by specifying the grid lines it should start and end on.
.item { grid-column: 2 / 4; /* spans from column line 2 up to (but not including) 4 */ grid-row: 1 / 3; /* spans from row line 1 to 3 */}Example: Grid Item Spanning Multiple Cells
<div class="container"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div></div>.container { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: 100px 100px; gap: 10px;}.item2 { grid-column: 2 / 4; /* spans 2 columns */ grid-row: 1 / 3; /* spans 2 rows */ background-color: lightcoral;}Aligning Individual Items
.item1 { justify-self: center; /* horizontally centers the item */ align-self: end; /* aligns item to the bottom of its cell */}Summary
Grid Items are direct children of the grid container.
Use
grid-columnandgrid-rowto position and span items.Use
justify-selfandalign-selfto align items inside their grid cells.grid-areais a shorthand or can reference named grid areas.
Want me to show examples with named grid areas or how to combine grid items with responsive layouts?