Varun Malik
PRO
last year
Varuncountry asked

Here's an HTML code I wrote: <h1>This is a heading <p>This is a paragraph.</p> How did this work on my Visual Studio when <h1> doesn't have a closing tag?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi there! It sounds like you're encountering a common situation when HTML behaves in unexpected ways.

In HTML, every opening tag should ideally have a closing tag to ensure that browsers render the content correctly, just like this:

This is a heading

This is a paragraph.

However, when you omit a closing tag like you've done here:

This is a heading

This is a paragraph.

browsers step in and try to "fix" the structure automatically. They'll assume where the ending tags should be, but this isn't always reliable and can result in unexpected outcomes, especially as your projects become more complex.

This automatic behavior might work similarly in your text editor like Visual Studio Code, but it’s not best practice. Therefore, always include closing tags to maintain clear and consistent HTML structure:

This is a heading

This is a paragraph.

Remember, it'll help prevent your webpage from displaying incorrectly or "breaking" unexpected parts of your layout.

Hope this helps clear things up! If any more questions come up as you go further, feel free to ask. I'm here to help!

HTML
This question was asked as part of the Learn HTML course.