Home » Website Errors » Invalid or Broken HTML: Find Unclosed Tags Before They Break Your Layout
Invalid or Broken HTML: Find Unclosed Tags Before They Break Your Layout
Browsers are forgiving. When they meet broken HTML, they guess what you meant and carry on. The trouble is that different browsers, screen readers and parsers guess differently. A page that looks right in Chrome can have a footer floating in the middle in Safari, a sidebar inside the article in a reader view, or a button that will not click.
What "broken" usually means
| Problem | Example | Effect |
|---|---|---|
| Unclosed element | <div class="card"> with no </div> |
Everything after it is nested inside the card |
| Wrong nesting | <a><div>...</a></div> |
Browsers rearrange elements in unexpected ways |
| Block inside inline/paragraph | <p><div>...</div></p> |
The paragraph closes early and leaves an empty <p> |
| Duplicate IDs | Two elements with id="menu" |
Scripts and anchor links target the wrong one |
| Missing quotes | <a href=page.html title=About us> |
Attribute values get cut at the space |
| Stray closing tag | </div> with no opener |
A parent closes too early |
Step 1: validate the page
Use the W3C validator at validator.w3.org. Paste the URL, upload a file, or paste the source directly. For pages behind a login, view the source in your browser and paste it in.
Read the output from the top. The first structural error ("Unclosed element", "End tag ... seen but there were open elements", "Stray end tag") is usually the cause of many later ones.
Step 2: see what the browser built
The validator shows what your HTML says. The browser's Inspector shows what the browser made of it. Open the Elements panel (F12) and fold up the tree. If the footer appears inside the main article, or the sidebar sits inside a paragraph, you have found where structure went wrong. Compare that spot with the source.
Step 3: fix structure, not symptoms
When a layout breaks, the instinct is to add CSS until it looks right. That hides the problem. Instead:
- Indent the HTML consistently so each opening tag lines up with its closing tag. Editors like VS Code can format the document for you.
- Use the editor's tag matching to jump between pairs.
- Close elements in the reverse order you opened them.
- Replace
<div>inside<p>with a<div>wrapper or a<span>.
Step 4: find where the bad HTML comes from
On a CMS, invalid markup usually enters from one of these places:
- Content pasted from Word or Google Docs, bringing hidden spans and styles. Paste as plain text, or use the editor's "clear formatting".
- Hand-edited HTML blocks in a page builder or a widget.
- Theme templates that open a wrapper in
header.phpand close it infooter.php. Editing one without the other breaks every page. - Ads or embed codes that include a stray closing tag.
Step 5: keep it valid
Run the validator after theme changes and on a sample of templates (home, post, category, page). Add an HTML linter to your editor so mistakes are underlined as you type.
Common mistakes
- Fixing the 200th error before the first.
- Trusting a single browser's rendering as proof the HTML is correct.
- Patching layout with
position: absoluteor negative margins to cover a missing</div>. - Pasting rich text straight into HTML mode of an editor.
Frequently Asked Questions
My page has validation errors but looks fine. Should I care?
Fix errors that change structure, such as unclosed elements and wrong nesting. They can break in another browser, in a screen reader, or after a small edit. Warnings about style or obsolete attributes are lower priority.
Why does the validator report hundreds of errors?
One unclosed element near the top can make everything after it look wrong. Fix the first structural error and run the check again. The count often drops sharply.
Does invalid HTML affect SEO?
Search engines tolerate minor errors. Serious ones can hide content, break links or move text out of the main content area, and that does affect how a page is read.
Found a mistake or still stuck? Contact us. See how we write and check these guides in our editorial policy, or read about DocumentGuy.