CSS Grid Layout is a powerful tool for creating complex, responsive web layouts. Three important properties that help in defining the structure of a grid are grid-template-columns
, grid-template-rows
, and grid-area
. These properties allow you to control the size and placement of grid items in the container. Let's explore each property in detail with examples.
The grid-template-columns
property defines the size of the columns in a grid container. You can specify column sizes using fixed units (like pixels), percentages, or flexible units like fr
.
<style> .grid-container { display: grid; grid-template-columns: 100px 200px 100px; } </style> <div class="grid-container"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
In this example:
<style> .grid-container { display: grid; grid-template-columns: 1fr 2fr 1fr; } </style> <div class="grid-container"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
In this example:
The grid-template-rows
property defines the size of the rows in a grid container. Similar to grid-template-columns
, it accepts fixed sizes, percentages, or flexible units like fr
.
<style> .grid-container { display: grid; grid-template-rows: 150px 200px 100px; } </style> <div class="grid-container"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
In this example:
<style> .grid-container { display: grid; grid-template-rows: 1fr 2fr 1fr; } </style> <div class="grid-container"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
In this example:
The grid-area
property allows you to place a grid item into a specific grid area by specifying the grid lines it should span across. It can take four values: row-start
, column-start
, row-end
, and column-end
.
<style> .grid-container { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 100px 200px; } .item1 { grid-area: 1 / 1 / 2 / 2; } .item2 { grid-area: 2 / 2 / 3 / 4; } </style> <div class="grid-container"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> </div>
In this example:
Item 1
spans from row 1, column 1 to row 2, column 2, so it takes up the top-left corner.Item 2
starts at row 2, column 2, and spans from row 2 to 3, and column 2 to 4, making it span two columns.<style> .grid-container { display: grid; grid-template-columns: 1fr 2fr 1fr; grid-template-rows: 100px 200px; } .header { grid-area: 1 / 1 / 2 / 4; } .main-content { grid-area: 2 / 1 / 3 / 3; } .sidebar { grid-area: 2 / 3 / 3 / 4; } </style> <div class="grid-container"> <div class="header">Header</div> <div class="main-content">Main Content</div> <div class="sidebar">Sidebar</div> </div>
In this example:
header
spans across all three columns in the first row.main-content
spans the first two columns in the second row.sidebar
takes the third column in the second row.
The properties grid-template-columns
, grid-template-rows
, and grid-area
are key to mastering CSS Grid. By using these properties, you can define the structure and placement of grid items, giving you complete control over your layout. Experiment with different combinations to create flexible, responsive web designs.