By WebNest Studio Engineering Team
HTML Tutorial
HTML Best Practices
Writing HTML that "works" in a browser is a low bar — writing HTML that's clean, maintainable, accessible, and easy for other developers (or your future self) to read is a different standard entirely. A handful of consistent habits separate professional-quality markup from markup that merely renders correctly by accident.
Why it matters
HTML is forgiving: browsers quietly repair broken markup, so mistakes do not show up as errors. They show up later, as a layout that differs between browsers or a script that cannot find an element. Consistent, valid markup is what makes a codebase pleasant for the next developer, who is often you.
Consistent Indentation and Formatting
Nested elements should be indented consistently (commonly two or four spaces) so that the document's structure is visually obvious just by scanning it. Inconsistent or absent indentation makes it much harder to spot mismatched or improperly nested tags at a glance, especially as a page grows.
Always Close Tags Properly
Every element that requires a closing tag should have one, closed in the correct order relative to other open tags. Void elements like <img>, <br>, and <input> have no closing tag at all and shouldn't be given one. Relying on a browser's error-correcting behavior to "fix" unclosed or mismatched tags produces markup that is fragile and can render differently across different browsers or parsers.
Avoid Inline Styles
Inline style attributes mix presentation directly into structural markup, making styles harder to reuse, override, and maintain consistently across a site. Styling should live in external or embedded CSS using classes, which keeps HTML focused purely on structure and content, and lets a single style rule apply consistently everywhere it's needed.
- Prefer class-based CSS selectors over inline style attributes for anything beyond a one-off, temporary tweak.
- Keep structural markup (HTML) and presentation (CSS) in separate concerns wherever practical.
Validate Your Markup
Running HTML through a validator (such as the W3C Markup Validation Service) catches structural errors — unclosed tags, invalid nesting, duplicate IDs, missing required attributes — that might otherwise render "fine" in one browser today but break unpredictably in another browser or a future version. Validating markup regularly, especially before shipping a page, catches these issues while they're still cheap to fix.
Example
Clean, consistently formatted markup with no inline styles
<section class="pricing-card">
<h2 class="pricing-card__title">Pro Plan</h2>
<p class="pricing-card__price">$15/month</p>
<ul class="pricing-card__features">
<li>100 GB storage</li>
<li>Priority support</li>
</ul>
</section>
Renders a pricing card section whose visual appearance is entirely controlled by an external stylesheet targeting the pricing-card classes, keeping the HTML readable, consistently indented, and free of inline style attributes.
Common Mistakes
- Using inline style attributes scattered across many elements instead of reusable CSS classes.
- Leaving tags unclosed or improperly nested and relying on the browser to silently correct them.
- Inconsistent or missing indentation, making the document structure hard to scan visually.
- Never running markup through a validator, letting small structural errors accumulate unnoticed.
Exercise
Clean up the markup: use lower-case tag names, close the paragraph and list items properly, quote the attribute value, and move the inline styles into a class named highlight in a style element.
Expected result: the same content, with the paragraph in red bold text, written as valid, consistently formatted HTML.
<H1>Features</H1>
<P style="color: red; font-weight: bold">New this month
<UL>
<LI>Faster search
<LI>Dark mode
</UL>
<a href=about.html>About</a>
Show hints
- A paragraph cannot contain a list, so close the
pbefore theulstarts. - In the
styleelement, write.highlight { color: red; font-weight: bold; }and apply it withclass="highlight".
Show solution
<style>
.highlight {
color: red;
font-weight: bold;
}
</style>
<h1>Features</h1>
<p class="highlight">New this month</p>
<ul>
<li>Faster search</li>
<li>Dark mode</li>
</ul>
<a href="about.html">About</a>
Quick quiz
1. Why are inline style attributes discouraged?
- A. They do not work in modern browsers
- B. They are slower to type
- C. They mix presentation with content, cannot be reused and are hard to override
- D. They break links
Show answer
C. Styles in a stylesheet can be reused across pages and changed in one place.
2. What does an HTML validator check?
- A. That the links are popular
- B. That the page looks good
- C. That the page loads quickly
- D. That the markup follows the HTML standard
Show answer
D. The W3C validator reports unclosed tags, invalid nesting and duplicate ids.
3. Which statement about id values is correct?
- A. An id must be unique within the page
- B. An id may contain spaces
- C. An id is required on every element
- D. Several elements may share one id
Show answer
A. Labels, fragment links and getElementById all rely on ids being unique.
Interview questions
Why should content, presentation and behaviour be kept separate?
Show answer
HTML describes the content and its meaning, CSS its appearance, and JavaScript its behaviour. Keeping them in separate files means a design can change without touching the content, styles and scripts are cached and reused across pages, and each part can be read, tested and maintained by itself. Inline styles and inline event handlers scatter these concerns through the markup and are difficult to override or update.
What are some common HTML mistakes?
Show answer
Choosing elements for their appearance, such as a heading level for its size. Leaving images without alt and inputs without labels. Using a div where a button or link is needed. Nesting elements incorrectly, such as a block element inside a paragraph. Reusing the same id. Omitting the doctype, the lang attribute or the viewport tag. Most are caught by a validator and an accessibility checker.
Key Points to Remember
- Consistent indentation makes nested structure easy to scan and debug.
- Close every tag that requires closing, in the correct nested order; never close void elements like img or br.
- Keep styling in CSS classes rather than inline style attributes.
- Regularly validate markup with a tool like the W3C Markup Validation Service to catch structural errors early.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering