๐ Testing and Debugging HTML and CSS
Introduction to Debugging
Debugging is the process of identifying and fixing errors in your code. Whether it's a missing HTML tag, a misplaced CSS property, or an accessibility oversight, debugging is an essential skill for web developers.
This lesson will help you understand common debugging techniques using industry-standard tools like Visual Studio Code and browser Developer Tools.
Why Debugging Matters
Errors in your code can lead to:
- Broken layouts
- Non-functional features
- Accessibility issues
Debugging early and often saves time and prevents frustration by ensuring your web pages meet the required standards and function as intended.
Common Errors in HTML and CSS
- HTML Errors
- Examples include missing closing tags, improper nesting, and incorrect attributes.
- CSS Errors
- Issues such as conflicting styles, broken layouts, and invalid property names.
- Accessibility Violations
- Errors like missing
altattributes, insufficient color contrast, and non-descriptive link text.
Types of Errors by Severity
๐จ Critical Errors (Highly Pronounced)
- HTML Structural Issues: Missing closing tags, improperly nested elements, and incorrect
<!DOCTYPE>declarations. - CSS Issues: Broken layouts, overlapping content, or elements disappearing due to incorrect styles.
- Accessibility Violations: Missing form labels, no keyboard navigability, or absent
altattributes on images.
โ ๏ธ Moderate Errors (Subtle but Noticeable)
- HTML Issues: Misusing HTML for layout, incorrect attributes, or redundant elements.
- Common Misuses:
- Using
<br>tags for spacing instead of CSS. - Applying inline styles directly in HTML.
- Using heading tags for appearance instead of document structure.
- Using
- CSS Issues: Improper media queries, mismatched styles, or conflicting selectors.
- Accessibility Violations: Poor contrast ratios, improper heading hierarchy, or overuse of ARIA roles.
๐ Minor Errors (Barely Noticeable or Hidden)
- HTML Issues: Unused tags, invalid attributes, or missing meta tags.
- CSS Issues: Overly specific selectors, deprecated properties, or inefficient animations.
- Accessibility Violations: Missing focus styles, vague links, or subtle animation timing issues.
Debugging Techniques
๐ Validate Your Code
- Validate HTML at W3C HTML Validator.
- Validate CSS at W3C CSS Validator.
- Check accessibility with WAVE Accessibility Checker.
- Check browser compatibility with Can I Use.
๐ ๏ธ General Practices
- Work incrementally: make small changes and test frequently.
- Watch for red underlines in VS Code or other IDEs; hover for quick tips.
- Use View Page Source and browser Inspect tools to view live HTML structure.
- Comment out blocks of code to isolate issues:
<!-- HTML comment -->or/* CSS comment */
๐ฆ Check Required Assets
- Ensure all files (CSS, JS, images) are saved locally or uploaded to the server.
- Check that paths are correct and files are in the right directory.
- If your entire page disappears after adding styles, you may be missing a closing
</style>tag.
๐ Troubleshooting URLs and File Paths
- Make sure you have an
index.htmlfile in the root of your GitHub Pages project. Without it, your site won't load automatically. - If your homepage isn't
index.html, you must include the filename in the URL. Example:https://username.github.io/repo/otherpage.html - Check your relative paths carefully.
./images/logo.pngand../images/logo.pngpoint to very different places. - Use your browser's Dev Tools โ Network tab to confirm whether linked files (images, stylesheets, etc.) are loading correctly.
๐ง Pro Tip: File paths are case-sensitive on GitHub Pages. About.html is not the same as about.html.
๐ฑ Test Across Devices
- Test in multiple browsers (Chrome, Firefox, Safari, Edge).
- Check responsiveness with browser tools (desktop, tablet, mobile views).
๐ก Programming Errors
- Watch for missing HTML closing tags and improper nesting.
- Check for missing CSS braces, colons, or semicolons.
- Take a break! A fresh look often reveals the issue.
Advanced Techniques
- ๐งช Perform a hard refresh to clear your browser cache.
- ๐ง Open Dev Tools with Ctrl+Shift+I (PC) or Cmd+Opt+I (Mac). Check the Console for errors.
- ๐งฉ Use the Elements tab in Dev Tools to inspect styles. Look for properties that are crossed out (strikethrough) or greyed out โ they're being overridden.
If the solution isn't obvious, look for info
and help
icons on your tools or LMS interface.
Real-World Debugging Examples
Example 1: Fixing a Broken Form
Severity Level: Critical
- Problem
- A form is missing
<label>elements and uses self-closing tags for void elements, which can cause validation warnings and accessibility issues.
<form>
<input type="text" name="username" />
<input type="submit" />
- Symptoms
- The form renders but lacks proper labeling. Screen readers can't associate input fields with their purpose, and validators may flag unnecessary self-closing slashes.
- Solution
- Add
<label>elements and remove self-closing slashes in HTML5.
<form>
<label for="username">Username:</label>
<input id="username" type="text" name="username">
<input type="submit" value="Submit">
</form>
Example 2: Broken Link to Stylesheet
Severity Level: Moderate
- Problem
- A misspelled filename in the
hrefattribute causes the external CSS file not to load.
<link rel="stylesheet" href="stlyes.css"> <!-- filename is misspelled -->
- Symptoms
- The styles from the external stylesheet do not apply at all. The browser renders default styles.
- Solution
- Use Dev Tools to inspect the element and confirm the stylesheet is loading in the Network tab. Check for selector typos in the Elements panel.
<link rel="stylesheet" href="styles.css"> <!-- filename is spelled correctly-->
Example 3: Identifying Subtle Accessibility Issues
Severity Level: Moderate to Minor
- Problem
- A design uses foreground and background colors that don't meet accessibility contrast standards, making text difficult to read for many users.
.text-container {
background-color: #C0C0C0;
color: #FFFFFF;
}
- Symptoms
- Text appears very faint or unreadable, especially for users with low vision or color blindness. This may not be flagged by a standard HTML validator.
- Solution
- Use a contrast checker. Adjust the colors for readability
.text-container {
background-color: #333333;
color: #FFFFFF;
}
๐งพ Debugging Checklist
- โ Use validators (HTML, CSS, accessibility)
- โ Check asset links and file paths
- โ Inspect layout and styles with Dev Tools
- โ Test on multiple browsers/devices
- โ Use contrast checkers for accessibility
- โ Keep your code clean and modular
- โ Take breaks and come back with fresh eyes
References
- MDN: Debugging HTML
- MDN: Debugging CSS
- The A11y Project โ accessibility tips and tools
- Can I Use โ browser compatibility tool
Last updated: August 14, 2025 at 10:12 PM