HTML Syntax

An HTML element is a component of an HTML document and instructs the browser how to display content.

For example, the title element represents the document's title, a table element represents an HTML table, and a footer element represents a footer section of a web page.

To be able to create elements, we need to know about syntax. Or how we write HTML.

DOCUMENTS

Newspapers, brochures, and insurance forms are different kinds of documents that we use in everyday life. We commonly see some kind of structure to the content of these documents.

This structure comes from the use of hierarchical headings and subheadings, main paragraph text, lists, and tables.

Common documents

In the example document below, we have a main heading at the top, followed by some introductory text.

Each section has a subheading, which is expanded upon with short paragraphs.

The text is clearly separated and given structure.

STRUCTURING A DOCUMENT

So, if I take this same formatted text and add it to the embedded CodePen below, what do you think might happen?

Will it display in the browser already formatted like the example document above?

Try copying and pasting the text into the CodePen below to find out.

When you save, you will notice that we have the same content, but it is not formatted.

The text is squashed together, and the bold styles and font sizings from headings and sub-headings are absent.

The reason for this is that we have not communicated the structure of the HTML document to our browser. We have not made clear how we want our content to be displayed.

To format the text, we use HTML tags.

TAGS VS ELEMENTS

Tags are a fundamental characteristic of HTML.

Each markup tag is composed of angled brackets surrounding a keyword. Like <html>, <head>, <body>, <title>, <p>, and so on.

The terms HTML element, and HTML tag, are often used interchangeably. You may hear elements referred to as tags, and tags referred to as elements.

An HTML tag marks the start or end of an HTML element.

An HTML element is the collection of opening and closing tags, attributes, and any content between the tags.

CASE INSENSITIVITY IN HTML

In HTML, tag and attribute names are not case-sensitive (but most attribute values are).

This means that <H1> and <h1> defines the same thing in HTML - which is the top-level heading element.

Even though the browser will recognize both, we should write our HTML elements in lowercase, as recommended by W3C - the main standards organization for the web.

ADDING HTML TAGS

The tags that we will add to the text pasted into the CodePen above will be the paragraph tag <p></p>, which we have seen in previous sections. Then three levels of heading tags:

  • <h1></h1>
  • <h2></h2>
  • <h3></h3>
<!--The elements we will use:-->
<h1></h1>
<h2></h2>
<h3></h3>
<p></p>

Paragraphs

The paragraph element is for displaying generic text on a web page. Paragraphs are defined with the <p> tag.

<p>This is a paragraph</p>

This is a paragraph

Paragraphs in earlier versions of HTML

In HTML4, it was enough to initiate a new paragraph using just the opening <p> tag. Most browsers will still display HTML correctly even if you do forget to include the closing tag.

<p>This is an old paragraph

Forgetting to include the closing tag can produce unexpected results or errors.

Headings

We use the <h1></h1> element to indicate the most important, or highest-level heading on the page.

<h1> has the largest default font size (though we can change this with CSS). So it is ideal for main headings.

There are a total of six heading levels available to us in HTML. These go from <h1> to <h6>, with the <h6> element being the lowest-level heading with the smallest default font size.

By default, browsers display headings with larger font sizes and bolder font weights than standard text.

You will find that you will commonly use <h1> through <h3> headings. Then occasionally, you might need to use an <h4>.

Using <h5> and <h6> is pretty rare.

If you add an <h5> or <h6>, you will see the default font size is smaller than the paragraph's default font size!

FORMATTING TEXT WITH HTML TAGS

Go back to the text pasted into CodePen, and add some <h1></h1> tags to the text of Main Heading.

Remember that these tags go on either side of the text.

The opening tag has the element name enclosed in angled brackets, and the closing tag has that too, plus a forward slash (/) before the element name.

The heading text goes between these.

<h1>Main Heading</h1>

Next, we will add an <h2> subheading. Again, we will ensure that we place opening and closing tags on either side of the text that reads Subheading.

<h2>Subheading</h2>

Now we have our last two subheadings, which are slightly smaller. For these we will use <h3> tags.

<h3>Another subheading</h3>

Lastly, we have seven paragraphs, and the text for each of these needs to be enclosed by <p> tags.

Again, we will need an opening tag preceding each section of paragraph text and a closing tag following each section of paragraph text.

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc bibendum in tortor vitae pharetra. </p>

So there you have it. We have taken a simple document and reproduced its structure with HTML. Once you have marked up the text correctly, it should look something like this:

THE GENERAL RULE OF HTML SYNTAX

Two things that I do want you to take away is that first, we use HTML to structure our content, and second, there is something that is a little bit of a pattern or general rule of HTML.

Below I am going to summarise what we have learned above:

  • An HTML element usually consists of an opening tag and a closing tag, with the content inserted in between those.
  • The opening tag gives the browser an instruction, and the closing tag informs the browser that that instruction has ended.
  • Each tag is made up of angled brackets with the name of the element inside.

NOTE: The closing tag is almost the same as the opening tag but has one key difference. That is that we put a forward slash in the closing tag before the name of the element.

So we can say that a general rule of HTML is this:

An HTML element has an opening tag and a closing tag that surround content.

Tags then, in a way, act as containers and tell us something about the information that lies between them.

If we have <h1></h1> opening and closing tags with content in between, we can safely assume that the content is the main heading of the page.

SELF-CLOSING TAGS

This next part is confusing. In addition to elements that follow the general rule of HTML that we have just defined, some elements are made of a single tag.

Usually, these would be elements that have no inner text content to be wrapped.

We call these empty elements, and these are made of self-closing tags.

Images

An example of an empty element would be the <img> image element.

<img src="https://images.unsplash.com/photo" alt="Sourced from Unsplash">

*The blue text is the attribute name, and the pink text is the attribute value.

From Unsplash

We see that in the example code above that the image element contains other details that we have not yet seen.

These are HTML attributes, which give us extra information about elements.

In this case, src refers to the source location of the image.

The alt attribute provides alternative text, which is used if the image cannot be viewed.

There are also a few empty elements that can help us to visually divide up web pages. For example, The <br> tag inserts a line break, and the <hr> tag creates a horizontal rule or line.

Since both of these are empty elements, there is no need for either to have a corresponding closing </br> or </hr> tag.

We do not need to worry about self-closing tags too much right now.

I just wanted to show you that these do exist, so that you know that some HTML elements are written in an alternative syntax.

To summarise, empty elements are usually those which do not wrap text content. We will cover them in greater detail later on in this HTML section.

The HTML elements that we will learn initially are going to follow the general rule that we have established. That we will have some content surrounded by two tags. An opening tag and a closing tag.

DEFINING PREFORMATTED TEXT

Sometimes, it may not be convenient to manage space with the use of elements like <br>.

Alternatively, we can use the <pre> preformatted text element to present text as it is written in the HTML file. White space is recognized and displayed in the browser.

The text of a <pre> element is in a monospace font by default (we will learn how to change this in the CSS section).

Whitespace inside this element is displayed as it is written.

<pre> can be a useful element when presenting text where spaces and line breaks are necessary. A poem or song lyrics, for example.

WRITING COMMENTS IN HTML

Comments can make code easier to understand. They may guide other developers (and you in the future) to understand what the purpose of a given section of code is.

We can use comments to leave ourselves annotations, reminders, and explanations. Or we can add comments to mark sections of applications, so they are easily visible in code editors.

Well-written code should not need to be commented very much at all.

Any comments that you do add are hidden from the user in the browser.

A comment in HTML begins with <!--, and ends with -->:

<!-- This comment will not be displayed -->

In other languages, the syntax for writing comments differs.

But code editors and online sandbox environments usually provide keyboard shortcuts for commenting. In all code editors I have used, we can comment anything out by highlighting it and hitting command + /, or ctrl + /.

In this section, we have learned how to write HTML. In other words, what the syntax of HTML is.

We learned of a general rule in HTML - that most HTML elements have an opening tag and a closing tag that surround some content.

So, with this knowledge in the bank, we can now begin building our first simple web page.

In the very next section, we will do just that. We will begin to look at the structure of HTML documents and add to our repertoire of HTML elements.

FURTHER RESOURCES

Next section →