HTML page tips
Recommendations for developing HTML pages.
Last updated on: 2024-12-09
Let's first show you the actual full page template, then we'll get into each part and explain why it's there.
Recommended page template:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><!-- TODO --></title> <meta name="description" content="<!-- TODO -->"> <link rel="icon" href="/favicon.ico" sizes="any"><!-- 32×32 --> <link rel="icon" href="/icon.svg" type="image/svg+xml"> <link rel="apple-touch-icon" href="/apple-touch-icon.png"><!-- 180×180 --> </head> <body> <!-- TODO --> </body> </html>
PS:
- Do not forget to modify the `lang`'s attribute to your app's language if needed.
- Serve your HTML page with a `Content-Type` of `text/html; charset=utf-8`.
Use "standards" compatibality mode
Ensure you have a `<!DOCTYPE html>` tag at the beginning of your HTML document: Otherwise, the document will be rendered in "quirks" mode, which would instruct the browser to emulate quirks in CSS and Selectors of old versions of some web browsers (ex: Internet Explorer).
References:
Official Quirks specs Quirks mode explained on MDNSet the global language attribute
<html lang="en">
References:
Global "lang" attribute explained on MDNCharacter set
Should be positioned right after the <head> tag.
<head>
<meta charset="UTF-8">
The charset declaration should be inserted in the first 1024 bytes of the HTML file. Ideally, put it right after opening the "head" tag.
Viewport
<meta name="viewport" content="width=device-width, initial-scale=1">
Favicon(s)
<link rel="icon" href="/favicon.ico" sizes="any"><!-- 32×32 --> <link rel="icon" href="/icon.svg" type="image/svg+xml"> <link rel="apple-touch-icon" href="/apple-touch-icon.png"><!-- 180×180 -->
Optional: Web app manifest
Note: ".webmanifest" files should be served with a `Content-Type` of `application/manifest+json`.
<link rel="manifest" href="/app.webmanifest">
Example Web App Manifest file:
{
"short_name": "MDN",
"name": "MDN Web Docs",
"icons": [
{"src": "/favicon.192x192.png", "type": "image/png", "sizes": "192x192"},
{"src": "/favicon.512x512.png", "type": "image/png", "sizes": "512x512"}
],
"start_url": "/?utm_source=pwa",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
Requirements to be installable:
Web Manifest file explained on MDN PWA installation criteriaOptional: Structured information
Various structured formats have been used on web pages over the years to provide information to third party programs that may need structured information about your app such as search-engine crawlers and social media apps.
Common formats are:
- JSON+LD
- Open Graph (OG) Meta Tags
They enable third party apps to provide a better user experience by showing your page with more information (ex: picture, author, and metadata).
Optional: Meta theme color
<meta name="theme-color" content="#dadec0">