🧩 CSS Grid Layout (Full Guide)

Last Updated: November 2025


CSS Grid is a two-dimensional layout system for creating rows and columns easily.
It is perfect for complex layouts like dashboards, galleries, or web pages with multiple sections.

🗣 Hinglish Tip: Flexbox ek direction (row/column) me layout karta hai, jabki Grid dono (row + column) me control deta hai.

To use Grid, set the parent container’s display to grid.

.container {
  display: grid;
}

All its direct children become grid items.

<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
</div>

Grid Container Properties

These control the layout of the grid.

PropertyDescriptionExample
displayDefines a grid containerdisplay: grid;
grid-template-columnsDefines the number & size of columnsgrid-template-columns: 200px 1fr 2fr;
grid-template-rowsDefines the number & size of rowsgrid-template-rows: 100px auto 50px;
gapSets spacing between rows and columnsgap: 10px;
row-gapVertical space between rowsrow-gap: 20px;
column-gapHorizontal space between columnscolumn-gap: 15px;
justify-itemsAligns items horizontally inside their celljustify-items: center;
align-itemsAligns items vertically inside their cellalign-items: center;
justify-contentAligns the grid as a whole horizontallyjustify-content: space-around;
align-contentAligns the grid as a whole verticallyalign-content: center;
grid-template-areasNames specific areas of the layoutgrid-template-areas: "header header" "sidebar main" "footer footer";

Example: Basic Grid Layout

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 100px 200px;
  gap: 10px;
}
.item {
  background-color: lightcoral;
  padding: 20px;
  font-size: 20px;
  text-align: center;
}

🗣 Hinglish Tip: fr ka matlab hota hai “fraction” — available space ka proportion.


🎯 Grid Item Properties

These properties control how individual grid items behave inside the grid.

PropertyDescriptionExample
grid-column-startDefines the column line where the item startsgrid-column-start: 1;
grid-column-endDefines the column line where the item endsgrid-column-end: 3;
grid-row-startDefines the row line where the item startsgrid-row-start: 1;
grid-row-endDefines the row line where the item endsgrid-row-end: 2;
grid-columnShorthand for start/endgrid-column: 1 / 3;
grid-rowShorthand for start/endgrid-row: 1 / 2;
justify-selfAligns item horizontally inside its grid celljustify-self: center;
align-selfAligns item vertically inside its grid cellalign-self: end;
grid-areaPlaces item in a named grid areagrid-area: header;

Example: Grid Item Properties

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: 100px 100px;
  gap: 10px;
}
.item1 {
  background-color: lightgreen;
  grid-column: 1 / 3;
}
.item2 {
  background-color: lightblue;
  grid-row: 1 / 3;
}
.item3 {
  background-color: lightcoral;
  justify-self: center;
}

🗣 Hinglish Tip: Grid me har cell ek coordinate system hota hai — column aur row numbers se items ko place karte hain.


💡 Shorthand Properties

ShorthandExpands ToExample
gridCombines grid-template and grid-auto propertiesgrid: auto / 1fr 1fr;
grid-columngrid-column-start + grid-column-endgrid-column: 1 / 3;
grid-rowgrid-row-start + grid-row-endgrid-row: 2 / 4;

Hinglish Tip: grid-template-areas se layout banana bahut visual aur readable hota hai — jaise table ke boxes.