Belajar Path File di HTML

Halo teman-teman!

Pada artikel kali ini, kita akan belajar tentang konsep baru yang penting untuk pemrograman web, yaitu file paths. Kalau sebelumnya kita sudah belajar tentang elemen gambar di HTML, kali ini kita akan memahami cara mengatur lokasi file gambar (atau file lainnya) agar bisa ditampilkan di website. Yuk, langsung mulai!

Apa Itu File Path?

Bayangkan kamu ingin menunjukkan lokasi spesifik suatu tempat ke temanmu. Misalnya, “Pergi ke Jakarta, masuk ke daerah Menteng, lalu cari Jalan Kebon Sirih.” Nah, komputer juga butuh cara untuk menemukan lokasi file, seperti gambar, dokumen, atau halaman HTML lainnya. Inilah yang disebut file path.

File path bisa diartikan sebagai “alamat” file atau folder di komputer kamu. Ada dua jenis file path yang sering digunakan:

  1. Absolute Path
    Alamat lengkap file yang dimulai dari “akar” (root) komputer. Misalnya:

    • Windows: C:\Project\Images\kucing.png
    • Mac: /Users/NamaUser/Project/Images/kucing.png
  2. Relative Path
    Alamat file yang relatif terhadap lokasi file tempat kamu menulis kode. Misalnya:

    • Dari file index.html ke gambar kucing.png di folder yang sama: ./kucing.png
    • Kalau gambarnya ada di folder lain: ./Images/kucing.png

Kenapa Harus Paham File Path?

Dalam web development, kita sering menggunakan relative path karena:

  • Lebih fleksibel: Kalau folder dipindahkan, file path ini tetap valid.
  • Lebih sederhana: Tidak perlu mengetik alamat lengkapnya.

Karakter Khusus di Relative Path

Ada beberapa simbol penting yang membantu kamu menavigasi file path:

  1. ./
    Artinya: “Di folder ini.”
    Contoh: ./kucing.png untuk file kucing.png di folder yang sama.

  2. ../
    Artinya: “Naik satu folder.”
    Contoh: Kalau file kucing.png ada di folder lain: ../Images/kucing.png.

Latihan: Menampilkan Gambar Hewan

Kamu punya folder dengan struktur seperti ini:

- Folder0
  - index.html
  - rabbit.png
  - Folder3
    - cat.png
- Folder1
  - fish.png
  - Folder2
    - bird.png
- dog.png

Tantangan: Tampilkan semua gambar hewan (rabbit, cat, dog, fish, bird) di halaman HTML! Berikut langkah-langkahnya:

  1. Rabbit
    Rabbit ada di folder yang sama dengan index.html. Jadi path-nya:

    <img src="./rabbit.png" alt="rabbit">
    
  2. Cat
    Cat ada di dalam Folder3. Jadi path-nya:

    <img src="./Folder3/cat.png" alt="cat">
    
  3. Dog
    Dog ada di luar folder Folder0. Kita perlu naik satu level:

    <img src="../dog.png" alt="dog">
    
  4. Fish
    Fish ada di Folder1, satu level di atas dan masuk ke folder tersebut:

    <img src="../Folder1/fish.png" alt="fish">
    
  5. Bird
    Bird ada di dalam Folder2, yang berada di dalam Folder1. Path-nya:

    <img src="../Folder1/Folder2/bird.png" alt="bird">
    

Tips untuk Pemula

  • Gunakan Preview di teks editor seperti VS Code untuk memeriksa apakah gambar tampil.
  • Kalau gambar tidak muncul, cek ulang path-nya dan perhatikan folder tempat file HTML kamu berada.
  • Selalu tambahkan atribut alt untuk memberi deskripsi gambar.

Kesimpulan:
Sekarang kamu sudah tahu cara menggunakan file paths untuk menampilkan file di HTML. Di pelajaran berikutnya, kita akan belajar cara menggunakan path ini untuk membuat website dengan banyak halaman. Jadi, jangan lewatkan ya!

Selamat mencoba!

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