Introduction

HTML (Hypertext Markup Language) is not a programming language; it is a markup language used to tell your browser how to structure the web pages you visit. It can be as complicated or as simple as the web developer wishes it to be. HTML consists of a series of elements, which you can use to enclose, wrap, or mark up different parts of the content into a hyperlink to link to another page on the web, italicize words, and so on. For example, take the following line of content:

1 | 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 a paragraph < p > element:

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

Anatomy of an HTML element

Let's explore our paragraph element a bit further:

Example of paragraph tags.

The main parts of our element are:

  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 start of the paragraph is.
  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 elements ends -- in this case where the end of the end of the paragraph is. Failing to include a closing tag is a common beginner error and can lead to strange results.
  3. The content: This is the content of the element, which in this case is just text.
  4. The element: The opening tag plus the closing tag plus the content equals the element.

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:

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

Block versus inline elements

There are two important categories of elements in HTML which you should know about. They are block-level elements and inline elements.

Take the following example:

1 | <em>first</em><em>second</em><em>third</em> 2 | 3 | <p>fourth</p><p>fifth</p><p>sixth</p>

<em> is an inline element, so as you can see below, the first three elements sit on the same line as one another with no space in between. On the other hand, <p> is a block-level element, so each element appears on a new line, with space above and below each (the spacing is due to default CSS styling that the browser applies to paragraphs).

Empty elements

Not all elements follow the above pattern of opening tag, content, closing tag. Some elements consist only of a single tag, which is usually used to insert/embed something in the document at the place it is included. For example, the <img> element embeds an image file onto a page in the position it is included in:

1 | <img src=" https://raw.githubusercontent.com/mdn/beginner-html-site/gh-pages/images/firefox-icon.png">

This would output the following on your page:

firefox-icon

Attributes

Elements can also have attributes, which look like this:

Attribute example

Attributes contain extra information about the element which you don't want to appear in the actual content. In this case, the class attribute allows you to give the element an identifying name that can be later used to target the element with style information and other things.

An attribute should have:

  1. A space between it and the element name (or the previous attribute, if the element already has one or more attributes).
  2. The attribute name, followed by an equal sign.
  3. An attribute value, with opening and closing quote marks wrapped around it.

Boolean attributes

You'll sometimes see attributes written without values — this is perfectly allowed. These are called boolean attributes, and they can only have one value, which is generally the same as the attribute name. As an example, take the disabled attribute, which you can assign to form input elements if you want them to be disabled (greyed out) so the user can't enter any data in them.

1 | <input type="text" disabled>

Omitting quotes around attribute values

When you look around the World Wide Web, you'll come across a number of strange markup styles, including attribute values without quotes. This is allowable in certain circumstances, but will break your markup in others. For example, if we revisit our link example from earlier, we could write a basic version with only the href attribute, like this:

1 | <a href=https://www.mozilla.org/> favorite website</a>

However, as soon as we add the title attribute in this style, things will go wrong:

1 | <a href=href=https://www.mozilla.org/ title=The Mozilla homepage>favorite website</a>

At this point the browser will misinterpret your markup, thinking that the title attribute is actually three attributes — a title attribute with the value "The", and two boolean attributes, Mozilla and homepage. This is obviously not what was intended, and will cause errors or unexpected behavior in the code, as seen in the live example below. Try hovering over the link to see what the title text is!

Our advice is to always include the attribute quotes — it avoids such problems, and results in more readable code too.

Single or double quotes?

In this article you'll notice that the attributes are all wrapped in double quotes. You might however see single quotes in some people's HTML. This is purely a matter of style, and you can feel free to choose which one you prefer. Both the following lines are equivalent:

1 | <a href="http://www.example.com">A link to my example.</a> 2 | 3 | <a href='http://www.example.com'>A link to my example.</a>

You should however make sure you don't mix them together. The following will go wrong!

<a href="http://www.example.com'>A link to my example.</a>

If you've used one type of quote in your HTML, you can include the other type of quote inside your attribute values without causing any problems:

1 | <a href=href="http://www.example.com" title="Isn't this fun?"> A link to my example.</a>

However if you want to include a quote within the quotes where both the quotes are of the same type(single quote or double quote), you'll have to use HTML entities for the quotes. For example, this will break:

1 | <a href='http://www.example.com' title='Isn't this fun?'> A link to my example.</a>

So you need to do this:

1 | <a href='http://www.example.com' title='Isn&#39;t this fun?'> A link to my example.</a>

Or this:

1 | <a href="http://www.example.com" title="Isn't this fun?"> A link to my example.</a>

Reference