DEV Community

Vaibhav Kumar
Vaibhav Kumar

Posted on

12 Common HTML Mistakes to Avoid for Better Code Quality

HTML

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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">
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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">
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>

Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

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".

Collapse
 
devxvaibhav profile image
Vaibhav Kumar

I know I used Chat GPT, but it was not used for anything wrong or illegal I used it spread knowledge 😄

Collapse
 
thumbone profile image
Bernd Wechner

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.

Thread Thread
 
devxvaibhav profile image
Vaibhav Kumar

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.

Thread Thread
 
thumbone profile image
Bernd Wechner • Edited

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.

Thread Thread
 
devxvaibhav profile image
Vaibhav Kumar

okay for sure i will remember this

Collapse
 
fruntend profile image
fruntend

Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍

Collapse
 
devxvaibhav profile image
Vaibhav Kumar

Thank you very much 🤞

Collapse
 
punund profile image
punund

tag don't have to be closed in this context:

<div> foo
  <p> bar
</div>
Enter fullscreen mode Exit fullscreen mode

This is valid HTML.

Collapse
 
devxvaibhav profile image
Vaibhav Kumar

the HTML code you provided is not valid is because the <p> tag is not closed before the closing </div> tag.

Collapse
 
punund profile image
punund

Let me refer you to the HTML specification:

Tag omission in text/html:
A p element's end tag can be omitted if the p element is immediately followed by an address, article, aside, blockquote, details, div, dl, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, hr, main, menu, nav, ol, p, pre, search, section, table, or ul element, or if there is no more content in the parent element and the parent element is an HTML element that is not an a, audio, del, ins, map, noscript, or video element, or an autonomous custom element.

html.spec.whatwg.org/multipage/gro...

Thread Thread
 
devxvaibhav profile image
Vaibhav Kumar

oh now i see, thank you for pointing out 🤞