Introduction

HTML (Hypertext Markup Language) is the code that is used to structure a web page and its content. For example, content could be structured within a set of paragraphs, a list of bulleted points, or using images and data tables. As the title suggests, this article will give you a basic understanding of HTML and its functions.

What is HTML?

HTML is a markup language that defines the structure of your content. HTML consists of a series of elements, which you use to enclose, or wrap different parts of the content to make it appear a certain way. The enclosing tags can make a word or image hyperlink to somewhere else, can italicize words, can make the font bigger or smaller, and so on. For example, take the following line of content:

My cat is very grumpy

If we wanted the line to stand by itself, we could specify that it is a paragraph by enclosing it in paragraph tags:

<p>My cat is very grumpy</p>
Anatomy of an HTML element

Let's explore this paragraph element a bit further.

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

HTML element consists of main parts are as follows:

  1. The opening tag: This consists of the name of the element (in this case, p), wrapped in opening and closing angle brackets. This states where the element begins or starts to take effect, in this case where the paragraph begins.
  2. The closing tag: This is the same as the opening tag, except that it includes a forward slash before the element name. This states where the element ends, in this case where the paragraph ends. Failing to add a closing tag is one of the standard beginner errors and can lead to strange results.
  3. The content: Is the content of the element, which in our case is just text.
  4. The element: The opening tag, the closing tag, and the content together comprise the element.

HTML elements can also have attributes that look like the following:

<p class="editor-note">My cat is very grumpy</p>

Attributes contain extra information about the element that you don't want to appear in the actual content. Here, class is the attribute name and editor-note is the attribute value. The class attribute allows you to give the element a non-unique identifier that can be used to target it (and any other elements with the same class value) with style information and other things.

An attribute should always have the following:

Note: Simple attribute values that don't contain ASCII whitespace (or any of the characters " ' ` = < >) can remain unquoted, but it is recommended that you quote all attribute values, as it makes the code more consistent and understandable.

Nesting elements

You can put elements inside other elements too, this is called nesting. If we wanted to state that our cat is very grumpy, we could wrap the word "very" in a <strong> element, which means that the word is to be strongly emphasized:

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

You do however need to make sure 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> element. 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 as shown above, then your web browser will try to make the 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 as an example:

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

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

HTML document

HTML document wraps up the basics of individual HTML elements, but they aren't handy on their own, they are combined to form an entire HTML page. Here we have the following:

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>My test page</title>
  </head>
  <body>
    <p>My cat is very grumpy</p>
  </body>
</html>
Headings

Heading elements allow you to specify that certain parts of your content are headings or subheadings. HTML contains 6 heading levels: <h1> is the highest section level and <h6> is the lowest, although you'll commonly only use 3 to 4 at most:

<!-- 4 heading levels: -->

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

Note: Anything in HTML between <! – – and – – > is an HTML comment. The browser ignores comments as it renders the code. In other words, they are not visible on the page - just in the code. HTML comments are a way for you to write helpful notes about your code or logic.

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

Lists in HTML are used to represent an item in a list and 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 and these wrapped in an <ul> element.
  2. Ordered lists are for lists where the order of the items does matter and these wrapped in an <ol> element.

Each item inside the lists is put inside an <li> 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>
Reference

All the documentation in this page is taken from MDN and it is only covering the basics of HTML, to find out more go to Learning HTML topic.