Common HTML & CSS Mistakes and How to Fix Them

Mark HaverbekeMark Haverbeke
08 Jun, 2025
Common HTML & CSS Mistakes and How to Fix Them

TABLE OF CONTENTS

1 . Not Closing Tags Properly

2 . Inline Styles vs External CSS

3 . Overusing !important

4 . Poor Use of Classes and IDs

5 . Ignoring Responsive Design

6 . Forgetting Accessibility (a11y)

7 . Not Using Semantic HTML

8 . Tools & Techniques for Debugging HTML/CSS

Conclusion

In the fast-evolving world of web development, even the most experienced developers can unknowingly adopt bad habits in HTML and CSS. These small errors can result in broken layouts, poor accessibility, bloated code, and frustrating debugging sessions. In this detailed guide, we'll explore the most common mistakes developers make—and how you can avoid or fix them using best practices, modern tools, and thoughtful coding techniques.

1 . Not Closing Tags Properly

One of the most fundamental mistakes is failing to close HTML tags. While modern browsers attempt to render the DOM intelligently, missing closing tags can cause unpredictable layout shifts and bugs—especially in nested elements.

❌ Incorrect:

<ul>
  <li>Item 1
  <li>Item 2
</ul>

✅ Correct:

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
</ul>

Tip: Use a code linter like HTMLHint or integrate Prettier in your IDE to auto-format and catch unclosed tags.

2 . Inline Styles vs External CSS

While inline styles can seem convenient for quick fixes, they break separation of concerns and hinder scalability.

❌ Using Inline Styles:

<div style="color: red; font-size: 20px;">Hello World</div>

✅ Best Practice: Use CSS classes

<div class="greeting">Hello World</div>
.greeting {
  color: red;
  font-size: 20px;
}

Benefits of external CSS:

  • Easier to maintain and scale
  • Encourages reusability
  • Reduces duplication
  • Enables media queries, pseudo-classes, and hover effects

3 . Overusing !important

The !important rule forces a style to apply, ignoring normal CSS cascade rules. While it may appear to “solve” an issue, overuse leads to a specificity war that becomes hard to manage.

❌ Bad Practice:

.button {
  background-color: blue !important;
}

✅ Better Solution:

  • Increase specificity naturally.
  • Refactor CSS architecture using BEM, SMACSS, or utility-first CSS like Tailwind.
  • Use !important sparingly and only when necessary, like overriding inline styles from third-party components.

4 . Poor Use of Classes and IDs

Using IDs for styling or relying too much on generic classes can result in overly specific and inflexible code.

❌ Using IDs for styling:

#main-heading {
  color: blue;
}

✅ Use classes instead:

.heading-primary {
  color: blue;
}

💡 Why?

  • IDs are unique and carry higher specificity than classes.
  • Classes allow multiple elements to share styles.
  • Better integration with JavaScript, especially in component-based frameworks.

Tip: Use BEM naming conventions for clarity and scalability:

.card__title--highlighted {
  color: red;
}

5 . Ignoring Responsive Design

In 2025, mobile traffic dominates web usage. A site that looks good only on desktop is effectively broken for most users.

❌ No media queries or viewport-based layout:

.container {
  width: 1024px;
}

✅ Use responsive techniques:

.container {
  width: 100%;
  max-width: 1024px;
  padding: 1rem;
}

@media (min-width: 768px) {
  .grid {
    display: flex;
  }
}

Also ensure the meta viewport tag is present:

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

Tools:

6 . Forgetting Accessibility (a11y)

Accessibility is not optional—it’s critical for inclusive design. Many developers unintentionally create barriers for users relying on screen readers or keyboard navigation.

Common Accessibility Mistakes:

  • Missing alt attributes on images
  • Improper heading hierarchy
  • No keyboard focus states
  • Using non-semantic divs for buttons or links

❌ Bad Example:

<div onclick="submitForm()">Submit</div>

✅ Accessible Button:

<button type="submit">Submit</button>

Accessibility Fixes:

  • Use semantic HTML tags
  • Use ARIA roles when necessary
  • Test with screen readers (NVDA, VoiceOver)
  • Use Lighthouse (Chrome) or axe DevTools to audit a11y

7 . Not Using Semantic HTML

Semantic HTML improves accessibility, SEO, and code clarity. Using tags according to their meaning helps screen readers and search engines understand your content better.

❌ Overusing

and :

<div class="header">
  <div class="nav">
    <span>Home</span>
  </div>
</div>

✅ Semantic version:

<header>
  <nav>
    <a href="/">Home</a>
  </nav>
</header>

Semantic Elements to Use:

  • header, nav, main, section, article, aside, footer
  • figure, figcaption
  • strong, em instead of
<b>, <i>

Tip: Use the HTML5 Outliner tool to check your document structure.

8 . Tools & Techniques for Debugging HTML/CSS

Debugging layout or styling issues can be time-consuming without the right tools. Here are effective tools and methods:

Chrome DevTools

  • Use the “Elements” panel to inspect DOM and applied styles
  • Toggle and edit CSS properties in real-time
  • Visualize box model and applied media queries

Code Validators

Browser Extensions

  • Axe DevTools (a11y testing)
  • Web Developer Toolbar
  • Lighthouse Audits for performance and best practices

Visual Debugging

  • Use outline property to visualize elements:
* {
  outline: 1px solid red;
}

Bonus: CSS Debugging Checklist

  • Is the selector matching the right element?
  • Are there any conflicting rules (check specificity)?
  • Are media queries applying correctly?
  • Have styles been overridden by !important or inline styles?

Conclusion

Building solid and scalable front-end code starts with mastering HTML and CSS fundamentals, and avoiding common mistakes that plague beginners and experienced developers alike. By closing your tags properly, respecting the cascade, writing semantic HTML, embracing responsive design, and always considering accessibility, you’ll not only improve your code quality but also deliver better user experiences.

Remember: The best websites are not just beautiful, they’re accessible, performant, maintainable, and meaningful under the hood.

Pro Tip: Make use of linters, formatters, validators, and browser dev tools consistently in your development process to catch these issues early and fix them efficiently.

Mark Haverbeke

Mark Haverbeke

Senior Frontend Engineer

Mark is a passionate software developer and author with expertise in JavaScript and Python. He enjoys simplifying complex programming concepts and sharing practical coding tips.