Use semantic HTML elements, such as ,

Writing clean HTML is about more than just making code that works—it's about creating markup that's readable, maintained, and accessible. Whether you're working alone or on a team, following best practices ensures your code is future-friendly and easier to debug. Here are the most effective practices for writing clean HTML:

1. Use Semantic HTML Elements
Choose elements that reflect the meaning of the content, not just how it should look. Semantic tags improve accessibility, SEO, and code clarity.
Examples:

<li> Use <header></header>
, <nav></nav>
, <main></main>
, <section></section>
, <article></article>
, <aside></aside>
, and <footer></footer>
for page structure.
<li> Use <p></p>
for paragraphs, <h1></h1>
to <h6></h6>
for headings, and <ul></ul>
, <ol></ol>
, <li>
for lists.
<li> Replace generic <div> or <code><span></span>
with meaningful tags like <button></button>
, <time></time>
, <figure></figure>
, or <blockquote></blockquote>
when appropriate. Avoid:
<div id="nav">...</div>
Prefer:

<nav>...</nav>
2. Maintain Consistent Indentation and Formatting
Proper indentation makes the document structure clear. Use consistent spacing (typically 2 or 4 spaces) and avoid mixing tabs and spaces.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Page Title</title>
</head>
<body>
<header>
<h1>Main Heading</h1>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
</header>
</body>
</html>
Self-closing tags (like <img src="/static/imghw/default1.png" data-src="image.jpg" class="lazy" alt="What are the best practices for writing clean HTML" >
, <br>
, <input>
) should be properly formatted. While HTML5 doesn't require a trailing slash, some teams prefer it for consistency—just be consistent.
Always use lowercase for element names and attribute names. This improves readability and avoids issues in case-sensitive contexts (like XHTML or JavaScript selectors).
Good:
<img src="/static/imghw/default1.png" data-src="image.jpg" class="lazy" alt="A description" />
Avoid:
<img src="/static/imghw/default1.png" data-src="image.jpg" class="lazy" ALT="A description" alt="What are the best practices for writing clean HTML" >
Also, always quote attribute values—even when optional (eg, autocomplete=off
should be autocomplete="off"
).
4. Prioritize Accessibility (a11y)
Write HTML with all users in mind, including those using screen readers or navigating with keyboards.
Key tips:
<li> Always include descriptive alt
attributes for <img alt="What are the best practices for writing clean HTML" >
tags. Use empty alt=""
if the image is decorative.<li> Use label
elements with for
attributes tied to form inputs.<li> Ensure logical heading hierarchy ( h1
→ h2
→ h3
, etc.) without skipping levels.<li> Use ARIA roles and properties only when necessary and correctly (eg, aria-label
, aria-hidden
).
Example:
<label for="email">Email Address</label>
<input type="email" id="email" name="email" required />
5. Keep Class and ID Names Meaningful and Consistent
Use clear, describe names that reflect purpose, not appear. Avoid presentational names like red-button
or big-text
.
Prefer:
<button class="call-to-action">Sign Up</button>
Over:
<button class="red-btn">Sign Up</button>
Use naming conventions like BEM (Block__Element--Modifier) if your project calls for it:
<article class="post post--featured">
<h2 class="post__title">Featured Article</h2>
</article>
6. Minimize Inline Styles and JavaScript
Keep structure (HTML), presentation (CSS), and behavior (JS) separate. Avoid
and onclick=""
attributes.
Bad:
<p style="color: red;" onclick="alert('hello')">Click me</p>
Good:
<p class="warning" id="alert-trigger">Click me</p>
Then handle styling and behavior in CSS and JS files.
7. Include Essential Document Structure
Always include the <!DOCTYPE html>
declaration and set the language with the lang
attribute. Include a viewport meta tag for responsive design.
Basic template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Page Title</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<!-- Content -->
</body>
</html>
<li> Minimize unnecessary wrapper elements.<li> Avoid deep nesting—keep the DOM shallow when possible.
<li> Validate your HTML using tools like the
W3C Validator to catch errors.
<li> Load scripts at the end of
or use
defer
/
async
to avoid render blocking.
Following these practices consistently leads to cleaner, more professional HTML. It might seem minor, but well-structured markup pays off in collaboration, debugging, and long-term project health. Basically, write HTML as if someone else (or future you) will have to understand it quickly.
The above is the detailed content of What are the best practices for writing clean HTML. For more information, please follow other related articles on the PHP Chinese website!