Common HTML Mistakes to Avoid and How to Fix Them
HTML is the backbone of web development, and making sure it's well-structured and error-free is crucial for both SEO and user experience. In this post, we’ll go through some of the most common HTML mistakes, why they matter, and how to fix them.
1. Missing Alt Attribute in Image Tags
Issue: Many developers forget to include the alt
attribute when adding images to a page. This can negatively affect accessibility and SEO.
Fix: Always include a descriptive alt
text for images. This ensures screen readers can describe the image to visually impaired users and helps search engines understand the content of your images.
Incorrect Format:
<img src="logo.png">
Correct Format:
<img src="logo.png" alt="Company logo">
2. Unclosed HTML Tags
Issue: Forgetting to close HTML tags is a common mistake, and it can lead to unpredictable page layouts and broken functionality.
Fix: Always ensure that every HTML tag has a corresponding closing tag. Modern HTML validators can help catch this issue.
Incorrect Format:
<div>Some content
Correct Format:
<div>Some content</div>
3. Using Inline Styles Instead of External Stylesheets
Issue: Using inline styles within HTML tags makes the code harder to maintain, and it can affect performance, especially on larger sites.
Fix: Use external stylesheets for styling. This makes your code cleaner, easier to maintain, and improves performance by allowing browsers to cache styles.
Incorrect Format:
<div style="color: red;">Text</div>
Correct Format:
<link rel="stylesheet" href="styles.css">
4. Using Deprecated Tags and Attributes
Issue: Using outdated tags or attributes like <font>
or <center>
that are no longer supported in HTML5.
Fix: Always use modern, semantic tags like <section>
, <article>
, <nav>
, and <header>
for layout and structure.
Incorrect Format:
<font color="red">Text</font>
Correct Format:
<article>Content goes here</article>
5. Not Using Semantic HTML Elements
Issue: Using <div>
tags for everything without considering semantic HTML elements.
Fix: Use semantic tags to enhance accessibility, SEO, and maintainability. For instance, <header>
, <footer>
, <article>
, and <section>
are better than generic <div>
tags.
Incorrect Format:
<div>Header content</div>
Correct Format:
<header>Header content</header>
6. Forgetting to Use the Doctype Declaration
Issue: Not including the <!DOCTYPE html>
declaration at the beginning of an HTML document can result in rendering issues, as the browser will switch to "quirks mode."
Fix: Always include <!DOCTYPE html>
at the top of your HTML document to ensure your page is rendered correctly.
Incorrect Format:
<html>
Correct Format:
<!DOCTYPE html>
7. Incorrect Nesting of Tags
Issue: HTML elements must be properly nested to ensure correct display and functionality. For example, placing block-level elements inside inline elements can cause issues.
Fix: Always ensure that your tags are properly nested. For example, avoid putting a <div>
inside a <span>
tag.
Incorrect Format:
<span><div>Text</div></span>
Correct Format:
<div>Text</div>
8. Using Non-Semantic Elements for Layout
Issue: Using <table>
for page layout instead of modern layout techniques like CSS Grid or Flexbox.
Fix: Use <table>
only for displaying tabular data. For layout, rely on CSS Grid or Flexbox.
Incorrect Format:
<table><tr><td>Content</td></tr></table>
Correct Format:
<div style="display: grid;">Content</div>
9. Using Deprecated HTML Attributes
Issue: Attributes like align
, border
, and bgcolor
are deprecated in HTML5.
Fix: Use CSS for styling instead of these deprecated attributes.
Incorrect Format:
<table border="1">
Correct Format:
<table style="border: 1px solid black;">
10. Failing to Add meta
Tags for Mobile Optimization
Issue: Not adding a meta
tag for viewport settings results in poor mobile responsiveness.
Fix: Always include the following meta
tag in your <head>
for better mobile optimization:
Incorrect Format:
<meta>
Correct Format:
<meta name="viewport" content="width=device-width, initial-scale=1">
11. Incorrect Use of <br>
Tags for Spacing
Issue: Using the <br>
tag for layout purposes or spacing is a common mistake. This leads to poor maintainability and design issues.
Fix: Use CSS for spacing, margins, and layout adjustments instead of relying on <br>
tags.
Incorrect Format:
<br><br><br>Text
Correct Format:
<div style="margin-bottom: 20px;">Text</div>
12. Overuse of Inline JavaScript
Issue: Embedding JavaScript code directly inside HTML elements, such as in the <body>
or in event attributes like onclick
, makes the code harder to maintain and less secure.
Fix: Always place JavaScript in external files, linking them in the <head>
or at the end of the <body>
section for better performance and maintainability.
Incorrect Format:
<button onclick="alert('Hello!')">Click me</button>
Correct Format:
<script src="script.js"></script>
13. Not Using <meta charset>
Tag
Issue: Forgetting to include the <meta charset>
tag can cause issues with character encoding, particularly with special characters and non-English text.
Fix: Always add the <meta charset="UTF-8">
tag in the <head>
section to ensure proper character encoding.
Incorrect Format:
<html>
Correct Format:
<meta charset="UTF-8">
14. Using Outdated or Unsupported Tags
Issue: Using tags like <marquee>
or <blink>
, which are no longer supported in modern browsers.
Fix: Avoid these tags entirely and use CSS animations or transitions for modern effects.
Incorrect Format:
<marquee>Moving text</marquee>
Correct Format:
<div style="animation: moveText 5s linear infinite">Moving text</div>
15. Not Adding Favicon
Issue: Not adding a favicon to your site results in an empty tab icon, which can look unprofessional.
Fix: Always add a <link>
tag to include a favicon.
Incorrect Format:
<html>
Correct Format:
<link rel="icon" href="favicon.ico">
16. Using Non-Descriptive Link Text
Issue: Using vague text like "Click here" for links reduces accessibility and SEO performance.
Fix: Use descriptive link text that explains the purpose of the link for both accessibility and search engine optimization.
Incorrect Format:
<a href="about.html">Click here</a>
Correct Format:
<a href="about.html">Learn more about our company</a>
17. Incorrect Use of <table>
for Layout
Issue: Using <table>
tags for layout purposes instead of for displaying tabular data.
Fix: Use CSS layout techniques like Flexbox or Grid for page layout and reserve <table>
for actual data representation.
Incorrect Format:
<table><tr><td>Content</td><td>Content</td></tr></table>
Correct Format:
<div style="display: flex;">Content</div>
18. Missing <head>
Elements
Issue: Missing necessary elements like the <title>
tag and meta tags in the <head>
section of your document.
Fix: Ensure that your page has all essential elements in the <head>
section for SEO and accessibility.
Incorrect Format:
<html>
Correct Format:
<head><title>Page Title</title></head>
19. Overloading Your Website with Unoptimized Images
Issue: Uploading large images without optimizing them for web use can slow down your site.
Fix: Always optimize images using compression tools and proper formats (such as .jpg
, .png
, or .webp
) before uploading them to your website.
Incorrect Format:
<img src="large-image.jpg">
Correct Format:
<img src="optimized-image.jpg" width="600">
20. Not Testing Your HTML for Accessibility
Issue: Ignoring accessibility features can lead to a website that’s difficult to navigate for users with disabilities.
Fix: Always use semantic HTML, include alt
attributes for images, and ensure keyboard navigation is possible. Testing your website with accessibility tools can help improve the experience for all users.
Incorrect Format:
<img src="image.jpg">
Correct Format:
<img src="image.jpg" alt="Description of image">