HTML
To scroll to the top of the page use <a href='#'>
.
for no script, add a descriptive line why your site would not work, and provide an alternative to use ublock origin and tell them what external site resources you are using on the current website.
The first link on page is skip to content link
It helps people skip the main content instead of tabbing through the navigation bar. This should be visible when focused, otherwise you can hide it .visually-hidden:not(:focus):not(:active)
.
Attributes
For boolean attributes, like required
- If you use it, then it is used.
- To not use it, remove it from attribute list. Do not do required=false, since setting any value for this attribute would make it true.
Template
<!DOCTYPE html>
Use standard mode and prevent browser from switching to quirks mode, which is used for Internet Explorer 5 and Netscape Navigator 4.
<html>
<head>
<title>
Shown in browser's title bar.
- Avoid one or two word titles. Use a descriptive phrase.
- Search engines will display the first 55-60 characters, so limit titles to this length and have the most important info in this range.
- Don't use keyword blobs as that negatively impacts SEO.
- Try to make the titles as unique as possible within your own site.
For accessibility, people will read the page title and infer the contents of the page. So make the title unique to every page of the website, ideally surfacing the primary purpose of the page, followed by the name of the website.
Titles can also be used alongside forms. If form refreshes the page, then title can be used to show the errors also.
<style>
<base>
Not used much. Used to set the base URL for relative links on the page.
For an in-page anchor <a href="#some-id">
, using <base>
would trigger an HTTP request to the base URL with the fragment attached. Due to this reason using <base>
is avoided.
<link>
Used to link external resources to the webpage. Specify the relationship using rel
attribute
-
alternate
- provide alternate representation of the page (different stylesheet, rss feed). -
author
- to provide more information about the author of the document -
canonical
- To provide preferred URL for the current document.- If your site is available in multiple translations, then provide the link to the "main language" (like English) as the authoritative source of truth.
- Use it to link to the original source of the blog post when publishing to external sites.
-
help
- provide link to get help for the current document, like documentation page. -
icon
- specify the favicon image which is shown in the title bar of browser, or when a website is bookmarked on desktop (using SVG is preferred).The image can be controlled by adding the attributes
media
- use the image when the media query is valid.type
- the type of image like png, jpgsizes
- specify the sizes of the icon.any
- means the specified icon can be scaled to any size (used withtype='image/svg+xml'
)<width in pixels>x<height in pixels>
- SVG is preferred, but this option is also available in case the icon file contains multiple sizes (not used much).
Use MANIFEST.json instead
Instead of duplicating the above
<link>
tags on every page, write them inMANIFEST.json
file. -
license
- to provide license info -
manifest
- to provide manifest file for Progressive Web Apps. Check this for more info on manifest file. -
next
- when the current page is part of a series, like a blog website. Usenext
to specify the next page in the series. -
prev
- similar tonext
. Use it to specify the previous page in the series. -
stylesheet
- to import external stylesheets -
dns-prefetch
- to improve page load times, by doing DNS lookup to get the IP address of the resource.- If the page is going to make requests to external domains (due to scripts, styleheets, images, libraries), then use
dns-prefetch
to instruct the browser to resolve the domain name in advance. - Put this tag near the start of
<head>
, so the browser can start DNS resolution as soon as possible. - You do not need this tag for resources that are accessed immeditaly as the page load's (like stylesheets, scripts) as the browser will automatically perform DNS resolution for them.
- This tag is used for resources that will be fetched conditionally or due to user interactions, as it can save latency time.
- If the page is going to make requests to external domains (due to scripts, styleheets, images, libraries), then use
-
modulepreload
- Long JS scripts can be broken into modules, and each module can be loaded separately. Consider this example
(
.mjs
used instead of.js
to signify the file is a module file)
The modules can be imported using
modulepreload
will download the module (multiple modules will be downloaded in parallel) and its dependencies, parse and compile it, and put the results into the module map to that it is ready to execute (be careful when doing this, as you would be taking resources from the main render). - Long JS scripts can be broken into modules, and each module can be loaded separately. Consider this example
(
-
preload
- similar tomodulepreload
, but it only downloads the file (or think offetch
statements) and stores the result in cache.Used to specify resource that your page will need very soon, and using
preload
will start getting these resources before browsers' main render cycle has started. The benefit is when the browser starts rendering the page, there are less blocks to the page's render (as resources are already fetched), thus improving performance.The following types (
as
) can be preloadedaudio
document
- HTML document intended to be embedded by a<frame>
,<iframe>
embed
fetch
font
image
- useimagesizes
,imagesrcset
to define responsive images.object
- resource embedded inside<object>
script
style
track
worker
- JS web workervidoe
-
preconnect
- provide a hint to the browser suggesting that it can open a connection to the linked website in advance, without disclosing any private info or downloading any content, so that when the link is followed the linked content can be fetched more quickly.Establishing a connection means doing DNS lookup, TCP handshake, TLS negotiation.
-
prefetch
- provide hint to browser to fetch and cache the target resource as it is likely to be required for a followup navigation or interaction.It is different from
preload
as the resources are fetched during idle time. Use it to prefetch resources for the next page that user might naviagte to, and for non-critical assets that might enhance the user experience if prefetched but aren't immediately necessary.In
preload
the reosurces are neeed immediately for the current page. -
pingback
- Used for pingback specification. Pingback is used by web authors to request notification when somebody links to their documents i.e. keep track of who is linking to, referring to their articles.Working
- I write a blog post on
kushajveersingh.com/post1.html
. - Someone reads my post, and links it in their own article at
some-other-domain.com/post.html
using<a href="kushajveersingh.com/post1.html>post</a>
. - Now the software used for publishing the
some-other-domain.com/post.html
(like Wordpress), will check the external links and check if they have a<link rel="pingback">
defined and send a message to that. - My
server-script.php
which accepts these requests, would verify the correctness of the ping (to ensure that it is not spam), and then do something with it.
- I write a blog post on
-
search
- (not used much) link to external search tool or plugin that can be used to search the current website, generally used with OpenSearch by Amazon.Opensearch was popular back in 2010s, but now there are better alternatives like ElasticSearch.
Other useful attributes of <link>
-
crossorigin
- make Cross-Origin Resource Sharing (CORS) requests with or without sending credentials (cookies, HTTP headers) -
fetchpriority
- provide a hint to the browser. Can behigh
low
auto
- determine fetch priority relative to other resources of the same type.
-
integrity
- to ensure the fetched resource hasen't been tampered with. It does so by comparing the cryptographic hash of the fetched resource to the expected hash provided inintegrity
attribute (generally used with CDNs and third-party resources).-
Generate hash (use sha256, sha384, or sha512)
-
Copy the hash to
<link>
-
<meta>
-
charset
- define the character encoding for the document (applies to css, js as well, meaning you have use emojis as variable names in JS). The default encoding in windows-1252. -
http-equiv
- Takes the following values-
content-security-policy
- to set Content Security Policy (CSP) (defined in HTTP spec) for the page, to help prevent cross-site scripting (XSS) and data injection attacks.Three type of XSS attacks
- Stored XSS - a user can add malicious script on the target server (e.g. by posting it in the forum, comment section). And when the user visits the affected page, the script runs in their browser.
- Reflected XSS - attacker crafts a URL containing malicious script input. The victim clicks on the URL, and the malicious code is sent to the server, and the server reflects the input back in the page content (e.g. in an error message or search result) and the victim's browser executes the malicious script.
-
DOM-based XSS - attacker injects a payload directly into the DOM. For example if you have JS to read the part of URL after the hash
Then the above code can be exploited by using the URL
http://vulnerable-website.com/page#<img src=x onerror=alert('XSS')>
For example, you can specify which sources of content (scripts, styles, images) are allowed to be laoded and executed by the browser
List of all the
content
values can be found in Content-Security-Policy page of HTTP.Setting HTTP headers is preferred
Setting HTTP headers through the backend server is preferred over using
<meta>
tags. Use<meta>
for cases, when site is being deployed statically. -
default-style
- used when providing alternate stylesheets, to set the default. -
refresh
-
to auto-refresh the current page after
content
number of seconds (useful in live dashboards or monitoring tools, news/control feeds, session management to keep the user logged in while user is idle and start a timer, queuing systems where people are placed in a virtual queue and their position needs to be updated live). -
to redirect to a different URL after
content
number of seconds (this is discouraged in modern web) and has negative impact on SEO. Usewindow.location.href = 'new url'
instead.
-
-
-
name
- to provide document level metadata. The followingname
values are supported in HTML spec-
application-name
- use to provide a short name to web application. Do not use with website or content sites. Similar to<title>
but title can be a long string, describing the current page, whileapplication-name
is supposed to be a short concise name (that will be shown in title bar or when bookmarked). -
author
- name of document author -
description
- short and accurate summary of the content of the page -
generator
- if you used a software to generate the current site like Wordpress, Astro -
keywords
- comman separated list of keywords to describe the page. Do not use now, as browsers ignore this, as people started exploiting the keywords. -
referrer
- set it through HTTP header instead. When you click a link to an external site, then your info (i.e. the link from which the user came) would be shared with the external site. To prevent this you can use referrer. -
theme-color
- To set the color of surrounding user interface like the browser's address bar or toolbar. -
color-scheme
- set the default theme for the page to light/darkAnd use
(prefers-color-scheme: dark)
to define the styles. -
viewport
- give hint to the browser about the intial size ofviewport
.width
- (pixel value ordevice-width
) width of viewport you want the website to be rendered atheight
- (pixel value ordevice-height
) (not used by browsers)initial-scale
- 0.0 to 10.0, to define the ratio between device width (in portrait mode, or height in landscape mode) and the viewport size.
-
robots
- give hint to the search engines to not index site and follow links. The default is to allow indexing and following of links.The web crawlers will automatically look for
robots.txt
file in the root directory. So you can skip this tag ifrobots.txt
file is used.
-
OpenGraph protocol tags can be defined, to control how the pages when posted on Facebook, Twitter
<body>
Body has many attributes to specify event handlers like onafterprint
, onblur
and much more, but the browser support for them is not there. A better alternative is to use window.addEventListener('afterprint', () => {})
instead.
Scripting
<canvas>
Used for drawing graphics. It is better to set the width/height in HTML or JavaScript and not CSS. If there is no translucency use canvas.getContext('2d', { alpha: falkse })
to improve the rendering performance.
<noscript>
HTML to be inserted in the page if script type on the page is unsupported or if scripting is currently turned off in the browser.
<script>
async
- script is fetched in parallel and evaluated as soon as it is available. Scripts with async do not have a guranteed order of execution.
This can be usefule to prevent FOUC (Flash of Unstyuled Content), where the webpage would briefly appear in it raw, unstyled content before the styles are applied.
-
defer
- script is fetched in parallel and evaluated after the document is parsed and before firingDOMContentLoaded
. The scripts are executed in order. -
fetchpriority
- provide hint to use when fetchinghigh
,low
,auto
(default). -
blocking
- Block the HTML parsing. You do not need to set it, as it is the default when you use<script src="">
without any attributes. -
crossorigin
-
integrity
- provide the SHA hash to verify the integrity of the script. -
referrerpolicy
- indicates which referrer to send when fetching the script. -
src
- URL of the script -
type
- leave it empty - to treat it as classic script
-
importmap
- body of the element contains an import map, used to specify how the browser resolves module specifier when importing JS modules.Now you can import modules much easily in JS
Otherwise you would have to do this
-module
- Treat script as JS module. The script is deferred by default. -
any other value - can be used for server side rendering
Content sectioning
Organize the document content into logical pieces.
<header>
At the top level (when parent is 'body'), it defines the site banner
(logo, company name, search feature, global navigation), and if it is nested in <main>
, <article>
, it identifies as header for that section.
<footer>
When used for the whole site (i.e. parent is body
), it should contain info that should be displayed on every page like copyright statement, contact info, links to privacy and cookie policies. In other places, footer has no special meaning.
<main>
Single use per page to identify main content of the document, or the central functionality of an application.
This content should expand upon the central topic of a document, so do not include things that are repeated on multiple pages like sidebars, navigation links, copyright info, site logos, search forms.
<article>
Self-contained, section of content that is independently reusable (like forum post, blog, product card, user-submitted comment)
Put <h1> - <h6>
as the first child element to identify the article.
<aside>
Generally in a sidebar or call-out box, to show content that is indirectly related to the document's main content (like some info about CSS on a HTML page).
<section>
To put generic standalone sections of a document where there is not more specific semantic element to use. Each section should have a heading.
Exampes include, a list of search results, or a map display and its controls.
Use this workflow to think
- If content is standalone, that makes sense as a standalone piece like blog, use
<article>
. - If content provides info that is relevant to the main content but not directly part of it, like related links, author bio, use
<aside>
. - If content is the main content of the page use
<main>
. - If element needs to be used just for styling purposes, use
<div>
.
Cases when heading is not required in <section>
, are typically found in application/UI sections.
<address>
To provide contact info, like physical address, URL, email address, phone number, social media handle, etc. Include the name of person to which the contact refers to inside <address>
.
<search>
Part of document that contains form controls or other content related to performing a search or filtering operation. It is not for showing the search results.
<nav>
If it is part of site heading, then it is considered main navigation for the site. If it is nested in an <article>
, it is considered internal navigation for that section only.
<hgroup>
Represents a heading (<h1> - <h6>
) and related content (<p>
). You can group subheadings, alternative title, or tagline.
<h1> - <h6>
- Do not use these to make text large/small. HTML's purpose if to define the structure of the page, and CSS's purpose is to style.
- Avoid skipping heading levels. People using screen reading software, will quickly jump from heading to heading to determine the content of the page, so don't skip heading levels as it might confuse the people as to where the missing heading is.
<hx>
in<main>
is heading for the page. When nesting<h1>
inside<article>, <aside>, <nav>
its font-size woudl be decremented based on the nesting level.
Text Content
<blockquote>
Add quotation marks using CSS
<dl>
Description list to provide definition of preceding terms. Used to implement gloassay or to display metadata (list of key-value pairs).
<div>
Used to group content and apply styling.
<figure>
Usually it is an image, illustration, diagram, code snippet that is referenced in the main flow of the document, but that can be moved to another part of the document or to an appexdix without affecting the main flow.
<hr>
To create a thematic break between paragraph level elements (for example a change of scene in a story, a shift of topic within section).
Do not use this to draw a horizontal line, as <hr>
has a semantic meaning.
<menu>
Semantic alternative to <ul>
, but browsers treat it the same as <ul>
. The key difference being, it was mainly intended for interactive items.
<ol>
Numberd list.
reversed
- to start numbering in reversestart
- integer to provide start count (always an integer)type
a
A
i
- lowercase roman numeralsI
1
- for numbers (default)
<p>
Can be structured grouping of related content, such as images, or form fields. Screen-readers provide shortcuts to let the users skip to the next or previous paragraph, letting them skim content like how white space lets visual users skip around.
<pre>
Preformatted text which is to be presented exactly as written in HTML file.
If you are using this to draw figures, provide an alternative on what the image is for accessibility.
<ul>
Use css line-height: 80%
to make the list compact and list-style-type
to set the type of the marker.
Created: November 21, 2023