- HTML Tutorial
- HTML - Home
- HTML - Introduction
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Headings
- HTML - Paragraphs
- HTML - Fonts
- HTML - Blocks
- HTML - Style Sheet
- HTML - Formatting
- HTML - Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - Style Guide
What is Style Guide in HTML?
In HTML, the style guide is a set of rules and conventions that define how to write and format code. It helps to ensure that the written code is readable, understandable and easy to modify. A style guide also helps to avoid common errors and bugs that can affect the functionality and appearance of a web page. In this tutorial, we are going to cover the most important style guidelines for creating better HTML code.
Start with HTML5 Doctype
Always start all HTML and XHTML documents with <!DOCTYPE html> declaration. Note that this declaration is case-insensitive. It is required to enforce the full standard mode (also known as no-quirks mode) to provide more consistent rendering of web pages. It is the latest web standard described by W3C and used by the layout engines of modern web browsers.
Example
<!DOCTYPE html> <html> <head> <!-- Content inside head tag --> </head> <body> <!-- Content inside body tag --> Hello, Welcome to Tutorialspoint </body> </html>
Specify document language
Always specify the document language with the help of lang attribute. This attribute is defined within the opening <html> tag, which is the root of HTML document. Specifying the document language will help in accessibility, speech analysis, translation and other functionalities.
Example
<!DOCTYPE html> <html lang="en"> <head> <!-- Content inside head tag --> </head> <body> <!-- Content inside body tag --> Hello, Welcome to Tutorialspoint </body> </html>
Define Charset
The W3C always recommend developers to declare the charset or character encoding explicitly. It can be done by using the charset attribute of <meta> tag. Pass UTF-8 as a value to charset attribute as it is the most commonly used character encoding and provides over a million characters.
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset = "UTF-8"> </head> <body> <!-- Content inside body tag --> Hello, Welcome to Tutorialspoint </body> </html>
Use Lowercase for elements and attributes
Don’t capitalize the element names, attribute names and values of attributes. In other programming languages like Java, C and C++, developers often use the camel case or snake case for declaring the variable names. However, for writing HTML code, W3C recommends the use of lowercase letters. Doing so will enhance the clarity and readability of our HTML code.
Example
<body> <h1> <!-- Heading comes here --> </h1> <p style = "font-size: 25px; "> <!-- contains paragraph --> Hello, Welcome to Tutorialspoint </p> </body>
Quote the attribute values
According to W3C recommendations, it is better to enclose attribute values in double quotes. This is important for values containing spaces, as HTML may not parse them correctly without the quotes. The use of double quotes is more common than single quotes. However, we can use single quotes as well.
<p style = "font-size: 25px; "> <!-- contains paragraph --> </p>
Use closing tags
In HTML, all elements must be closed properly, even if some elements have optional closing tags. Please note that there are certain elements that are self-closing including <img>, <hr>, <br> and many more.
Example
<!DOCTYPE html> <html> <head> <!-- Content inside head tag --> </head> <body> <h1> <!-- Heading comes here --> </h1> <p> <!-- contains paragraph --> </p> <br> <p> <!-- contains paragraph --> Hello, Welcome to Tutorialspoint </p> </body> </html>
Use proper Indentation
The use of proper indentation shows the structure and hierarchy of our HTML code. Use spaces instead of tabs for indentation, and use a consistent number of spaces (usually two or three) per level. Also, we can use the blank lines to separate large code blocks.
Example
<!DOCTYPE html> <html> <head> <!-- Content inside head tag --> </head> <body> <h1> <!-- Heading comes here --> </h1> <p> <!-- contains paragraph --> </p> <p> <!-- contains paragraph --> Hello, Welcome to Tutorialspoint </p> </body> </html>
Set the viewport
Setting the viewport helps web pages render well on different devices. It is achieved by controlling the width and scale of the page. It is used for ensuring the responsiveness of a particular web page.
<meta name = "viewport" content = "width=device-width, initial-scale = 1.0">
Add Comments
We use comments to explain the purpose or functionalities of the particular HTML code. Comments should start with <!-- and end with -->. Avoid using comments for styling or scripting purposes.
Example
<!DOCTYPE html> <html lang="en"> <head> <!-- Content inside head tag --> </head> <body> <h1> <!-- Heading comes here --> </h1> <p> <!-- contains paragraph --> Hello, Welcome to Tutorialspoint </p> </body> </html>