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

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

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