HTML Elements

HTML elements are the fundamentals of HTML. HTML documents are simply a text file made up of HTML elements. These elements are defined using HTML tags.

An HTML element is an individual component of an HTML document. Any given web page consists of many HTML elements.

HTML tags tell your browser which elements to present and how to present them. Where the element appears is determined by the order in which the tags appear.

HTML consists of over 100 tags. Don't let that put you off though - you will probably find that most of the time, you only use a handful of tags on your web pages. Having said that, I highly recommend learning all HTML tags eventually - but we'll get to that later.

OK, lets look more closely at the example that we created in the previous lesson.

Try it

Explanation of the above code:

Closing your Tags

As mentioned in a previous lesson, you'll notice that all of these HTML elements have opening and closing tags, and that the content of the element is placed in between them. There are a few exceptions to this rule.

You'll also notice that the closing tag is slightly different to the opening tag — the closing tag contains a forward slash (/) after the <. This tells the browser that this tag closes the previous one.

Indents and Carriage Returns

You can indent the code and add extra carriage returns if you like. This can help with readability, and it doesn't affect the way the page is displayed in the browser.

In fact, code indenting is a universal practice in computer programming circles, and most HTML editors automatically indent the code for you as you type.

For example, we could take the above example, and indent it like the following example, and it will still appear the same in the browser.

Try it

UPPERCASE or lowercase?

You can use uppercase or lowercase when coding HTML, however, most developers use lowercase. This helps the readability of your code, and it also saves you from constantly switching between upper and lower case. Lowercase also helps keep your code XML compliant (if you're using XHTML), but but that's another topic.

Therefore...

Optional Tags

Some of the above tags are optional in certain situations. In particular, the html, head, and body tags can all be omitted.

So it would be perfectly valid to change the above example to this:

Try it

However, there are some very specific rules around when you can and can't do this. For example, the html start tag can be omitted if it's not immediately followed by a comment, and the same applies to its end tag. And the head element's start tag can only be omitted if the element is empty, or if the first thing inside the element is an element. And its end tag can only be omitted if the element is not immediately followed by a space character or a comment. The body element also has its own specific rules for omitting tags.

If you're interested in learning which tags you can leave out, check the HTML specification for the rules around the specific tags you'd like to omit.

If you're not sure whether or not it's OK to omit a tag, just include all tags (like in the first example).

In the next lesson, we learn about some of the more common formatting tags.