Web Development
HTML Course
CSS Course
JavaScript Course
PHP Course
Python Course
SQL Course
SEO Course

CSS Grid: Introduction

CSS Grid is a two-dimensional layout system that allows organizing HTML elements into rows and columns. It's ideal for complex structures, responsive pages, and precise layouts.

Activating the Grid

To activate the Grid system, apply the display: grid property to a container. Alternatively, you can use display: inline-grid for inline behavior. All direct children of this container automatically become grid items.

🔧 Example: Activating the Grid

Basic Terminology

Before building a layout with CSS Grid, it's important to understand the fundamental terms:

Defining the Structure

The grid structure is established by defining columns and rows. The properties grid-template-columns and grid-template-rows are used to create a flexible or fixed framework.

Example:

grid-template-columns: repeat(3, 1fr);
grid-template-rows: minmax(100px, auto);

This code creates 3 equal columns and rows that have a minimum height of 100px and can expand automatically.

🔧 Live Example

Spacing Between Elements

CSS Grid offers precise control over the space between columns and rows using the gap property. It can be applied globally or separately on each axis.

Example:

grid-template-columns: repeat(3, 1fr);
grid-template-rows: 150px;
gap: 20px;

This code creates 3 equal columns, a single row of 150px, and a 20px gap between all cells.

🔧 Example: Spacing Between Elements

Aligning Elements

CSS Grid provides control over alignment both at the container level and at the item level. These properties help distribute space and position elements inside the grid cells.

At the Container Level

At the Item Level

Example:

.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  justify-items: center;
  align-items: center;
}

All items will be centered on both axes inside their cells.

🔧 Example: Aligning Elements

Manual Positioning

CSS Grid allows precise positioning of elements within the grid using start and end lines. You can use individual properties or shorthand versions.

Example:

.item {
  grid-column: 2 / 4;
  grid-row: 1 / 3;
}

The item will occupy columns 2 and 3 and rows 1 and 2.

🔧 Example: Manual Positioning

Visual Layout with Named Areas

CSS Grid allows the definition of a semantic and maintainable layout using grid-template-areas. Each area is named and assigned to an item using grid-area.

Example of defining the areas:

grid-template-areas:
  "header header"
  "main sidebar"
  "footer footer";

Each grid item must be assigned to a named area:

.header  { grid-area: header; }
.main    { grid-area: main; }
.sidebar { grid-area: sidebar; }
.footer  { grid-area: footer; }
🔧 Example: Visual Layout with Named Areas

Auto-Filling and Flexible Layout

CSS Grid allows automatic generation of columns or rows based on the available space using auto-fill and auto-fit. These are useful in responsive layouts.

Example:

grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));

This code creates columns with a minimum width of 150px that expand to an equal fraction of the available space. The number of columns adjusts automatically.

🔧 Example: Auto-Filling and Flexible Layout

Automatic Flow and Default Dimensions

CSS Grid can automatically generate rows or columns for elements that don't have a defined position. This behavior is controlled using the grid-auto-flow, grid-auto-rows, and grid-auto-columns properties.

Example:

.grid {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 120px;
}

This code creates columns with a width of 120px, and elements are automatically placed in columns.

🔧 Example: Automatic Flow and Default Dimensions

Grid on Individual Elements

Each element within a grid can be manually positioned using specific CSS properties. These offer full control over placement in rows and columns.

Example:

.item {
  grid-column: 2 / 4;
  grid-row: 1 / 3;
}

This code places the element between columns 2 and 4 and between rows 1 and 3.

🔧 Example: Grid on Individual Elements

Implicit vs. Explicit Grid

In CSS Grid, we can define the structure explicitly or let the browser generate it automatically (implicitly).

Example:

.grid {
  display: grid;
  grid-template-columns: 100px 100px;
  grid-auto-rows: 80px;
}

Here, two columns are explicitly defined, and any additional rows are implicitly generated with a height of 80px.

🔧 Example: Implicit vs. Explicit Grid

Grid Nesting and Subgrid

CSS Grid allows the creation of grids inside other grids. This is called nesting. Starting with the CSS Subgrid specification, we can even synchronize inner grids with their parent grid.

Subgrid is supported only in modern browsers (Firefox, Safari, Chromium 117+).

🔧 Example: Grid Nesting and Subgrid

Display Order and Layering in Grid

CSS Grid allows control over the order in which elements are displayed and their layering using properties like order and z-index.

Example:

.item {
  order: 2;
  position: relative;
  z-index: 10;
}

This code moves the element to the second position and brings it above other elements.

🔧 Example: Display Order and Layering in Grid

Grid and Media Queries

CSS Grid works excellently with media queries, allowing the layout to adapt based on screen size.

Example:

.grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
}
@media (max-width: 600px) {
  .grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

This code displays 4 columns on large screens and only 2 columns on small screens.

🔧 Example: Grid and Media Queries

Grid and Fallbacks for Older Browsers

Although CSS Grid is supported by most modern browsers, some older versions do not recognize it. In such cases, fallbacks or feature queries can be used.

Example:

@supports (display: grid) {
  .container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }
}
@supports not (display: grid) {
  .container {
    display: flex;
    flex-wrap: wrap;
  }
}

This code applies Grid only if the browser supports it; otherwise, it falls back to Flexbox.

🔧 Example: Grid and Fallbacks for Older Browsers

Page Layout with Grid

A typical page layout includes:

We use grid-template-areas to visually define the structure.

.grid {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
  grid-template-columns: 200px 1fr;
  grid-template-rows: auto 1fr auto;
  gap: 10px;
}
🔧 Example: Page Layout with Grid

Responsive Image Gallery

We use grid-template-columns with auto-fit and minmax() to create a gallery that automatically adapts to the screen size.

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
}

This code creates columns that automatically expand, with a minimum width of 150px.

🔧 Example: Responsive Image Gallery

Dashboard with Dynamic Cards

We use grid-template-columns with auto-fit and minmax() to create a flexible layout where cards automatically rearrange based on available space.

.dashboard {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

The cards will expand and reorganize automatically across different screen sizes.

🔧 Example: Dashboard with Dynamic Cards

Applied Project: Complete Layout with CSS Grid

This layout combines all the concepts learned: named areas, auto-fill, alignment, layering, media queries, and fallbacks.

.grid {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar content"
    "sidebar gallery"
    "sidebar dashboard"
    "footer footer";
  grid-template-columns: 250px 1fr;
  grid-template-rows: auto auto auto 1fr auto;
  gap: 20px;
  padding: 20px;
}
🔧 Applied Project: Complete and Responsive Layout

🛠️ Useful Tips for CSS Grid

These tips help you build layouts that are robust, clear, and adaptable.

CSS Grid is not just a layout system — it's a visual language. The better you understand it, the more clearly you can express your ideas.


🧠 Quiz – CSS Grid: Introduction


Congratulations! This introductory course ends here. In the next course, you'll learn the JavaScript programming language. Good luck and happy learning!

Top