Belajar Nesting dan Indentasi di HTML (Membuat List Bertingkat dengan Rapi)

Nesting dan Indentasi di HTML

Halo teman-teman!

Di pelajaran sebelumnya, kita sudah belajar membuat ordered list dan unordered list. Nah, kali ini kita akan memperdalamnya dengan nested list dan memahami pentingnya indentasi. Ini langkah penting agar kode kita rapi dan mudah dibaca, terutama saat membuat struktur list yang lebih kompleks.

Apa Itu Nested List?

Nested list adalah list yang berada di dalam list lain. Dengan kata lain, kalian bisa membuat list di dalam sebuah item list. Contohnya seperti ini:

Contoh Nested List:

  • Item A
  • Item B
    • Subitem B1
    • Subitem B2
  • Item C

Dengan nested list, kalian bisa membuat hierarki dalam daftar, seperti daftar bahan atau langkah-langkah yang saling berkaitan.


Cara Membuat Nested List

1. Struktur Dasar

Untuk membuat nested list, gunakan <ul> (unordered list) atau <ol> (ordered list) di dalam sebuah <li> (list item). Contohnya:

<ul>
    <li>Item A</li>
    <li>Item B
        <ul>
            <li>Subitem B1</li>
            <li>Subitem B2</li>
        </ul>
    </li>
    <li>Item C</li>
</ul>

Hasil di browser:

  • Item A
  • Item B
    • Subitem B1
    • Subitem B2
  • Item C

2. Menggabungkan Ordered dan Unordered List

Kalian juga bisa menggabungkan <ol> dan <ul> untuk membuat struktur yang lebih kompleks:

<ol>
    <li>Langkah 1</li>
    <li>Langkah 2
        <ul>
            <li>Detail A</li>
            <li>Detail B</li>
        </ul>
    </li>
    <li>Langkah 3</li>
</ol>

Hasil di browser:

  1. Langkah 1
  2. Langkah 2
    • Detail A
    • Detail B
  3. Langkah 3

Pentingnya Indentasi

Indentasi membuat kode lebih rapi dan mudah dibaca. Contoh tanpa indentasi:

<ul><li>Item A</li><li>Item B<ul><li>Subitem B1</li><li>Subitem B2</li></ul></li><li>Item C</li></ul>

Dibandingkan dengan indentasi yang benar:

<ul>
    <li>Item A</li>
    <li>Item B
        <ul>
            <li>Subitem B1</li>
            <li>Subitem B2</li>
        </ul>
    </li>
    <li>Item C</li>
</ul>

Kenapa penting?

  • Kode lebih mudah dibaca dan dipahami, terutama untuk nested list.
  • Membantu menemukan kesalahan (debugging) saat ada masalah di kode.

Contoh Proyek: Nested List untuk Daftar Belanja

Kita akan membuat nested list untuk daftar belanja:

<!DOCTYPE html>
<html>
<head>
    <title>Daftar Belanja</title>
</head>
<body>
    <h1>Daftar Belanja</h1>
    <ul>
        <li>Bahan Makanan
            <ul>
                <li>Beras</li>
                <li>Sayuran</li>
                <li>Buah-buahan</li>
            </ul>
        </li>
        <li>Kebutuhan Rumah Tangga
            <ul>
                <li>Sabun</li>
                <li>Sampo</li>
            </ul>
        </li>
        <li>Minuman
            <ul>
                <li>Teh</li>
                <li>Kopi</li>
                <li>Air Mineral</li>
            </ul>
        </li>
    </ul>
</body>
</html>

Hasil di browser:

  • Bahan Makanan
    • Beras
    • Sayuran
    • Buah-buahan
  • Kebutuhan Rumah Tangga
    • Sabun
    • Sampo
  • Minuman
    • Teh
    • Kopi
    • Air Mineral

Tips untuk Nested List

  1. Perhatikan penutup tag: Setiap tag pembuka harus memiliki tag penutup yang tepat.
  2. Indentasi otomatis: Gunakan fitur di teks editor seperti Visual Studio Code untuk otomatis merapikan indentasi (Ctrl+S atau Cmd+S).
  3. Periksa hasil secara berkala: Gunakan fitur preview untuk melihat hasil kode di browser.

Kesimpulan
Nested list membantu kita membuat daftar yang lebih terstruktur dan kompleks. Dengan indentasi yang benar, kode kalian akan lebih rapi dan mudah dipahami. Selanjutnya, kita akan belajar tentang Anchor Element untuk membuat hyperlink di website. Sampai jumpa di pelajaran berikutnya! 😊

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

Image

If you’ve ever struggled to align elements neatly in CSS—or tried to make a layout look good on both desktop and mobile— Flexbox CSS is your new best friend. Short for "Flexible Box Layout," Flexbox is a layout module in CSS3 that provides an efficient way to distribute space and align items in a container, even when their sizes are dynamic. In this flexbox tutorial for beginners , we’ll guide you step-by-step through what Flexbox is, why it’s essential for responsive design, and how to use it to build clean, adaptable layouts. By the end of this guide, you’ll be able to use Flexbox confidently to build elements like navigation bars, feature sections, and even a CSS flexbox pricing table example . Whether you're a self-taught developer, a student, or someone shifting into a front-end career, Flexbox will become one of your most powerful tools in building responsive, user-friendly websites. Before diving into the code, if you're not yet familiar with CSS fundamenta...

Image

Hello everyone! In our last post about Grid Layouts , we explored how to create a basic grid structure in CSS. Now, let’s take that one step further. Today, we're diving into Grid Sizing — how to size columns and rows inside your grid layout. By the end of this post, you’ll know exactly when to use fixed units like px , flexible units like fr , and responsive functions like minmax() . We’ll even explore some cool developer tools and an interactive test. Ready? Let’s go! What is Grid Sizing? Grid Sizing refers to how we control the size of the rows and columns inside our CSS Grid layout. Depending on what kind of content you're building (e.g., dashboards, cards, galleries), you'll want your grid to behave differently. In CSS Grid, we use properties like: grid-template-rows grid-template-columns grid-auto-rows grid-auto-columns Let’s break these down together. 1. Fixed Sizes with px and rem You can define static sizes for rows and columns using pixels ...