HTML is the backbone of the web, and mastering it is essential for any web developer. However, even the most experienced developers can make mistakes when coding in HTML. In this blog post, we will discuss 12 common HTML mistakes that developers make and how to avoid them.
1. Improper Nesting
Improper nesting is a common mistake that occurs when HTML tags are not properly nested within each other. This can lead to broken layouts and issues with rendering. To avoid this mistake, make sure that all HTML tags are properly nested within each other.
<!-- Bad -->
<div>
<p>This is some text</div>
</p>
<!-- Good -->
<div>
<p>This is some text</p>
</div>
2. Not Using Semantic HTML
Semantic HTML is a way of writing HTML that conveys the meaning of the content rather than just the appearance. Not using semantic HTML can make your code harder to read and understand. To avoid this mistake, make sure to use the appropriate HTML tags to convey the meaning of your content.
<!-- Bad -->
<div class="heading">My Heading</div>
<!-- Good -->
<h1>My Heading</h1>
3. Overusing or Misusing Divs
Divs are a powerful tool for creating layouts in HTML but overusing or misusing them can make your code harder to maintain and understand. To avoid this mistake, use divs sparingly and only when they are needed to group related content.
<!-- Bad -->
<div class="container">
<div class="header">Header</div>
<div class="content">Content</div>
<div class="footer">Footer</div>
</div>
<!-- Good -->
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>
4. Using Deprecated Tags
HTML tags can become deprecated over time as new versions of HTML are released. Using deprecated tags can cause compatibility issues and may not be supported by modern web browsers. To avoid this mistake, make sure to use the latest version of HTML and avoid using deprecated tags.
<!-- Bad -->
<center>This text is centered</center>
<!-- Good -->
<div style="text-align: center;">This text is centered</div>
5. Not Using CSS for Styling
While HTML can be used to add some basic styling to your web pages, it is not intended for complex styling. To avoid this mistake, use CSS to style your web pages and keep your HTML code clean and easy to read.
<!-- Bad -->
<h1 style="font-size: 24px;">My Heading</h1>
<!-- Good -->
<h1 class="heading">My Heading</h1>
6. Not Including Meta Tags
Meta tags are an important part of HTML that provides information about your web page, such as the title, description, and keywords. Not including meta tags can make it harder for search engines to index your web page and can negatively affect your search engine rankings. To avoid this mistake, make sure to include relevant meta tags in your HTML code.
<!-- Bad -->
<title>My Website</title>
<!-- Good -->
<meta name="description" content="This is my website">
<meta name="keywords" content="web development, HTML, CSS">
7. Using Inline Styles
Inline styles are styles that are applied directly to HTML elements using the style attribute. While inline styles can be useful for small styling tweaks, they can make your HTML code harder to read and maintain. To avoid this mistake, use CSS stylesheets to apply styles to your web pages.
<h1 style="font-size: 24px; color: red;">My Heading</h1>
<!-- Good -->
<h1 class="heading">My Heading</h1>
/* CSS */
.heading {
font-size: 24px;
color: red;
}
8. Not Including Alt Attributes for Images
Alt attributes provide a description of an image that is displayed in case the image fails to load or cannot be displayed. Not including alt attributes can make your web pages inaccessible for users with visual impairments and can negatively affect your search engine rankings. To avoid this mistake, make sure to include relevant alt attributes for all images in your HTML code.
<!-- Bad -->
<img src="image.jpg">
<!-- Good -->
<img src="image.jpg" alt="A beautiful sunset over the ocean">
9. Using Tables for Layouts
While tables can be used to create layouts in HTML, they are not intended for this purpose and can make your code harder to maintain and understand. To avoid this mistake, use CSS and HTML5 layout elements to create your page layouts.
<!-- Bad -->
<table>
<tr>
<td>Header</td>
</tr>
<tr>
<td>Content</td>
</tr>
<tr>
<td>Footer</td>
</tr>
</table>
<!-- Good -->
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>
10. Not Closing Tags Properly
Forgetting to close tags properly can cause issues with rendering and can make your HTML code harder to read and understand. To avoid this mistake, make sure to always close HTML tags properly.
<!-- Bad -->
<div>
<p>This is some text
<!-- Good -->
<div>
<p>This is some text</p>
</div>
11. Using Non-Valid HTML
Using non-valid HTML can cause issues with rendering and can make your web pages inaccessible for some users. To avoid this mistake, make sure to validate your HTML code using a validator tool such as the W3C Markup Validation Service.
12. Not Organizing Your HTML Code
Not organizing your HTML code can make it harder to maintain and understand. To avoid this mistake, use indentation, line breaks, and comments to organize your HTML code.
<!-- Bad -->
<div><p>This is some text</p></div><h1>Heading</h1>
<!-- Good -->
<div>
<p>This is some text</p>
</div>
<h1>Heading</h1>
Conclusion
In conclusion, by avoiding these common HTML mistakes, you can significantly improve the quality of your code. Writing clean, maintainable, and accessible HTML is not only beneficial for your users but also for yourself and other developers who may need to work with your code in the future. Remember to validate your code, organize it properly, and use appropriate HTML elements and attributes for their intended purposes. With these best practices in mind, you can write HTML code that is easier to read, understand, and maintain for yourself and others.
Top comments (12)
Funny how this article is much newer than ChatGPT, and yet that still generates mostly the same headings and descriptions when prompted, and even starts with the phrase "HTML is the backbone of the web".
I know I used Chat GPT, but it was not used for anything wrong or illegal I used it spread knowledge 😄
Ah, but did you properly credit and reference it? Quietly playing the game that suggests you are the originating author? The very dilemma that the new wave of AI represents.
Does chat GPT says to give credit and reference?? I am not playing any game I am just using chatGPT to do something which can be useful for everyone, including me too.
You need ChatGPT to tell you to honestly credit the actual author? Seriously? You don't find that openness, transparency and honesty are useful ethics in this modern world of ours, as to your sources? Well, I think you do value honesty which is good (as you admitted to it in response to an observation that "Funny how this article is much newer than ChatGPT, and yet that still generates mostly the same headings and descriptions when prompted, and even starts with the phrase "HTML is the backbone of the web").
I recommend strongly you do cite your references and sources and credit them when publishing, it is a standard publication ethic and I predict it may come back to bite you if you fail to honour it over time.
okay for sure i will remember this
Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍
Thank you very much 🤞
tag don't have to be closed in this context:
This is valid HTML.
the HTML code you provided is not valid is because the
<p>
tag is not closed before the closing</div>
tag.Let me refer you to the HTML specification:
html.spec.whatwg.org/multipage/gro...
oh now i see, thank you for pointing out 🤞