CSS Grid as a Two-Dimensional Layout System

Hi everyone!
If you’ve just finished learning about Flexbox, congrats! Flexbox is a great tool for arranging items in a single direction—either horizontally or vertically. But what if we want to build something more complex, like a layout with both columns and rows? That’s where CSS Grid becomes your new best friend.

In this article, we’ll explore the basics of Grid, compare it with Flexbox, and build a fun layout like a chessboard—all with just a few lines of code. Let’s jump in!

What is CSS Grid?

CSS Grid is a two-dimensional layout system that lets you organize content on a web page like pieces in a puzzle. Grid works in two directions—rows and columns—which makes it perfect for complex layouts like galleries, dashboards, or even news websites.

Image Illustration: A responsive weather forecast layout for Bern using grid-based sections.

With Grid, you can:

  • Create flexible column and row structures

  • Position items precisely on the page

  • Maintain consistent spacing between items

  • Build layouts that stay neat across different screen sizes (responsive design!)

CSS Grid vs Flexbox: What's the Difference?

You might wonder, "Isn't Flexbox also for layout? What's the difference?"

Let’s break it down:

System Dimension Best For
Flexbox 1D Arranging elements in a single line
Grid 2D Organizing layouts with rows + columns

Imagine you're placing a header, sidebar, main content, and footer. With Flexbox, you'd need nested divs and a bit of manual tweaking. With Grid, you can define it all directly in one container!

Image Illustration: Diagram showing the difference between 1D (Flexbox) and 2D (Grid) layout directions

Real-World Use: Combine Flexbox & Grid

Most developers don’t choose one over the other—they combine both!

Examples:

  • Use Grid to define the major layout areas (header, main, footer)

  • Use Flexbox inside those areas to align items in a row or column

"The more skills we carry, the more flexible we become." — Wise words from Grandpa :)

Try It Yourself: Grid vs Flexbox Example

To get a hands-on feel for the differences, check out this demo: dindindesign.com/p/css-grid-vs-flexbox.html

When you resize the browser:

  • Grid keeps rows and columns aligned perfectly

  • Flexbox allows flexibility but items might not line up consistently

Image Illustration: Screenshot comparing responsive layouts using Grid vs Flexbox

Let's Build Our First Grid!

Let’s start small with a simple example. Say we have the following HTML:

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

CSS Setup:

.container {
  display: grid;
  grid-template-columns: 1fr 2fr;
  grid-template-rows: 1fr 1fr;
  gap: 10px;
}

Explanation:

  • display: grid → Activates grid layout

  • grid-template-columns: 1fr 2fr → First column takes 1 part, second takes 2

  • grid-template-rows: 1fr 1fr → Two equal-height rows

  • gap: 10px → Adds spacing between cells without needing margin

Image Illustration: A 2x2 grid layout where one column is twice as wide

Just like that, we’ve created a neat layout with minimal code!

Fun Challenge: Build a Chessboard!

Time to level up! Let’s build an 8x8 chessboard using CSS Grid. This challenge also introduces repeat() and paves the way for Grid Sizing.

HTML Structure:

Already pre-filled with 64 divs:

<div class="chessboard">
  <div class="white"></div>
  <div class="black"></div>
  <!-- 62 more -->
</div>

CSS Styling:

.chessboard {
  display: grid;
  grid-template-columns: repeat(8, 100px);
  grid-template-rows: repeat(8, 100px);
  width: 800px;
}

.white {
  background-color: #f0d9b5;
  width: 100px;
  height: 100px;
}

.black {
  background-color: #b58863;
  width: 100px;
  height: 100px;
}

Image Illustration: A modern chessboard layout with neutral square colors

What this does:

  • Creates 8 equal columns and rows

  • Defines each square as 100x100px

  • Colors them accordingly

If your board stretches across a large screen, you can center it with margin: auto or tweak display styles for polish.

Why Does CSS Grid Matter?

CSS Grid lets us:

  • Improve layout consistency

  • Reduce unnecessary wrapper divs

  • Build responsive layouts without heavy libraries

  • Combine with Flexbox for full layout control

For modern web design, mastering Grid is a superpower that makes your pages cleaner, more efficient, and visually awesome.

Trusted source: MDN Web Docs on CSS Grid

Ready for the Next Adventure?

We’ve just scratched the surface of CSS Grid’s power. In the next post, we’ll dive into Grid Sizing—how to control column and row sizes flexibly, proportionally, or automatically.

Grid Sizing - How to Size Columns and Rows

Keep experimenting, keep building. Because as the old saying goes, skills are the only luggage that never weighs you down.

See you in the next post, friends!

Keywords: CSS Grid, display grid, CSS Grid tutorial, Grid vs Flexbox, responsive layout, web design, chessboard grid, two-dimensional layout, grid-template-columns, grid-template-rows

CSS Grid

Comments

Postingan Populer

Image

Halo teman-teman! Aku mau cerita tentang salah satu project seru yang aku kerjakan: halaman marketing untuk SEO Panel . Aku nggak bikin seluruh platform-nya, tapi fokus di bagian depan website supaya setiap elemen dan kontennya bisa langsung menyampaikan nilai produk dengan jelas dan mudah dipahami. Awal Proyek SEO Panel punya platform SEO all-in-one yang keren banget di belakang layar, dengan fitur-fitur lengkap buat bantu pengguna meningkatkan peringkat website. Tapi mereka butuh halaman depan yang nggak kalah kuat: tampilannya harus menarik, langsung menjelaskan manfaat, dan bikin pengunjung mau mencoba layanan. Aku mulai dengan memahami audiens mereka: siapa penggunanya, masalah apa yang sering ditemui di SEO, dan apa yang bikin mereka tertarik. Dari situ aku bikin alur konten dan layout yang gampang ditangkap pengunjung dalam beberapa detik pertama. Proses Desain Untuk halaman ini aku pakai WordPress dan Elementor supaya desainnya fleksibel dan nggak perlu coding dari awal. Fokus...

Image

Superingan is a Blogger template designed with simplicity and functionality in mind, offering a clean and user-friendly platform for professional websites. This theme is perfect for various website types, including personal blogs, online magazines, news websites, and portfolios. Features: Responsive Design: Your website will look its best across all devices, from desktops and laptops to tablets and smartphones. Customizable Colors: Easily change the template's colors to match your branding or preferences. Custom Headers: Upload your own header image to add a personal touch to your website. Custom Menus: Create and manage your website's navigation menus with ease. Widget Areas: Add widgets to display additional content and features on your website's sidebar. Clean HTML & CSS Code: The template is built with clean and well-structured code, making it easy to learn and customize further. Superingan is an ideal choice for those seeking a professional website with a sim...

Hi everyone! Welcome back to our CSS Grid series on Dindin Design! In the previous article, we explored how to size our grid columns and rows using fr , px , minmax() , and more. Now that we know how to set up a grid, it’s time to take it to the next level — by learning how to place elements inside the grid. In this article, we’ll learn Grid Item Placement — how to position and span items across your grid using properties like grid-column , grid-row , and grid-area . You’ll also learn how Flexbox and Grid can work together, and how to create overlapping layouts. What You’ll Learn Key Grid terminology: tracks, lines, cells, and items How to place grid items using grid-column , grid-row , and grid-area How to span items across multiple tracks Centering content inside grid items using Flexbox How to create overlapping layouts Grid Basics Refresher Before diving into placement, let’s refresh a few terms: Term Description Grid Container The parent element wi...