HTML & CSS - Mastery / CSS Grid System

Creating a Grid Container

🤝 Grid in Action

To begin using CSS Grid, you must define a grid container using the display: grid; property. This makes its direct children grid items that can be placed into rows and columns.


🧱 Master Code block to be used further

<body>
  <div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
  </div>
</body>
.container {
  display: grid;
  gap: 10px;
  padding: 10px;
}

.item {
  background: steelblue;
  color: white;
  text-align: center;
  padding: 20px;
  font-size: 20px;
}

🧭 Defining Columns and Rows

Use the following properties to define the grid structure:

🔹 grid-template-columns

Defines how many columns the grid should have, and their widths.

🔹 grid-template-rows

Defines the number of rows and their heights.


🧠 Summary

Property

Purpose

display: grid;

Turns an element into a grid container

grid-template-columns

Defines number and size of columns

grid-template-rows

Defines number and size of rows

fr

Creates flexible, proportion-based columns