Elements are the fundamentals of HyperText Markup Language (HTML). Each HTML document is made of elements, which are defined using tags.
HTML elements and HTML tags are often confused. The tags are used to open and close the object, whereas the element includes both tags and its content. Let’s consider an example with the <h1> tag: <h1> Title of the document </h1> - is an element, and <h1>, </h1> - are tags.
Types of HTML Elements
HTML elements can come in pairs or be empty. The paired elements have opening (<>) and (</>) closing tags, and the content of the element is placed in between them.
Do not forget the closing tag! Though some HTML elements are displayed correctly, even without the closing tag, however, do not rely on this. This might lead to unexpected results or errors.
The empty elements have no closing tags. Empty elements can be "closed" in the opening tag like this: <br/> .
<html> <head> <title>Title of the document</title> </head> <body> <h1> Title of the document</h1> <p>The first paragraph</p> <p>The second paragraph, <br/> where line break is inserted </p> </body> </html>
In the above-mentioned example we used 4 paired elements:
- The <html> element, which includes all other HTML elements of the webpage.
- The <body> element, which contains the body of the webpage.
- The <p> element, that define a paragraph.
- The <h1> element, which contains the heading of the webpage.
In the given example we also used an empty <br> tag to insert a line-break.
Block-level and Inline HTML Elements
For the purpose of styling, all HTML elements are divided into two categories: block-level elements and inline elements.
Block-level elements are those that structure the main part of your web page, by dividing your content in coherent blocks. They always start on a new line and take up the full width of a page, from left to right; they also can can take up one line or multiple lines and have a line break before and after the element. Block-level elements can contain both block-level & inline elements.
Block-level elements are <address>, <article>, <aside>, <blockquote>, <canvas>, <dd>, <div>, <dl>, <dt>, <fieldset>, <figcaption>, <figure>, <footer>, <form>, <h1>-<h6>, <header>, <hr>, <li>, <main>, <nav>, <noscript>, <ol>, <output>, <p>, <pre>, <section>, <table>, <tfoot>, <ul> and <video>.
All block-level elements have opening and closing tags.
<html> <head> <title>Title of the document</title> </head> <body> <footer> <p>Author: W3docs team</p> <p><a href="email@example.com">Send message to the author</a>.</p> </footer> </body> </html>
In the example we used <footer> block-level element to define the footer of the web page and its content.
Inline elements are meant to differentiate part of a text, to give it a particular function or meaning. Inline elements usually comprise a single or few words. They do not cause a line break and do not take up the full width of a page, only the space bounded by its opening and closing tag. The inline elements are usually used within other HTML elements.
Inline elements are: <a>, <abbr>, <acronym>, <b>, <bdo>, <big>, <br>, <buttom>, <cite>, <code>, <dfn>, <em>, <i>, <img>, <input>, <kbd>, <label>, <map>, <object>, <q>, <samp>, <script>, <select>, <small>, <span>, <strong>, <sub>, <sup>, <textarea>, <time>, <tt> and <var>.
Nested HTML elements
HTML elements can contain other elements, i.e. be nested. For example, if you want to give a text a strong emphasis and tell the browser to display it as bold, you can use the <strong> tag nested in the <p> tag.
<p><strong>This part of the sentence</strong> has a strong emphasis and is displayed as bold.</p>
HTML tags should be “nested” in a proper order, meaning that the tag opened most recently is always the next tag to close.
In our example, we closed the <strong> tag first, and then the <p> tag.