HTML and CSS Validation
Overview
Writing HTML markup and CSS can easily lead to mistakes in the syntax. We can use validation tools to help write standards-based HTML markup and CSS.
Prepare
Validation means to check the HTML or CSS code provided to the validator tool and compare it with the rules of the accompanying doctype and report on the possible errors, warnings, and other info to consider. In this class, we will use the Web Developer browser extension tool to drive our HTML and CSS validation checks which you were asked to install as part of the course setup.
- Review Setup Activity: 📄 'Web
Developer' Browser Extension - w3schools.com
An demonstration of how to use the tools was provided in the setup activity.
VS Code has built in help with HTML and CSS. For example, it will provide squiggly underlining of possible issues with your HTML and CSS. Hovering over the squiggly line will provide a description of the issue. You can also use the Problems tab to see a list of issues.
In this example screenshot, a closing } was left off the footer selector rule in CSS. VS Code provides a red squiggly underline to indicate a problem.
The gray } on line #73 in the figure is the system providing a possible fix which you can accept using the tab key when in focus.
Activity Instructions
Validate HTML
- Open this demonstration page in your browser. This page contains some HTML issues.
- Validate the HTML by using the Web Developer browser extension that you have already added to your browser.
- Note and identify the errors, warnings, and information provided by the validation report.
- You should see a report of over 12 errors, warning, and info.
- On paper, try to fix the first five messages.
Answers
- Warning: The opening html tag should have a language attribute.
<html lang="en-US">
- Info: Remove the trailing slash on any void element like meta tags.
Not required but now considered good practice to remove them.
<meta charset="UTF-8">
- Error: The required title element was missing in the head.
<title>This title should match up with the heading 1</title>
- Error: List elements cannot contain anything beside list items as
direct children. Probably change the
<div>
divider to a<li>
list item within the<ul>
list element. - Error: Do not use units in width and height attributes for
<img>
image elements.
The pixel unit is implied in HTML.width="200" not width="200px"
- Warning: The opening html tag should have a language attribute.
- ☑️ Post questions you have about fixing any of the remaining errors found in the General channel of MS Teams.
Validate CSS
- With the same page open, now validate the CSS using the Web Developer browser extension.
- Note and identify the line numbers of the CSS issues.
- What are the issues and how might they be fixed?
Answers
- Line 9 - Value Error: font-size property value should be
1.2pt
- Line 20 - Value Error: padding should be
5px
- Line 70 - Property doesn't exist. A suggestion should be provided.
- Line 9 - Value Error: font-size property value should be
Tips
- The validator will tell you the line number and column where it found the error. Use this as a starting point when searching for the real issue.
- Start with the top of the list and work down when validating. Often fixing one issue will resolve additional listed errors/warnings.
- Do not try to fix everything at once. You may have to validate more than once as you start fixing issues.
Submission
- Share anything that you find interesting or may have some questions about with the class in MS Teams.
Optional Resources
- Debugging HTML - MDN
- Debugging CSS - MDN