What happens if we don't add a closing tag?


#1

Question

In the context of this exercise, what happens if we don’t add a closing tag for an HTML element?

Answer

If you do not add a closing tag for an HTML element, you may end up with unexpected issues with your webpage. However, excluding a closing tag will not cause error messages or crashes, so they technically are not an absolute requirement. But, this does not mean you should exclude them.

One important reason to add a closing tag is that anything that follows an opening tag in the HTML code will become nested in that element. For example, say you meant to have two separate elements, such as a <div> and a <p> element. Like so,

<body>
  <div>
  </div>
  <p>
  </p>
</body>

If you omit the closing tags of the elements, like so

<body>
  <div>
  <p>
</body>

then the paragraph element is now nested inside the div element. This can cause many issues in terms of layout and design of a webpage.

Adding closing tags is also best practice, and should always be included. If you do that then errors will be avoided, and it will reduce confusion when other developers are working on the same code.

Some elements do not need a closing tag (known as “void” tags), such as <br>, <img>, and <link> tags, because they do not have content. But for other types of elements, you should always add a closing tag to them.