CSS grid is a two-dimensional layout that helps web designers build complex and flexible web pages. As we know now, CSS offers a two-dimensional grid layout model which allows us exact control over the placement and the size of elements within the rows and columns.
Due to CSS Grid's new properties and values, web designers can now create dynamic, responsive, and grid-based layouts without relying heavily on positioning workarounds or floats. It makes the process of organizing the content on a web page easier, more logical, and more effective.
In CSS, we have a two-dimensional layout system that is called CSS grid layout, also known as CSS grid, or with the help of CSS grid, web designers can build flexible and adaptable grid-based layouts for web pages. With precise control over the positioning and sizing of elements within the grid, it offers a flexible way to arrange content in rows and columns.
The grid container and grid items make up the CSS Grid Layout model:
Comparing CSS Grid to more conventional layout techniques that heavily rely on floats, positioning, or intricate CSS frameworks, CSS Grid introduces new properties and values that make it easier and more efficient for web designers to create grid-based layouts.
The grid-template-columns and grid-template-rows properties define the number and size of columns and rows in a CSS Grid. They can use values such as px, %, fr, and auto to control the grid structure.
The following example shows how to create a grid with three columns and two rows of different sizes.
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: 100px 150px 200px;
grid-template-rows: 80px 100px;
gap: 10px;
}
.grid-item {
background-color: lightblue;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
</div>
</body>
</html>
The gap property defines the space between rows and columns in a CSS Grid. You can also use row-gap and column-gap when different spacing is required.
The following example shows how to add space between the rows and columns of a grid.
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(3, 100px);
gap: 20px;
}
.grid-item {
background-color: lightgreen;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
</div>
</body>
</html>
CSS Grid provides properties such as grid-column and grid-row to control the position of individual grid items. These properties can also make an item span across multiple columns or rows.
The following example shows how to make a grid item span across two columns.
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(3, 100px);
gap: 10px;
}
.grid-item {
background-color: lightblue;
padding: 20px;
text-align: center;
}
.item1 {
grid-column: 1 / 3;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item item1">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
</div>
</body>
</html>
You can arrange elements in a grid-based structure using the Grid layout in CSS to build a simple program. A simple HTML and CSS program that uses a Grid layout to create a 3x3 grid is shown here as an example:
HTML:
CSS:
Output:

In this example, you can see the display property of the container is set to the grid to create a 3x3 grid using the grid layout in CSS. The grid's columns and rows are defined using the grid-template-columns and grid-template-rows properties, respectively. We use CSS to style each grid item with a background color and center its content. Each grid item is a div element with the class grid item.
You should see a 3x3 grid with numbers from 1 to 9 set up in a grid layout when you open the HTML file in a web browser. To create grid layouts that are more complex and tailored to your needs, you can change the CSS properties.
We request you to subscribe our newsletter for upcoming updates.