How to check for errors in HTML ?
Last Updated :
31 Oct, 2021
HTML5 is easy to understand and it's not compiled into different forms before the browser displays it.
In any code, 2 types of errors are possible:
- Syntax error: Incorrect syntax leading to compile time error in other languages.HTML have no affect of syntax error.
- Logical error: Syntax is correct but output is unexpected because of wrong logic.
In HTML we never encounter syntax errors because the browser parses HTML permissively, which means that the page is displayed even if there are any syntax errors. Browsers have some built-in rules to display incorrect HTML. Therefore there would always be some output, even if it is not what is expected.
The way that browsers parse HTML is a lot more permissive than how other programming languages are run, which leads to both good (content gets displayed) and bad scenarios (content is displayed in an unexpected manner).
In the below example, there are some syntax errors like incomplete p tag, incomplete h1 tag, but still, it displays some of the content in an expected way. In this case, the initial p tag and h1 tag but the next p tag is displayed as h1. These scenarios are easy to identify and avoid in small codes but when the length of code increases, it will be complex to debug code.
Example:
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML errors</title>
</head>
<body>
<h1>HTML errors</h1>
<p>unclosed paragraph tag
<h1>its displaying the way it is intended
<p>but it may lead to next unexpected thing
</body>
</html>
Output:
Validating our HTML: In order to make sure your html code is error free is by using html validation service by w3c. This site takes html as input and returns the errors in html document. You can provide html by providing link to html document, uploading html file or directly pasting html there.
W3C interface
Example: Let's try by directly pasting html. Here is the result for the code given above. The errors in the HTML are highlighted, so now we can make changes accordingly in html and make it error free.
Errors
The error free HTML is shown below.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h2>Welcome To GFG</h2>
<p>Default code has been loaded into the Editor.</p>
</body>
</html>
Similar Reads
How to turn off spell checking for HTML form elements ? You may have noticed often that while filling a form in any website if we made any spelling or grammatical mistake it would get underlined by a red wavy line. It is sometimes annoying for the user as he/she may get distracted by it. So, many websites turn off the Spell Check Feature for their users.
2 min read
How to Display an Error for Invalid Input with PHP/HTML? PHP can be easily embedded in HTML files, and HTML code can also be written in a PHP file. The key difference between PHP and a client-side language like HTML is that PHP code is executed on the server, while HTML code is directly rendered in the browser. To display errors for invalid input using HT
2 min read
How to show All Errors in PHP ? We can show all errors in PHP using the error_reporting() function. It sets the error_reporting directive at runtime according to the level provided. If no level is provided, it will return the current error reporting level. error_reporting(E_ALL) level represents all errors, warnings, notices, etc.
3 min read
How to Compare Two HTML Elements? Comparing two HTML elements can be useful for determining if they are the same in terms of tag name, attributes, content, or structure. This is often needed when working with dynamic web applications or creating custom validation routines.These are the following approaches to compare HTML elements:T
3 min read
How to enable error reporting in PHP ? In PHP, we can decide whether to show an error to end-users or not. You can enable or disable error reporting in PHP with three approaches: Approach 1: In the php.ini file, we can set the display_error parameter as on or off. The on means errors are displayed and off means no errors to display and r
3 min read
How to create form validation by using only HTML ? In Web Development, we often use JavaScript with HTML to validate the form, but we can also do the same via HTML in the following ways. HTML <input> required Attribute HTML <input> type Attribute HTML <input> pattern Attribute HTML <input> required Attribute: In input tag of
2 min read