Having consistent, clean, and tidy HTML code makes it easier for others to read and understand.
Begin your document by declaring the document type as the first line.
For HTML, ensure you use the correct document type declaration.
<!DOCTYPE html> |
While HTML permits mixing uppercase and lowercase letters in element names, it’s advisable to opt for lowercase names. Here’s why:
<body> <p>This is a paragraph.</p> </body> |
<BODY> <P>This is a paragraph.</P> </BODY> |
In HTML, it’s not mandatory to close all elements (e.g., the <p> element).
Nevertheless, we highly recommend closing all HTML elements, following this syntax:
<section> <p>This is a paragraph.</p> <p>This is a paragraph.</p> </section> |
<section> <p>This is a paragraph. <p>This is a paragraph. </section> |
While HTML permits mixing uppercase and lowercase letters in attribute names, it’s advisable to opt for lowercase names. Here’s why:
<a href=”https://www.w3schools.com/html/”>Visit our HTML tutorial</a> |
<a HREF=”https://www.w3schools.com/html/”>Visit our HTML tutorial</a> |
Although HTML permits attribute values without quotes, it’s advisable to use quoted attribute values. Here’s why:
<table class=”striped”> |
<table class=striped> |
This approach won’t function as expected due to the presence of spaces in the value.
<table class=table striped> |
It’s crucial to always include the alt attribute for images. This attribute serves as vital descriptive text if the image fails to display for any reason.
Additionally, it’s advisable to define the width and height of images. Doing so minimizes flickering by allowing the browser to allocate space for the image before loading it.
<img src=”html5.gif” alt=”HTML5″ style=”width:128px;height:128px”> |
<img src=”html5.gif”> |
While HTML permits spaces around equal signs, it’s generally easier to read and organize code without them. Keeping attribute-value pairs space-less enhances readability and groups entities more effectively.
<link rel=”stylesheet” href=”styles.css”> |
<link rel = “stylesheet” href = “styles.css”> |
When working with an HTML editor, it’s inconvenient to constantly scroll horizontally to read the HTML code.
To enhance readability and usability, it’s best to avoid excessively long lines of code.
Avoid inserting blank lines, spaces, or indentations arbitrarily in your code.
For improved readability, include blank lines to separate sizable or cohesive code blocks.
Additionally, for enhanced readability, utilize two spaces for indentation instead of the tab key.
<body> <h1>Famous Cities</h1> <h2>Tokyo</h2> <p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area, and the most populous metropolitan area in the world.</p> <h2>London</h2> <p>London is the capital city of England. It is the most populous city in the United Kingdom.</p> <h2>Paris</h2> <p>Paris is the capital of France. The Paris area is one of the largest population centers in Europe.</p> </body> |
<body> <h1>Famous Cities</h1> <h2>Tokyo</h2><p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area, and the most populous metropolitan area in the world.</p> <h2>London</h2><p>London is the capital city of England. It is the most populous city in the United Kingdom.</p> <h2>Paris</h2><p>Paris is the capital of France. The Paris area is one of the largest population centers in Europe.</p> </body> |
<table> <tr> <th>Name</th> <th>Description</th> </tr> <tr> <td>A</td> <td>Description of A</td> </tr> <tr> <td>B</td> <td>Description of B</td> </tr> </table> |
<ul> <li>London</li> <li>Paris</li> <li>Tokyo</li> </ul> |
The <title> element is a mandatory component in HTML.
The content within the page title carries significant importance for Search Engine Optimization (SEO). Search engine algorithms utilize the page title to determine the ranking and order of pages listed in search results.
The <title> element performs several crucial functions:
Hence, it’s essential to craft the title with precision and relevance to enhance its effectiveness.
<title>HTML Style Guide and Coding Conventions</title> |
An HTML page can pass validation even without the presence of <html> and <body> tags.
Example
<!DOCTYPE html> <head> <title>Page Title</title> </head> <h1>This is a heading</h1> <p>This is a paragraph.</p> |
However, it’s highly advisable to consistently include the <html> and <body> tags in your HTML documents.
Excluding the <body> tag may lead to errors, particularly in older browser versions.
Furthermore, omitting both the <html> and <body> tags can potentially cause crashes in DOM and XML software.
The HTML <head> tag is optional.
Browsers automatically append all elements before <body> to a default <head> element if the <head> tag is omitted.
Example
<!DOCTYPE html> <html> <title>Page Title</title> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html> |
Nevertheless, we advise employing the <head> tag.
Closing empty elements in HTML is optional.
<meta charset=”utf-8″> |
<meta charset=”utf-8″ /> |
If you anticipate XML/XHTML software accessing your page, ensure to include the closing slash (/) as it’s mandatory in XML and XHTML.
Including the lang attribute within the <html> tag is essential as it declares the language of the web page, aiding search engines and browsers in proper interpretation.
Example
<!DOCTYPE html> <html lang=”en-us”> <head> <title>Page Title</title> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html> |
For accurate interpretation and optimal search engine indexing, it’s crucial to define both the language and character encoding <meta charset=”charset”> as early as possible within an HTML document.
<!DOCTYPE html> <html lang=”en-us”> <head> <meta charset=”UTF-8″> <title>Page Title</title> </head> |
On web pages, the viewport refers to the visible area for users, which changes according to the device being used – it’s typically smaller on mobile phones compared to computer screens.
It’s advisable to include the following <meta> element in all your web pages:
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″> |
This provides the browser with directives on managing the page’s dimensions and scaling.
The “width=device-width” component adjusts the page’s width to match the device’s screen width, adapting to different devices.
The “initial-scale=1.0” portion establishes the initial zoom level when the browser loads the page.
Below is an illustration comparing a webpage before and after adding the viewport meta tag:
Hint: If you’re viewing this page on a phone or tablet, try tapping the two links below to observe the contrast. |
Concise comments should be single-lined, such as:
<!– This is a comment –> |
Multi-line comments should be formatted as follows:
<!– This is a long comment example. This is a long comment example. This is a long comment example. This is a long comment example. –> |
Long comments are more readable when they are indented with two spaces for clarity.
Simplify linking to style sheets by omitting the type attribute.
<link rel=”stylesheet” href=”styles.css”> |
Short CSS rules can be compressed for brevity, like so:
p.intro {font-family:Verdana;font-size:16em;} |
For lengthy CSS rules, it’s advisable to spread them across multiple lines:
body { background-color: lightgrey; font-family: “Arial Black”, Helvetica, sans-serif; font-size: 16em; color: black; } |
Ensure consistency in CSS formatting:
Simplify the syntax for loading external scripts by omitting the type attribute.
<script src=”myscript.js”> |
Employing messy HTML code may lead to JavaScript errors.
These two JavaScript statements yield distinct outcomes:
Example
getElementById(“Demo”).innerHTML = “Hello”; getElementById(“demo”).innerHTML = “Hello”; |
Certain web servers, such as Apache on Unix systems, are case-sensitive regarding file names. For example, “london.jpg” cannot be accessed as “London.jpg”.
Conversely, other servers like Microsoft’s IIS are not case-sensitive. Thus, “london.jpg” can be accessed as “London.jpg”.
When employing a combination of upper and lowercase, it’s crucial to acknowledge this distinction.
Transitioning from a case-insensitive to a case-sensitive server can result in even minor discrepancies breaking your website.
To circumvent these issues, consistently employ lowercase file names!
HTML files are ideally given a .html extension (although .htm is permissible).
CSS files should end with a .css extension.
JavaScript files should conclude with a .js extension.
The .htm and .html file extensions are functionally identical.
All web browsers and web servers interpret both extensions as HTML.
When a URL lacks a specified filename at the end (e.g., “https://www.code7schools.com/“), the server typically appends a default filename like “index.html”, “index.htm”, “default.html”, or “default.htm”.
If your server is set up to recognize only “index.html” as the default filename, your file should be named “index.html” rather than “default.html”.
However, servers often allow configuration with multiple default filenames, giving you the flexibility to set up as many as needed.