Grid Item Placement - How to Control Layouts in CSS Grid

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 with display: grid
Grid Item Each child element inside the grid
Track A row or column
Cell The smallest unit, where rows and columns intersect
Grid Line The lines that separate rows and columns

Image Illustration: Grid Anatomy
Description: A visual grid layout showing labeled rows, columns, tracks, and grid lines.


Step 1: Setting Up the Grid Container

Let’s create a basic layout:

<div class="grid-container">
  <div class="cowboy"></div>
  <div class="astronaut"></div>
  <div class="book"></div>
</div>
.grid-container {
  display: grid;
  height: 100vh;
  grid-template-columns: 1fr 1fr 1.5fr;
  grid-template-rows: 1fr 1fr;
  gap: 3rem;
}

This creates a 3-column, 2-row layout. The third column is wider than the others, and there’s a 3rem gap between cells.

Step 2: Grid Item Defaults

By default, grid items fill cells in document order, one per cell.

Image Illustration: Default Placement
Description: Cowboy in cell 1, Astronaut in cell 2, Book in cell 3.

But what if we want to customize placement? Let’s explore that now.

Step 3: Using grid-column to Span Columns

To make the first item (.cowboy) span across two columns:

.cowboy {
  background-color: #60a5fa;
  grid-column: span 2;
}

This makes it span from line 1 to 3 — two columns wide.

Tip: You can inspect your grid using Chrome DevTools → Select grid element → Click the grid badge.

Image Illustration: Cowboy spans two columns
Description: Blue cowboy box stretching across first two columns.

Step 4: Line-Based Placement with grid-column-start and grid-column-end

You can also use line numbers:

.cowboy {
  grid-column-start: 2;
  grid-column-end: 4;
}

Or use negative numbers from the end:

.cowboy {
  grid-column-end: -1;
}

Using negative lines helps when your grid is dynamic.

Step 5: Spanning Rows with grid-row

Let’s say the book icon should stretch two rows:

.book {
  background-color: #f97316cc;
  grid-row: span 2;
}

This places .book to span vertically, across both rows.

Image Illustration: Book spans two rows
Description: Orange book icon stretches from top to bottom.

Step 6: Positioning the Astronaut Box

Let’s now place .astronaut below .cowboy and have it span two columns:

.astronaut {
  background-color: #22c55e;
  order: 1;
  grid-column: span 2;
}

Alternatively, you can use explicit placement:

.astronaut {
  grid-column-start: 1;
  grid-column-end: 3;
  grid-row-start: 2;
  grid-row-end: 3;
}

Or use grid-area shorthand:

.astronaut {
  grid-area: 2 / 1 / 3 / 3;
}

Step 7: Centering Content with Flexbox

Grid controls layout, but Flexbox is perfect for centering.

.cowboy, .astronaut, .book {
  display: flex;
  justify-content: center;
  align-items: center;
}

Image Illustration: Centered emojis
Description: Each emoji is centered inside its box.

Using Flexbox like this helps keep content aligned within each grid item.

Bonus: Overlapping Grid Items

Unlike Flexbox, CSS Grid allows item overlap.

Example: let’s overlay .book on top of .cowboy and .astronaut.

.book {
  background-color: #f97316cc;
  grid-area: 1 / 2 / 3 / 4;
}

Image Illustration: Overlapping grid
Description: Transparent book box sits on top of blue and green boxes.

Practice Time: Grid Garden

Now that you’ve got the hang of placing items, it’s time to play a game.

Try out Grid Garden by Thomas Park. It’s like Flexbox Froggy — but for CSS Grid.

28 levels of grid-placement practice await. Go water the carrots and poison the weeds.

Learn More

What’s Next?

In the next lesson, we’ll start building our final CSS Grid project — a fully responsive layout using everything we’ve learned.

You can also check out the Project: Mondrian Painting to see how to recreate a famous artwork with CSS Grid.

See you in the next tutorial!

Comments

Postingan Populer

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...

Halo teman-teman, apa kabar kalian? Jika kalian punya blog atau website dan ingin banget meningkatkan penghasilan lewat Google AdSense, kalian berada di tempat yang tepat. Dalam artikel yang sangat panjang ini, kita akan kupas tuntas gimana caranya menaikkan Cost Per Click (CPC) dan Click-Through Rate (CTR) , dua hal penting yang sangat memengaruhi besaran uang yang bisa kalian dapatkan dari iklan di blog. Kita bakal bahas strategi-strategi jitu supaya blog kalian makin menarik di mata pengiklan. Dengan begitu, nilai klik (CPC) bisa melonjak, dan persentase pengunjung yang mengklik iklan (CTR) juga ikut naik. Kita akan bedah kenapa kedua hal ini krusial, gimana mengoptimalkan konten, cara memilih topik yang pas, sampai gimana menempatkan iklan dengan taktik yang tepat. Nggak ketinggalan, kita juga akan kupas soal SEO—karena kalau blog kalian gampang ditemukan di mesin pencari, jumlah pengunjung pasti meningkat. Lebih banyak pengunjung = lebih besar peluang klik = pendapatan blog yan...

Image

Halo teman-teman! Bagaimana kabarnya? Pada artikel sebelumnya kita sudah membahas tentang Mengenal CSS Selector (Cara Mengatur Tampilan Website dengan Tepat) . Di sana, kita belajar bagaimana menargetkan Element tertentu di dalam HTML menggunakan CSS Selector . Teknik ini penting supaya kita bisa mengatur tampilan setiap Element website sesuai keinginan. Nah, kali ini kita akan melanjutkan pembahasan ke topik yang tidak kalah seru, yaitu CSS Colours atau dalam bahasa Indonesia sering juga disebut warna CSS . Walau kedengarannya sederhana, penguasaan warna di CSS sangatlah berguna untuk membuat tampilan website menjadi lebih menarik. Jika sebelumnya kita hanya sempat menyinggung sedikit soal pengaturan warna di CSS, sekarang kita akan mendalaminya hingga tuntas! Jika kamu siap, mari kita mulai petualangan warna kita di dalam dunia CSS!