How Can We Help?
< All Topics

Grid

CSS Grid is a powerful layout system that allows you to create two-dimensional grid-based layouts with ease. It provides a flexible and intuitive way to arrange elements in rows and columns. Let’s explore CSS Grid and how you can use it to create sophisticated and responsive layouts.

### Container and Grid Items

To use CSS Grid, you need a container element that acts as a grid container and one or more child elements within it that become grid items. The container is defined using the `display` property with the value `grid` or `inline-grid`. Here’s an example:

“`
.container {
display: grid;
}

“`

### Grid Template Columns and Rows

The `grid-template-columns` and `grid-template-rows` properties define the columns and rows of the grid respectively. You can specify the size and number of columns or rows using values like pixels (`px`), percentages (`%`), or the `fr` unit for flexible sizing. Here’s an example:

“`
.container {
grid-template-columns: 1fr 2fr 1fr; /* 3 columns with flexible width */
grid-template-rows: 100px 200px; /* 2 rows with specific height */
}

“`

### Grid Gaps

The `gap` property sets the gap between grid items, controlling the spacing between columns and rows. It can take two values to specify the gap horizontally and vertically respectively. Here’s an example:

“`
.container {
gap: 10px 20px; /* 10px gap between rows, 20px gap between columns */
}

“`

### Grid Placement

Grid items can be placed within the grid using the `grid-column` and `grid-row` properties. You can specify the start and end positions of the item within the grid using line numbers or named grid lines. Here’s an example:

“`
.item {
grid-column: 1 / 3; /* spans from column line 1 to column line 3 */
grid-row: 2; /* spans in row 2 */
}

“`

### Grid Auto Placement

CSS Grid also offers automatic placement of grid items using the `grid-auto-flow` property. It controls how the grid items are automatically placed within the grid when there is no explicit placement defined. Here’s an example:

“`
.container {
grid-auto-flow: dense; /* fills empty grid cells with subsequent items */
}

“`

### Grid Responsive Layouts

CSS Grid is particularly useful for creating responsive layouts. By using media queries and changing the grid template columns and rows based on the viewport size, you can create layouts that adapt to different screen sizes. Here’s an example:

“`
.container {
grid-template-columns: 1fr 2fr; /* default: 2 columns */
}

@media (max-width: 768px) {
.container {
grid-template-columns: 1fr; /* 1 column for smaller screens */
}
}

“`

### CSS properties related to CSS Grid

| Property | Description |
| — | — |
| display | Defines the element as a grid container. |
| grid-template-columns | Specifies the columns of the grid and their sizes. |
| grid-template-rows | Specifies the rows of the grid and their sizes. |
| grid-template-areas | Defines named grid areas and their layout within the grid. |
| grid-template | Shorthand property for grid-template-rows, grid-template-columns, and grid-template-areas. |
| grid-column-gap | Sets the gap between columns in the grid. |
| grid-row-gap | Sets the gap between rows in the grid. |
| gap | Shorthand property for grid-row-gap and grid-column-gap. |
| justify-items | Aligns grid items along the inline (horizontal) axis within their grid cell. |
| align-items | Aligns grid items along the block (vertical) axis within their grid cell. |
| place-items | Shorthand property for align-items and justify-items. |
| justify-content | Aligns grid items along the inline (horizontal) axis within the grid container. |
| align-content | Aligns grid items along the block (vertical) axis within the grid container when there is extra space. |
| place-content | Shorthand property for align-content and justify-content. |
| grid-auto-columns | Specifies the size of implicitly created columns in the grid. |
| grid-auto-rows | Specifies the size of implicitly created rows in the grid. |
| grid-auto-flow | Controls the placement of items in the grid when they are not explicitly placed. |
| grid | Shorthand property for grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, and grid-auto-flow. |
| grid-area | Specifies a grid item’s size and location within the grid. |
| grid-column-start | Specifies the start position of a grid item within the grid. |
| grid-column-end | Specifies the end position of a grid item within the grid. |
| grid-row-start | Specifies the start position of a grid item within the grid. |
| grid-row-end | Specifies the end position of a grid item within the grid. |
| grid-column | Shorthand property for grid-column-start and grid-column-end. |
| grid-row | Shorthand property for grid-row-start and grid-row-end. |
| justify-self | Aligns a grid item along the inline (horizontal) axis within its grid cell. |
| align-self | Aligns a grid item along the block (vertical) axis within its grid cell. |
| place-self | Shorthand property for align-self and justify-self. |

These properties can be applied to the grid container and grid items to create flexible and dynamic grid-based layouts using CSS Grid. Use these properties in combination to control the grid structure, placement, alignment, and sizing of grid items within the grid container

### Conclusion

CSS Grid provides a powerful and flexible layout system for creating sophisticated and responsive grid-based layouts. By using the grid container, grid items, grid template columns and rows, gap properties, grid placement, and responsive techniques, you can create dynamic and adaptive layouts for your web pages. Experiment with CSS Grid and combine it with other CSS properties to achieve the desired layout and design for your web projects.

Table of Contents