CSS Grid is the powerful layout property in CSS that allows the web developers to design the complex and responsive grid-based layout. Unlike the older layout systems like Flexbox, which focuses on one dimensional layouts. CSS Grid is the two dimensional system, means that it can handle simultaneously the both rows and columns.
Prerequisites
What is CSS Grid?
CSS Grid is a layout system that divides the webpage into the grid of the rows and columns, where you can place items precisely within this grid. It can gives the more flexibility and control over how the elements are displayed on the webpage. CSS Grid can be used to create the responsive layouts that adapt to the different screen sizes, making it a perfect choice for the modern web development.
CSS Grid introduces the few new concepts such as the grid containers and grid items, where the parent element is the grid container, and the children are the grid items.
Properties of the CSS Grid
Some key properties of the CSS Grid that help in the creating a grid layout:
Grid Container Properties:
- display: grid : It can defines the element as the grid container.
- grid-template-columns: It can specifies the number and size of the columns in the grid.
- grid-template-rows: It can specifies the number and size of the rows in the grid.
- grid-gap: It can specifies the spacing between the rows and columns.
Grid Item Properties:
- grid-column-start and grid-column-end: It can defines the start end points of the grid items in the columns.
- grid-row-start and grid-row-end: It can defines the start and end points of the grid items in the rows.
- grid-area: The shorthand for specifying the both row and column positioning.
Syntax:
.container {
display: grid;
grid-template-columns: 100px 200px auto;
grid-template-rows: 100px 100px;
grid-gap: 10px;
}
.item1 {
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 2;
}
.item2 {
grid-column: 2 / 4;
grid-row: 2;
}
Explanation
- display: grid: It defines the container as the grid.
- grid-template-columns: It defines the layout of the columns. For example,
grid-template-columns: 100px 200px auto;
creates three columns: the first is 100px wide, the second is 200px, and the third takes up the remaining space - grid-template-rows: It creates space between rows and columns. For example,
grid-gap: 10px;
creates a 10px gap between both rows and columns. - grid-gap: It can creates a 10px gap between the rows and columns.
- grid-column-start and grid-column-end: It defines where an item starts and ends in terms of columns. For example,
grid-column-start: 1;
and grid-column-end: 3;
means the item spans from the first column to the third column. - grid-row-start and grid-row-end: It defines where an item starts and ends in terms of rows. For example,
grid-row-start: 1;
and grid-row-end: 2;
means the item spans from the first row to the second row.
In this example, we can create the simple grid layout using the CSS Grid.
HTML
<!-- index.html-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Grid Example</title>
<style>
.container {
display: grid;
grid-template-columns: 100px 200px auto;
grid-template-rows: 100px 100px;
grid-gap: 10px;
background-color: #ddd;
padding: 10px;
}
.item {
background-color: #4CAF50;
color: white;
padding: 20px;
font-size: 20px;
text-align: center;
}
.item1 {
grid-column: 1 / 3;
grid-row: 1 / 2;
}
.item2 {
grid-column: 2 / 4;
grid-row: 2;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">Item 1</div>
<div class="item item2">Item 2</div>
<div class="item item3">Item 3</div>
</div>
</body>
</html>
Output
Explanation
- The .container can uses the grid-template-columns and grid-template-rows to define the grid with three columns and two rows.
- grid-gap can be adds the spacing between the grid items.
- .item1 spans two columns but only stays in the first row, while .item2 spans across the second and third columns in the second row.
Similar Reads
CSS Grid Items CSS Grid Items are contained in the Grid Container, where for each row, the container will contain one grid item for each column, by default. The Grid Items can also be styled, which will span in multiple columns and/or rows. Grid Item is a no of child boxes wrapped inside the grid container. There
9 min read
Pure CSS Grids While creating a genuine responsive website layout the grid system is a crucial tool for web developers. A grid is a set of classes that helps us to divide the width of the screen or display into smaller units and make the website look responsive on various devices. Pure.CSS also comes up with such
4 min read
Primer CSS Grid Primer CSS is a free open-source CSS framework that is built with the GitHub design system to provide support to the broad spectrum of Github websites. It creates the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure that our patterns ar
4 min read
Materialize CSS Grids There are 12 standard columns fluid responsive grid systems that helps you to layout your page in an ordered and easy way. It uses the row and column style classes to define rows and columns respectively. Rows are used to specify a padding-less container to be used for responsive columns and col are
3 min read
Pure CSS Grids Units Sizes Pure CSS is a CSS framework. It is a free and open-source tool collection for creating responsive web applications. It was developed by Yahoo and is used for creating faster, more beautiful, and more responsive layouts. Pure Grids are easy to work with and very powerful in creating grid layouts in w
3 min read
CSS grid Property It is a CSS property that offers a grid-based layout system, with rows and columns, making it easier to design web pages without floats and positioning. Try It: .item { border: 1px solid gray; font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; border-radius: 3px; margin:
4 min read
CSS grid-row Property The grid-row property in CSS is used to define the size and position of a grid item within a grid layout. It combines the grid-row-start and grid-row-end properties to specify the item's start and end positions along the row axis.Syntax:grid-row: grid-row-start|grid-row-end;Property Values1. grid-ro
3 min read
CSS Grid Layout Module CSS Grid Layout is used to design responsive web layouts with rows and columns. It allows you to place items exactly where you want, making it easier to create flexible and modern web pages.Unlike older methods like floats, CSS Grid gives more control over alignment and adapts well to different scre
5 min read
Primer CSS Grid Column Widths Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by
2 min read
CSS grid-template Property The grid-template property in CSS is a shorthand property for defining grid columns, rows, and areas. It allows you to set values for the following longhand properties: grid-template-rows, grid-template-columns, and grid-template-areas.Syntaxgrid-template: none| grid-template-rows/ grid-template-col
4 min read