Mengenal CSS Grid

Halo teman-teman!

Yuk kita bahas bareng tentang CSS Grid—fitur keren yang memudahkan kita menyusun layout dua dimensi di web. Dalam panduan ini, kita akan kenalan dari dasar sampai bisa mengatur ukuran dan posisi elemen dengan lebih leluasa. Kalau sebelumnya kamu sudah terbiasa dengan Flexbox, Grid ini akan terasa seperti pelengkap yang seru dan bermanfaat!

Grid sangat cocok buat kamu yang ingin membangun layout kompleks seperti dashboard, galeri foto, atau halaman dengan banyak elemen yang harus tersusun rapi, baik secara horizontal maupun vertikal. Yuk langsung kita mulai!

Apa Itu Grid?

Dengan menambahkan display: grid ke sebuah elemen, kita mengubahnya menjadi grid container. Jika Flexbox bekerja satu arah (horizontal atau vertikal), Grid bisa bekerja dua arah sekaligus—baik kolom maupun baris.

Grid hadir karena banyak desainer merasa kesulitan membangun layout kompleks hanya dengan Flexbox atau teknik lama seperti float. Grid ibarat sistem layout dua dimensi yang memberikan kontrol penuh terhadap penempatan dan ukuran elemen.

Kelebihan Grid:

  • Cocok untuk struktur besar seperti header, sidebar, dan konten utama.

  • Mendukung responsivitas secara alami.

  • Ukuran bisa fleksibel dengan fr, auto, minmax(), dan lainnya.

Contoh implementasi:

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

Grid bukan pengganti Flexbox, melainkan pelengkap. Gunakan Grid untuk menyusun struktur utama halaman, dan Flexbox untuk merapikan isi setiap bagian.

Mengatur Ukuran Grid

Menentukan ukuran grid sangat penting agar layout tetap konsisten dan responsif. Berikut beberapa teknik yang bisa digunakan:

1. Ukuran Tetap (px, rem)

Gunakan untuk elemen dengan dimensi konsisten.

.container {
  grid-template-columns: 400px 800px;
  grid-template-rows: 100px 200px;
}

2. Ukuran Otomatis (auto)

Menyesuaikan lebar atau tinggi sesuai isi konten.

.container {
  grid-template-columns: 200px auto;
  grid-template-rows: auto auto;
}

3. Satuan Fraksional (fr)

Proporsional terhadap ruang kosong yang tersedia.

.container {
  grid-template-columns: 1fr 2fr;
}

4. Fungsi minmax()

Menetapkan batas minimum dan maksimum.

.container {
  grid-template-columns: minmax(200px, 500px) auto;
}

5. Fungsi repeat()

Mempercepat penulisan kolom/baris berulang.

.container {
  grid-template-columns: repeat(8, 100px);
}

6. Auto Rows & Columns

Menentukan ukuran default untuk elemen yang melebihi jumlah baris/kolom yang ditentukan.

.container {
  grid-auto-rows: 50px;
  grid-auto-columns: 200px;
}

💡 Tips: Gunakan Chrome DevTools > Layout > Grid overlay untuk memvisualisasikan struktur grid secara real-time.

Menempatkan Elemen di Grid

Setelah grid terbuat, saatnya kita mengatur posisi elemen-elemen di dalamnya.

Terminologi Penting

  • Grid Container: elemen dengan display: grid

  • Grid Items: elemen anak dari container

  • Tracks: baris & kolom

  • Cells: pertemuan antara baris & kolom

  • Lines: batas antar cell

Penempatan Dasar

.item {
  grid-column: 2 / 4;
  grid-row: 1 / span 2;
}

Penempatan dengan grid-column dan grid-row

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

Shorthand:

.item {
  grid-column: 1 / 3;
  grid-row: 2 / 4;
}

Menggabungkan dengan Flexbox

.item {
  display: flex;
  justify-content: center;
  align-items: center;
}

grid-area untuk Penempatan Lengkap

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

Gunakan untuk menyederhanakan penempatan dengan satu deklarasi.

Mengubah Urutan Elemen

.item {
  order: 1;
}

Indeks Negatif

.item {
  grid-column-end: -1; /* akhir baris terakhir */
}

Menumpuk Elemen

.item1 {
  grid-area: 1 / 1 / 3 / 3;
}
.item2 {
  grid-area: 2 / 2 / 4 / 4;
  background-color: #ff660080; /* transparan */
}

Saatnya Mencoba!

Langsung aja praktik bikin layout dengan CSS Grid! Coba eksplorasi:

  • grid-area

  • minmax()

  • repeat()

  • Kombinasi dengan Flexbox

Untuk latihan seru, coba mainkan Grid Garden di: https://cssgridgarden.com

Dan jangan lupa aktifkan grid overlay di DevTools agar kamu bisa melihat struktur grid yang kamu bangun.

Selamat mencoba, dan terus eksplorasi Grid! Kalau kamu berhasil membuat layout yang keren, jangan ragu untuk membagikannya ke teman-teman. Belajar bareng selalu lebih seru! 🚀

Comments

Postingan Populer

Image

Calculate Business Loan Payments, Interest, and Total Cost Easily and Accurately Loan Amount (USD) Annual Interest Rate (%) Loan Term (Years) Extra Monthly Payment (USD) Reset Calculate Hi everyone! If you're looking for extra capital to launch a startup or expand your existing business, it's important to know how much you'll need to repay—monthly and overall. Don’t commit to a loan without understanding the full picture. That’s where our Online Business Loan Calculator comes in handy. This tool is designed for business owners, entrepreneurs, or anyone seeking a full breakdown of a loan. It helps you calculate monthly payments, total interest, repayment period, and overall cost automatically with just a few simple inputs. How to Use the Business Loan Calculator Fill in the following fields based on your situation: Loan Amount (USD): Ent...

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! Let’s explore CSS Grid together—a powerful feature that helps us build two-dimensional layouts on the web. In this guide, we’ll start from the basics and work our way up to controlling the size and position of elements with more flexibility. If you're already familiar with Flexbox, Grid will feel like a fun and useful complement! CSS Grid is perfect for building complex layouts like dashboards, photo galleries, or any page that requires a clean structure both horizontally and vertically. Let’s dive in! What is Grid? By adding display: grid to an element, we turn it into a grid container . While Flexbox works in one direction (horizontal or vertical), Grid works in both—columns and rows simultaneously. Grid was introduced to solve layout challenges that couldn’t easily be handled by Flexbox or older techniques like float . Think of Grid as a two-dimensional layout system that gives you full control over element placement and sizing. Why Use Grid? Great for la...