Css Pr Grid
## CSS grid Property
The `grid` property is a comprehensive CSS shorthand property that allows you to configure all of your grid container settings in a single declaration.
By using `grid`, you can set both **explicit** grid properties (such as rows, columns, and template areas) and **implicit** grid properties (such as auto-rows, auto-columns, and auto-flow) simultaneously.
---
## Quick Example
Create a three-column grid layout where the first row has a fixed height of `160px`:
```css
.grid-container {
display: grid;
grid: 160px / auto auto auto;
}
```
---
## Property Definition and Usage
The `grid` shorthand property can be used to set the following individual properties in one place:
* **Explicit Grid Properties:** `grid-template-rows`, `grid-template-columns`, and `grid-template-areas`.
* **Implicit Grid Properties:** `grid-auto-rows`, `grid-auto-columns`, and `grid-auto-flow`.
* **Gap Properties:** `grid-column-gap` and `grid-row-gap` *(Note: Modern CSS specifications have moved gap properties to the `gap` shorthand, but they can be reset to their initial values by this shorthand).*
### Specification Details
| Feature | Description |
| :--- | :--- |
| **Default Value** | `none none none auto auto row` |
| **Inherited** | No |
| **Animatable** | Yes (refer to individual sub-properties for details) |
| **CSS Version** | CSS Grid Layout Module Level 1 |
| **JavaScript Syntax** | `object.style.grid = "250px / auto auto auto"` |
---
## Browser Support
The numbers in the table specify the first browser version that fully supports the `grid` property:
| Property | Chrome | Edge | Firefox | Safari | Opera |
| :--- | :---: | :---: | :---: | :---: | :---: |
| **grid** | 57 | 16 | 52 | 10 | 44 |
---
## Property Values
Because `grid` is a powerful shorthand, it accepts several syntax variations depending on whether you are defining explicit templates or implicit auto-flows.
| Value | Description |
| :--- | :--- |
| `none` | **Default**. Does not define explicit or implicit rows or columns. |
| `grid-template-rows / grid-template-columns` | Sets the explicit sizes of rows and columns (separated by a slash `/`). |
| `grid-template-areas` | Defines a grid template using named grid areas. |
| `grid-template-rows / grid-auto-columns` | Sets explicit row heights and defines the implicit column width behavior. |
| `grid-auto-rows / grid-template-columns` | Defines the implicit row height behavior and sets explicit column widths. |
| `grid-template-rows / grid-auto-flow grid-auto-columns` | Sets explicit row heights, configures the auto-placement algorithm flow, and sets implicit column widths. |
| `grid-auto-flow grid-auto-rows / grid-template-columns` | Configures the auto-placement algorithm flow, sets implicit row heights, and defines explicit column widths. |
| `initial` | Resets the property to its default value. |
| `inherit` | Inherits the property from its parent element. |
---
## Code Examples
### Example 1: Using Named Grid Areas
In this example, we define a grid area named `myArea`. The item `.item1` is configured to span across the first two columns of the first two rows.
```css
.item1 {
grid-area: myArea;
}
.grid-container {
display: grid;
grid:
'myArea myArea . . .'
'myArea myArea . . .';
}
```
### Example 2: Creating a Complete Web Page Template
You can map out an entire page layout using named grid areas within the `grid` shorthand. This creates a highly readable, self-documenting layout structure.
```css
/* Assign grid area names to items */
.item1 { grid-area: header; }
.item2 { grid-area: menu; }
.item3 { grid-area: main; }
.item4 { grid-area: right; }
.item5 { grid-area: footer; }
/* Define the grid structure on the container */
.grid-container {
display: grid;
grid:
'header header header header header'
'menu main main main right'
'menu footer footer footer footer';
}
```
---
## Considerations & Best Practices
1. **Readability vs. Conciseness:** While the `grid` shorthand is highly concise, it can become difficult to read when mixing explicit templates with implicit auto-flow rules. For complex layouts, using individual properties like `grid-template-columns` and `grid-template-rows` is often preferred for team collaboration.
2. **The Slash (`/`) Separator:** When defining rows and columns together, the slash `/` always separates row configurations (on the left) from column configurations (on the right).
3. **Overwriting Sub-properties:** Remember that using the `grid` shorthand resets all sub-properties it controls to their initial values. If you only want to change columns, use `grid-template-columns` instead of the `grid` shorthand to avoid accidentally resetting your row configurations.
YouTip