Terjemahan ini belum lengkap. Mohon bantu menerjemahkan artikel ini dari Bahasa Inggris.

hyperlink

HTML (Hypertext Markup Language) adalah kode yang digunakan untuk mengatur tata letak tampilan halaman web dan isinya. Contohnya, konten dapat disusun dalam bentuk sebuah paragraf, penomoran berbentuk titik, atau menampilkan gambar dan tabel. Seperti judulnya, artikel ini akan memberi anda pemahaman mendasar tentang HTML dan fungsinya.

Jadi sebenarnya apa itu HTML?

HTML bukan sebuah bahasa pemrograman; HTML adalah markup language (bahasa markup) bahasa yang mendefinisikan struktur kontenmu. HTML terdiri dari serangkaian elements, dimana kamu dapat gunakan untuk meletakkan/ membungkus konten didalamnya, sehingga mempunyai tampilan tertentu sesuai fungsi elemen nya. tags pembungkus dapat membuat sebuah kata atau gambar terhubung (hyperlink) ke konten lainnya, memiringkan tulisan, memperbesar atau memperkecil font, dan lain-lain. Sebagai contoh pada konten dibawah ini:

My cat is very grumpy

Kalau kita mau baris ini berdiri sendiri, kita bisa menetapkan bahwa ini adalah sebuah paragraf dengan menaruhnya diantara tags paragraf:

<p>My cat is very grumpy</p>

Anatomi elemen HTML

Ayo kita jelajahi elemen paragraf ini lebih jauh.

Bagian utama dari elemen kita adalah:

  1. Tag pembuka: Ini terdiri dari nama elemen (dalam hal ini, p), yang ada diantara kurung sudut pembuka dan penutup. Ini menyatakan awal elemen atau dimana elemen mulai bekerja — dalam hal ini dimana paragraf dimulai.
  2. Tag penutup: Ini mirip dengan tag pembuka, bedanya, di tag penutup ada garis miring di depan nama elemen. Ini menyatakan dimana elemen berakhir — dalam hal ini dimana paragraf berakhir. Lupa menulis tag penutup adalah salah satu kesalahan umum pemula yang dapat menyebabkan hasil yang aneh.
  3. Konten: Ini adalah konten elemen yang dalam hal ini hanya teks.
  4. Elemen: Elemen terdiri dari tag pembuka dan penutup serta konten.

Elemen juga bisa mempunyai atribut, yang terlihat seperti inwhich look like this:

Atribut mempunyai informasi ekstra tentang elemen. Atribut tidak tampil di konten aktual. Disini, class adalah nama atribut, dan editor-note adalah nilainya. Atribut class membantu membari penanda (identifier) pada elemen sehingga dapat digunakan untuk menargetkan elemen dengan informasi style dan lain-lain.

Sebuah atribut harus mempunyai:

  1. Jarak antara atribut dan nama elemen (atau atribut sebelumnya, kalau elemen sudah mempunyai atribut).
  2. Nama atribut, diikuti tanda sama dengan.
  3. Petik pembuka dan penutup diantara nilai atribut.        

Elemen Bersarang

Kita dapat menempatkan sebuah elemen kedalam elemen lainnya — ini dikenal dengan istilah bersarang. Jika kita ingin menetapkan tulisan "very grumpy" dengan tulisan very di tebalkan, kita dapat membungkus kata  "very" dalam elemen <strong>

<p>My cat is <strong>very</strong> grumpy.</p>

You do however need to make sure that your elements are properly nested: in the example above we opened the <p> element first, then the <strong> element, therefore we have to close the <strong> element first, then the <p>. The following is incorrect:

<p>My cat is <strong>very grumpy.</p></strong>

The elements have to open and close correctly so that they are clearly inside or outside one another. If they overlap like above, then your web browser will try to make a best guess at what you were trying to say, which can lead to unexpected results. So don't do it!

Empty elements

Some elements have no content, and are called empty elements. Take the <img> element we already have in our HTML:

<img src="images/firefox-icon.png" alt="My test image">

This contains two attributes, but there is no closing </img> tag, and no inner content. This is because an image element doesn't wrap content to have an effect on it. Its purpose is to embed an image in the HTML page in the place it appears.

Anatomy of an HTML document

That wraps up the basics of individual HTML elements, but they aren't very useful on their own. Now we'll look at how individual elements are combined to form an entire HTML page. Let's revisit the code we put into our index.html example (which we first met in the Dealing with files article):

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>My test page</title>
  </head>
  <body>
    <img src="images/firefox-icon.png" alt="My test image">
  </body>
</html>

Here we have:

  • <!DOCTYPE html> — the doctype. In the mists of time, when HTML was young (about 1991/2), doctypes were meant to act as links to a set of rules that the HTML page had to follow to be considered good HTML, which could mean automatic error checking and other useful things. However, these days no one really cares about them, and they are really just a historical artefact that needs to be included for everything to work right. For now, that's all you need to know.
  • <html></html> — the <html> element. This element wraps all the content on the entire page, and is sometimes known as the root element.
  • <head></head> — the <head> element. This element acts as a container for all the stuff you want to include on the HTML page that isn't the content you are showing to your page's viewers. This includes things like keywords and a page description that you want to appear in search results, CSS to style our content, character set declarations, and more.
  • <body></body> — the <body> element. This contains all the content that you want to show to web users when they visit your page, whether that's text, images, videos, games, playable audio tracks, or whatever else.
  • <meta charset="utf-8"> — this element sets the character set your document should use to UTF-8, which includes most characters from the vast majority of human written languages. Essentially it can now handle any textual content you might put on it. There is no reason not to set this, and it can help avoid some problems later on.
  • <title></title> — the <title> element. This sets the title of your page, which is the title that appears in the browser tab the page is loaded in. It is also used to describe the page when you bookmark/favourite it.

Images

Let's turn our attention to the <img> element again:

<img src="images/firefox-icon.png" alt="My test image">

As we said before, it embeds an image into our page in the position it appears. It does this via the src (source) attribute, which contains the path to our image file.

We have also included an alt (alternative) attribute. In this attribute, you specify descriptive text for users who cannot see the image, possibly because:

  1. They are visually impaired. Users with significant visual impairments often use tools called screen readers to read out the alt text to them.
  2. Something has gone wrong causing the image to not display. For example, try deliberately changing the path inside your src attribute to make it incorrect. If you save and reload the page, you should see something like this in place of the image:

The key words about alt text are "descriptive text". The alt text you write should provide the reader with enough information to have a good idea of what the image conveys. In this example, our current text of "My test image" is no good at all. A much better alternative for our Firefox logo would be "The Firefox logo: a flaming fox surrounding the Earth."

Try coming up with some better alt text for your image now.

Note: Find out more about accessibility at MDN's Accessibility landing page.

Marking up text

This section will cover some of the basic HTML elements you'll use for marking up text.

Headings

Heading elements allow you to specify that certain parts of your content are headings — or subheadings — of your content. In the same way that a book has a main title, chapter titles and subtitles, an HTML document can too. HTML contains six heading levels, <h1><h6> although you'll commonly only use 3–4 at most:

<h1>My main title</h1>
<h2>My top level heading</h2>
<h3>My subheading</h3>
<h4>My sub-subheading</h4>

Now try adding a suitable title to your HTML page just above your <img> element.

Paragraphs

As explained above, <p> elements are for containing paragraphs of text; you'll use these frequently when marking up regular text content:

<p>This is a single paragraph</p>

Add your sample text (you should have it from What should your website look like?) into one or a few paragraphs, placed directly below your <img> element.

Lists

A lot of the web's content is lists, and HTML has special elements for these. Marking up lists always consist of at least two elements. The most common list types are ordered and unordered lists:

  1. Unordered lists are for lists where the order of the items doesn't matter, like a shopping list. These are wrapped in a <ul> element.
  2. Ordered lists are for lists where the order of the items does matter, like a recipe. These are wrapped in an <ol> element.

Each item inside the lists is put inside an <li> (list item) element.

For example, if we wanted to turn the part of the following paragraph fragment into a list:

<p>At Mozilla, we’re a global community of technologists, thinkers, and builders working together ... </p>

We could modify the markup to this:

<p>At Mozilla, we’re a global community of</p>
    
<ul> 
  <li>technologists</li>
  <li>thinkers</li>
  <li>builders</li>
</ul>

<p>working together ... </p>

Try adding an ordered or unordered list to your example page.

Links are very important — they are what makes the web a web! To add a link, we need to use a simple element — <a> — the "a" being short for "anchor". To make text within your paragraph into a link, follow these steps:

  1. Choose some text. We chose the text "Mozilla Manifesto".
  2. Wrap the text in an <a> element, like so:
    <a>Mozilla Manifesto</a>
  3. Give the <a> element an href attribute, like so:
    <a href="">Mozilla Manifesto</a>
  4. Fill in the value of this attribute with the web address that you want the link to link to:
    <a href="https://www.mozilla.org/en-US/about/manifesto/">Mozilla Manifesto</a>

You might get unexpected results if you omit the https:// or http:// part, called the protocol, at the beginning of the web address. After making a link, click it to make sure it is sending you where you wanted it to.

href might appear like a rather obscure choice for an attribute name at first. If you are having trouble remembering it, remember that it stands for hypertext reference.

Add a link to your page now, if you haven't already done so.

Conclusion

If you have followed all the instructions in this article, you should end up with a page that looks like the one below (you can also view it here):

A web page screenshot showing a firefox logo, a heading saying mozilla is cool, and two paragraphs of filler text

If you get stuck, you can always compare your work with our finished example code on GitHub.

Here, we have only really scratched the surface of HTML. To find out more, go to our HTML Learning topic.

 

In this module

 

Tag Dokumen dan Kontributor

Kontributor untuk laman ini: adangwihanda, yasminzy, oktafh, bangyasri
Terakhir diperbarui oleh: adangwihanda,