Here's a surprising fact about HTML: browsers almost never show an error. Forget a closing tag, nest elements wrongly or misspell an attribute, and the page usually still appears. That sounds friendly, but it hides problems. The page might look fine in Chrome on your laptop and break on a phone, in a screen reader, or when you add CSS and JavaScript later.
This chapter collects the habits professional developers follow to write clean, correct HTML, and shows how to validate your pages automatically so mistakes are caught before your users find them.
Why Browsers Don't Complain
The HTML Living Standard defines not only correct HTML but also exactly how browsers must recover from incorrect HTML. Every modern browser follows the same error-recovery rules, so they quietly "fix" your mistakes, but the fix may not be what you meant.
A classic example. You might write a list inside a paragraph:
<p>Skills:
<ul>
<li>C++</li>
</ul>
</p>A <p> can only contain phrasing content (text and inline elements), so the browser builds this DOM instead:
<p>Skills:</p> ← paragraph closed automatically when <ul> starts
<ul><li>C++</li></ul>
<p></p> ← the leftover </p> creates a new, EMPTY paragraphYou now have three elements where you expected one. CSS like p { border: 1px solid; } or JavaScript like p.querySelector("ul") will behave strangely, and you'd have no idea why. A validator would have told you immediately.
Validation means checking your HTML against the rules of the HTML standard (and optionally extra best-practice rules) using a tool called a validator or checker.
Validating with the W3C Checker
The official free tool is the Nu Html Checker run by the W3C at validator.w3.org/nu. You can check a page three ways: by URL (for live sites), by uploading a file, or by pasting your code into the text box.
Let's validate this page, which has several common mistakes. Can you spot them before reading on?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My Profile</title>
</head>
<body>
<h1>Aman Verma</h1>
<p>Final year B.Tech student.
<ul>
<li>C++</li>
<li>Python</li>
</ul>
</p>
<img src="aman.jpg">
<a href="resume.pdf"><button>Download resume</button></a>
<p id="contact">Email: aman@example.com</p>
<p id="contact">Phone: 98765 43210</p>
<center>Thanks for visiting!</center>
</body>
</html>This is the real output of the W3C checker for this file (the numbers are line.column positions):
14.3-14.6: error: No “p” element in scope but a “p” end tag seen.
15.3-15.22: error: An “img” element must have an “alt” attribute, except under
certain conditions. For details, consult guidance on providing
text alternatives for images.
16.24-16.31: error: The element “button” must not appear as a descendant of the
“a” element.
18.3-18.18: error: Duplicate ID “contact”.
17.3-17.18: info warning: The first occurrence of ID “contact” was here.
19.3-19.10: error: The “center” element is obsolete. Use CSS instead.Reading the Errors
Error | What it means | Fix |
|---|---|---|
No "p" element in scope but a "p" end tag seen | The | End the paragraph before the list |
"img" must have an "alt" attribute | No text alternative | Add a descriptive |
"button" must not appear as a descendant of "a" | Interactive elements can't be nested: is a click a link click or a button click? | Use just a link (style it like a button with CSS) |
Duplicate ID "contact" |
| Use different ids, or a |
"center" element is obsolete | Presentational element removed from HTML | Use a normal element and CSS |
Tip: fix the first error first, then validate again. One mistake (like a missing closing tag) can cause a chain of later errors that disappear once the first is fixed.
The Fixed Page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Aman Verma | Profile</title>
</head>
<body>
<h1>Aman Verma</h1>
<p>Final year B.Tech student. Skills:</p>
<ul>
<li>C++</li>
<li>Python</li>
</ul>
<img src="aman.jpg" width="200" height="200" alt="Aman Verma smiling, wearing a blue shirt">
<p><a href="resume.pdf" download>Download resume (PDF)</a></p>
<p id="email">Email: aman@example.com</p>
<p id="phone">Phone: 98765 43210</p>
<p>Thanks for visiting!</p>
</body>
</html>Running the fixed file through the W3C checker gives no errors or warnings at all.
Validating from the Command Line
In real projects, you don't want to paste every file into a website. html-validate is a popular open-source checker that runs with Node.js. With Node installed, run it in your project folder:
npx html-validate buggy.htmlOutput for the same buggy file:
9:4 error Element <p> is implicitly closed by adjacent <ul> no-implicit-close
14:4 error Stray end tag '</p>' close-order
15:4 error <img> is missing required "alt" attribute wcag/h37
16:25 error <button> is missing recommended "type" attribute no-implicit-button-type
16:25 error <button> element is not permitted as a descendant of <a> element-permitted-content
18:10 error Duplicate ID "contact" no-dup-id
19:4 error <center> is deprecated: use CSS instead deprecated
19:4 error <center> element is not permitted as content under <body> element-permitted-content
✖ 8 problems (8 errors, 0 warnings)It finds the same problems plus a couple of extra best-practice checks (it points to line 9, where the paragraph is actually broken, and it asks for an explicit type on buttons). The last column is the rule name, which you can look up on the tool's website. On the fixed file, it prints nothing, which means no problems.
Teams often run a checker automatically on every commit, along with tools like Lighthouse (in Chrome DevTools) for accessibility, performance and SEO audits.
A valid page isn't automatically a good page. As we saw in the Accessibility Basics chapter, a validator can't tell whether your alt text makes sense or whether a
<div>should have been a<button>. Validation catches mistakes in the rules; you still have to think about meaning.
Best Practices Checklist
1. Always Start with the Basic Skeleton
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>A Unique, Descriptive Title</title>
</head>
<body>
...
</body>
</html>Each line has a job (see the HTML Document Structure and The Head Element & Meta Tags chapters): the doctype keeps the browser in standards mode, lang helps screen readers and translation, charset prevents garbled characters like ’, viewport makes it work on phones, and <title> names the page.
2. Use Semantic Elements
Choose elements for their meaning: <header>, <nav>, <main>, <article>, <button>, <h1>–<h6> in order. Use <div> and <span> only when no semantic element fits (see the Semantic HTML and Block vs Inline Elements: div & span chapters).
3. Separate Structure, Style and Behaviour
Avoid | Prefer |
|---|---|
| A class and a rule in an external CSS file |
|
|
| CSS |
HTML describes what the content is, CSS says how it looks, and JavaScript says how it behaves. Keeping them apart means you can redesign a site by changing one CSS file instead of hundreds of HTML tags.
4. Follow Nesting and Content Rules
Close elements in the reverse order you opened them:
<p><strong>Hi</strong></p>, not<p><strong>Hi</p></strong>.No block content inside
<p>(no lists, divs, tables or headings).No interactive content inside interactive content: no
<button>in an<a>, no<a>in an<a>.<li>only inside<ul>,<ol>or<menu>;<ul>can only directly contain<li>(and script-supporting elements).Every
idis unique in the page.
5. Write Consistent, Readable Markup
Use lowercase element and attribute names. (HTML ignores case, but lowercase is the universal convention.)
Quote attribute values, preferably with double quotes.
class=btn primarybreaks, because the space ends the value;class="btn primary"works.Indent nested elements consistently (2 or 4 spaces) so the structure is visible.
Write boolean attributes simply:
<input required>. Remember thatrequired="false"still means required.Use descriptive file and folder names in lowercase with hyphens:
about-us.html,images/team-photo.jpg. Some servers (like most Linux servers) treatPhoto.JPGandphoto.jpgas different files.Use comments
<!-- like this -->to explain sections, but remember anyone can read them with "View Source", so never put passwords or private notes in them.
6. Know Void Elements and the Trailing Slash
Void elements can't have content or an end tag: <br>, <hr>, <img>, <input>, <meta>, <link>, <source>, <track>, <wbr>, <area>, <col>, <base> and <embed>.
Writing <br /> or <img ... /> is allowed, but in HTML the slash has no effect. On a non-void element it is dangerous, because the slash is ignored and the element stays open:
<div/>
<p>Hello</p>W3C checker output:
error: Self-closing syntax (“/>”) used on a non-void HTML element.
Ignoring the slash and treating as a start tag.
error: End tag for “body” seen, but there were unclosed elements.
error: Unclosed element “div”.The paragraph ends up inside the div. (Inside <svg>, as we saw in the Canvas & SVG chapter, self-closing tags are fine because SVG follows XML rules.)
7. Avoid Obsolete Elements
Obsolete | Use instead |
|---|---|
| CSS |
| CSS ( |
|
|
|
|
| CSS animation (and think twice: moving text is hard to read) |
| Normal page layout; |
Watch out for the opposite myth: <b>, <i>, <u>, <s> and <small> are not obsolete. HTML5 gave them specific meanings (see the Text Formatting chapter), so they're valid when used for those meanings.
8. Make It Accessible and Fast
Alt text on every
<img>, a<label>for every input, real buttons and links (see the Accessibility Basics chapter).widthandheighton images and iframes to prevent layout jumps.Load scripts with
defer(or put them at the end of<body>) so they don't block the page from showing.Use
loading="lazy"on images and iframes far down the page.
Errors vs Warnings: What Must You Fix?
Message type | Meaning | Action |
|---|---|---|
Error | Your markup breaks a rule of the HTML standard | Fix it |
Warning | Allowed, but possibly a mistake or bad practice | Read it and decide; usually fix it |
Info | Extra context (for example, where a duplicate id first appeared) | Use it to understand the error |
Aim for zero errors. Valid HTML gives you a predictable DOM, which makes CSS and JavaScript behave the same in every browser, makes the page more robust for assistive technology, and makes bugs much easier to find.
Common Mistakes
"It looks fine in my browser, so it's correct." Browsers hide errors; validate anyway.
Fixing errors from the bottom of the list. Start from the first one; later errors are often side-effects.
Wrapping a
<button>in an<a>to make a "button link".Reusing an
idfor styling several elements. Use aclass.Copying old tutorials that use
<center>,<font>or table-based layouts.Thinking a valid page is an accessible page. Validation is necessary but not enough.
Interview Questions
Why validate HTML if browsers display invalid pages anyway?
Browsers silently repair invalid markup, and the repaired DOM may not match what you intended, causing CSS, JavaScript and accessibility problems that are hard to debug. Validation catches these errors early, gives you a predictable DOM, and helps the page work consistently across browsers and assistive technologies.
What happens if you put a <ul> inside a <p>?
The parser closes the paragraph automatically when it sees <ul>, because <p> can only contain phrasing content. The list becomes a sibling of the paragraph, and the later </p> creates an extra empty paragraph.
What does the trailing slash in <br /> do in HTML?
Nothing. It's allowed on void elements for compatibility with XHTML habits, but it has no effect. On non-void elements like <div/>, the slash is ignored and the element is treated as an open start tag, which is an error.
Are <b> and <i> deprecated?
No. They were redefined in HTML5 with semantic meanings: <b> draws attention to text without adding importance (like keywords), and <i> marks text in an alternate voice (like technical terms or foreign words). <strong> and <em> are for importance and emphasis.
Which tool is the official HTML validator?
The W3C's Nu Html Checker at validator.w3.org/nu. It checks against the current HTML Living Standard and can check a URL, an uploaded file or pasted code.
Conclusion
Browsers silently repair invalid HTML, so a page that looks fine may still have a broken DOM.
Validate with the W3C Nu Html Checker or a command-line tool like
html-validate, and fix errors starting from the first.Start every page with the standard skeleton, use semantic elements, and keep HTML, CSS and JavaScript separate.
Respect nesting rules, unique ids, quoted attributes and lowercase names; avoid obsolete elements such as
<center>and<font>.Validation is necessary but not sufficient: also test accessibility, performance and how real people use the page.
You now know everything you need to build a professional page. In the final chapter, Project: Build a Complete Web Page, we'll put all of it together.