codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎨

HTML & CSS

23 / 59 topics
22CSS Flexbox23CSS Grid24CSS Responsive Design25CSS Media Queries
Tutorials/HTML & CSS/CSS Grid
🎨HTML & CSS

CSS Grid

Updated 2026-05-15
10 min read

CSS Grid

Introduction

In the world of web development, creating layouts that are both aesthetically pleasing and functional is crucial. As web technologies have evolved, so too has our ability to create complex and responsive designs. One powerful tool in this regard is CSS Grid, a layout system designed to make it easier to design two-dimensional grid-based user interfaces.

CSS Grid allows you to define rows and columns within a container element, enabling precise control over the placement of child elements. This makes it an ideal choice for creating responsive layouts that adapt to different screen sizes and orientations.

Concept

At its core, CSS Grid is based on the concept of defining a grid system using rows and columns. You can specify how many rows and columns you want, as well as their sizes. Once the grid is defined, you can position elements within it using various properties such as grid-row, grid-column, and grid-area.

CSS Grid also supports responsive design through media queries, allowing you to adjust the layout based on the size of the viewport.

Examples

Let's dive into some practical examples to see how CSS Grid works in action.

Basic Grid Layout

First, let's create a simple grid layout with three columns and two rows. We'll use the display: grid property to define the grid container and specify the number of columns using grid-template-columns.

CSS
1.container {
2display: grid;
3grid-template-columns: repeat(3, 1fr);
4gap: 10px;
5}
6.item {
7background-color: #f0f0f0;
8padding: 20px;
9text-align: center;
10}
HTML
1<div class="container">
2<div class="item">Item 1</div>
3<div class="item">Item 2</div>
4<div class="item">Item 3</div>
5<div class="item">Item 4</div>
6<div class="item">Item 5</div>
7<div class="item">Item 6</div>
8</div>

In this example, the .container class defines a grid with three equal columns. The gap property adds space between the grid items.

Responsive Grid Layout

Now, let's make our grid responsive by using media queries to adjust the number of columns based on the screen size.

CSS
1.container {
2display: grid;
3grid-template-columns: repeat(3, 1fr);
4gap: 10px;
5}
6
7@media (max-width: 768px) {
8.container {
9 grid-template-columns: repeat(2, 1fr);
10}
11}
12
13@media (max-width: 480px) {
14.container {
15 grid-template-columns: 1fr;
16}
17}

In this example, we've added media queries to change the number of columns when the screen width is less than or equal to 768 pixels and 480 pixels. This makes our layout responsive and adaptable to different devices.

Named Areas

CSS Grid also allows you to define named areas within your grid, making it easier to position elements by name rather than using row and column numbers.

CSS
1.container {
2display: grid;
3grid-template-areas:
4 "header header"
5 "sidebar main";
6gap: 10px;
7}
8
9.header { grid-area: header; }
10.sidebar { grid-area: sidebar; }
11.main { grid-area: main; }
HTML
1<div class="container">
2<div class="header">Header</div>
3<div class="sidebar">Sidebar</div>
4<div class="main">Main Content</div>
5</div>

In this example, we've defined three named areas: header, sidebar, and main. We then use the grid-area property to place each element in its designated area.

What's Next?

Now that you have a good understanding of CSS Grid, it's time to explore other aspects of web development. In the next section, we'll dive into HTML Images, learning how to add and style images on your web pages.

Stay tuned for more tutorials and tips to enhance your web development skills!


PreviousCSS FlexboxNext CSS Responsive Design

Recommended Gear

CSS FlexboxCSS Responsive Design