CSS Grid is a powerful layout system that allows for precise control over the placement of items in both rows and columns. To make use of this layout, we can define the grid columns and rows in a container using the grid-template-columns
and grid-template-rows
properties. This article will explain how to define grid columns and rows in CSS and provide examples to illustrate the concepts.
Grid columns and rows form the foundation of the CSS Grid layout. The grid-template-columns
property defines the width of the columns, while the grid-template-rows
property defines the height of the rows. Both properties accept values in different units, including pixels (px), percentages (%), and flexible units like fr
(fractional units).
To define the number of columns and their width, you use the grid-template-columns
property. This property specifies how many columns your grid will have and what size each column will be. You can use any valid length unit, but fractional units (fr
) are commonly used to create flexible layouts.
<style> .grid-container { display: grid; grid-template-columns: 1fr 1fr 1fr; } </style> <div class="grid-container"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
In this example:
1fr
unit means "one fraction" of the available space. So, all three columns will share the space equally.<style> .grid-container { display: grid; grid-template-columns: 200px 300px 1fr; } </style> <div class="grid-container"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
In this example:
1fr
.
Just like columns, you can define the number of rows and their height using the grid-template-rows
property. This property allows you to control how much space each row occupies. You can use fixed units like pixels, flexible units like fr
, or even percentages for row heights.
<style> .grid-container { display: grid; grid-template-rows: 100px 200px; } </style> <div class="grid-container"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
In this example:
fr
for Flexible Rows<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:
You can combine both grid-template-columns
and grid-template-rows
to create a full grid layout. This allows you to define both the columns and rows simultaneously and control how your grid items are arranged.
<style> .grid-container { display: grid; grid-template-columns: 1fr 2fr 1fr; grid-template-rows: 100px 200px 150px; } </style> <div class="grid-container"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> <div>Item 4</div> <div>Item 5</div> <div>Item 6</div> </div>
In this example:
1fr
, 2fr
, and 1fr
.
Defining grid columns and rows in CSS is a fundamental part of working with CSS Grid. By using grid-template-columns
and grid-template-rows
, you can create flexible, responsive layouts that adapt to different screen sizes. Whether you're building a simple layout or a complex grid structure, understanding how to define and manipulate rows and columns is essential for creating well-organized, dynamic web pages.