HTML elements are the basic building blocks of HTML. Each element gets to have its own tag or tags. Usually, elements have two tags: an opening tag and a closing tag. However, there are some odd ones that have just one (opening) tag; such elements are called empty elements or void elements.
You might find some folks using the terms HTML elements and HTML tags interchangeably. Technically, that is not correct. An HTML element is a collection of tag (or tags) and the content that goes with it. In fact, we can have an element even if the tag is absent! For example, we can have the head element present in the document, even if the tag is not there since tag for the head element is optional.
An HTML page consists of four top-level elements. First, the element that declares the type of HTML page. It is mandatory to have the declarative element -- without that, the HTML document is considered invalid. Second, the HTML element itself -- this element is the main element in an HTML page and pretty much everything resides within it. This element, in turn, embodies the next key elements: the head element and the body element. The head element contains meta-information for the page. The body element is where all the contents that needs to be displayed goes.
Here is an example (given below) that contains the above four elements.
<!doctype html> <html> <head> <title> Learning HTML Elements! </title> </head> <body> <!-- The content in the body element --> <p> The body-content goes here. </p> </body> </html>
Elements can also nest other elements. The rather obvious example is the html element that is nesting two other elements: head and body. Another example is the body element that is nesting a comment element (identified by <!-- and --> tags) and a paragraph element (identified by <p> and </p> tags).
Before we move on, it is important to note that we cannot have HTML element embedded inside the enclosed non-HTML element. Thus, if we were to get over-enthusiastic and try to embed HTML elements within the non-HTML element, then that would not work. Due to this reason, the following example would not work because it is adding a <p> element within a script element, which is non-HTML.
In addition to the elements mentioned above, HTML supports many more types of elements. This fairly large list of elements enables HTML to support various types of content like text, lists, tables, images, links, scripts, etc. Hence, let us list tags for some of these common elements. We would revisit these tags and a lot more in later sections.
|<head>||Head of the HTML document||No|
|<body>||Body of the HTML document||No|
|<title>||Window title text or page header. Is mandatory||No|
|<div>||Add a division||No|
|<table>||Add a table||No|
|<p>||Add a paragraph||No|
|<form>||Add a form||No|
|<input>||Provide input options within a form||Yes|
|<script>||Run scripts (for interactivity)||No|
|<style>||Specify style rules (for presentation)||No|
|<img>||Embed an image||Yes|
|<br>||Adds a line-break||Yes|
|<ul>||Add an unordered list||No|
|<ol>||Add an ordered list||No|
|<a>||Add an Internet Link (URL)||No|
|<!-- -->||Add a Comment||No|
Needless to say, the above list is far from complete! For a complete list, we refer the reader to go through the W3C spec: http://www.w3.org/.