- Services
- Case Studies
- Technologies
- NextJs development
- Flutter development
- NodeJs development
- ReactJs development
- About
- Contact
- Tools
- Blogs
- FAQ
Mastering CSS grid-template-areas for Better Layouts
Define named grid areas, use keywords, strings, and global values.
Introduction
The grid-template-areas
CSS property allows you to define named grid areas within a grid container, making your layout easier to organize and structure. This property is part of the CSS Grid Layout Module, a powerful system for creating two-dimensional layouts on the web.
Browser Compatibility
The grid-template-areas
property is well-supported across many devices and browser versions, having been available since July 2020. This ensures that your grid designs work consistently across different platforms.
Description
The grid-template-areas
property allows you to define named grid areas within a grid container. This makes it easier to manage and reference these areas in your CSS. By using grid-template-areas
, you can create a more organized and flexible grid layout, which is especially useful for complex web designs.
Syntax
Values
none
The none
keyword value indicates that the grid container does not define any named grid areas. This is the default value for the property.
String Values
String values are used to define the layout of the grid. Each string represents a row, and each cell within the string represents a column. Multiple cells with the same name within and between rows create a single named grid area that spans the corresponding grid cells. These cells must form a rectangle.
Example:
In this example, a
, b
, c
, and d
are named grid areas, and .
represents an empty cell.
Null Cell Tokens
All unnamed areas in the grid can be represented using null cell tokens, which are sequences of one or more .
(U+002E FULL STOP) characters. These tokens create empty spaces in the grid.
Example:
In this example, a null token (.
) is used to create an empty space in the grid container.
Global Values
The grid-template-areas
property also accepts global values:
inherit
: The grid container inherits the value from its parent element.initial
: The grid container is set to its initial value, which isnone
.revert
: The grid container reverts to the default value specified by the user agent.revert-layer
: The grid container reverts to the value specified by the user agent for the specific layer.unset
: The grid container is reset to the initial value if it is already specified, or inherits the value if it is not.
Formal Definition
The grid-template-areas
property in CSS is used to specify named grid areas within a grid container, allowing for more organized and flexible layouts.
Property | grid-template-areas |
---|---|
Initial Value | none |
Applies To | Grid containers |
Inherited | No |
Computed Value | As specified |
Animation Type | Discrete |
Formal Syntax
none
: This keyword value indicates that the grid container does not define any named grid areas.[<string>]+
: A sequence of one or more strings, where each string represents a row in the grid. Each cell within the string represents a column. Cells with the same name within and between rows create a single named grid area that spans the corresponding grid cells.
Examples
Example 1: Simple Grid Layout
In this example, we will create a simple grid layout with named areas for a header, navigation, main content, and footer.
HTML:
CSS:
Result:
This code creates a grid layout with a header spanning the top row, a navigation area on the left, a main content area on the right, and a footer at the bottom. The null token (.
) is used to create an empty space in the bottom left corner of the grid.
Example 2: Complex Grid Layout
In this example, we will create a more complex grid layout with named areas for a header, menu, main content, right sidebar, and footer.
HTML:
CSS:
Result: This code creates a grid layout with a header spanning the top row, a menu on the left, a main content area in the middle, a right sidebar, and a footer at the bottom. The layout is designed to be flexible and responsive, ensuring a good user experience across different devices.
Specifications
The grid-template-areas
property is defined in the CSS Grid Layout Module Level 2. This module provides a robust system for creating two-dimensional layouts on the web, enabling developers to design complex and flexible grid structures.
CSS Grid Layout Module Level 2
- Specification: CSS Grid Layout Module Level 2
- Section: Grid Template Areas Property
The CSS Grid Layout Module Level 2 outlines the syntax, values, and behavior of the grid-template-areas
property. It specifies how named grid areas should be defined and how they interact with other grid properties. This specification ensures that developers can create reliable and consistent grid layouts that work seamlessly across various devices and browsers.
See Also
grid-template-rows
: Defines the rows of the grid.grid-template-columns
: Defines the columns of the grid.grid-template
: A shorthand property for defining both the rows and columns of the grid.- Grid template areas: An in-depth guide to using grid template areas in CSS.
- Video: Grid template areas: A video tutorial on how to use grid template areas effectively.
Talk with CEO
We'll be right here with you every step of the way.
We'll be here, prepared to commence this promising collaboration.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.