How to Write a Good index.html File

Every web developer has been there: you're starting a new project and staring at an empty file called index.html. You try to remember, which tags were meant to go in the again? Which are the meta tags that are best practice and which ones are deprecated? Recently, I found myself in this exact situation. My first instinct was to copy the head section from a previous project, but as I reviewed the code, I realized some tags were outdated or simply didn't apply to my new project. What followed was a deep dive into HTML head tags – which ones are essential, which are optional, and which are just cluttering my code. If you're in a hurry and just want the template: You can find my complete starter template on GitHub. The repository contains two main files: index.html: A clean, minimalist template with just what you need and no unnecessary extras. index-commented.html: The same template but with detailed comments explaining how and why you should use each tag. This article is essentially a deep dive into the comments from the index-commented.html file, providing more context and explanations for each decision made in the template. This template represents my opinionated approach after researching current best practices. It aims to be a solid foundation for most web projects while maintaining good performance, accessibility, and search engine optimization. Let's dive into the essential components of a well-structured HTML head. The tags you must include These tags should be present in virtually every HTML document you create. They're essential for proper rendering, SEO, and accessibility. and lang="en": Setting the document type and language Always begin your HTML document with the doctype declaration. This tells browsers which version of HTML you're using (in this case, HTML5) and helps ensure consistent rendering. The lang attribute on the tag specifies the language of your page - this is crucial for screen readers, search engines, and browsers. If your content is in a different language, change the code accordingly (e.g., lang="es" for Spanish). : The page title Hello world! Every HTML document must have a title tag. This text appears in browser tabs, bookmarks, and search engine results. Make your titles descriptive yet concise, ideally under 60 characters. A good title both informs users about your page content and includes relevant keywords for SEO. : Configuring viewport for responsive design In today's mobile-first world, this meta tag is non-negotiable. It tells mobile browsers how to control the page's dimensions and scaling. Let's break down what each parameter does: viewport-fit=cover: Ensures content extends to the edge of the display (especially important for notched phones) width=device-width: Sets the width of the page to follow the screen width of the device initial-scale=1.0: Sets the initial zoom level when the page is first loaded minimum-scale=1.0: Prevents users from zooming out too much maximum-scale=5.0: Allows users to zoom in up to 5x (limiting this completely would harm accessibility) Without this tag, mobile devices will render pages at a typical desktop screen width and then scale them down, resulting in tiny, unreadable text and forcing users to zoom and pan. , , : Essential meta information These meta tags provide important information about your page: description: A concise summary of your page content (ideally 150-160 characters). This often appears in search engine results below your title. keywords: Relevant keywords for your page content. While less important for Google these days, other search engines and crawlers may still use this information. author: The name of the individual or organization that created the page. While these tags don't directly affect page rendering, they're valuable for SEO and content categorization. : Avoiding duplicate indexation This tag helps search engines avoid indexing the same content multiple times when it's accessible via different URLs. For example, if your page is accessible via multiple URLs (like example.com/page and example.com/page/index.html), the canonical tag tells search engines which URL is the "official" version to index, preventing duplicate indexation which can harm your search rankings. Fill in the href attribute with the primary URL you want search engines to associate with this content. CSS loading strategies: Critical inline CSS vs. external stylesheets body { background: #fefefe; color: #222; font-family: 'Roboto', sans-serif; padding: 1rem; line-height: 1.8; } This strategy for CSS loading optimizes performance in two ways: Inline critical CSS: By embedding essential styles directly in the HTML document, you avoid making an additional network request that would block rendering. This makes critical styles load blazingly fast

Mar 22, 2025 - 23:39
 0
How to Write a Good index.html File

Every web developer has been there: you're starting a new project and staring at an empty file called index.html. You try to remember, which tags were meant to go in the again? Which are the meta tags that are best practice and which ones are deprecated?

Recently, I found myself in this exact situation. My first instinct was to copy the head section from a previous project, but as I reviewed the code, I realized some tags were outdated or simply didn't apply to my new project. What followed was a deep dive into HTML head tags – which ones are essential, which are optional, and which are just cluttering my code.

If you're in a hurry and just want the template: You can find my complete starter template on GitHub. The repository contains two main files:

  • index.html: A clean, minimalist template with just what you need and no unnecessary extras.
  • index-commented.html: The same template but with detailed comments explaining how and why you should use each tag.

This article is essentially a deep dive into the comments from the index-commented.html file, providing more context and explanations for each decision made in the template.

This template represents my opinionated approach after researching current best practices. It aims to be a solid foundation for most web projects while maintaining good performance, accessibility, and search engine optimization.

Let's dive into the essential components of a well-structured HTML head.

The tags you must include

These tags should be present in virtually every HTML document you create. They're essential for proper rendering, SEO, and accessibility.

and lang="en": Setting the document type and language


 lang="en">

Always begin your HTML document with the doctype declaration. This tells browsers which version of HTML you're using (in this case, HTML5) and helps ensure consistent rendering. The lang attribute on the tag specifies the language of your page - this is crucial for screen readers, search engines, and browsers. If your content is in a different language, change the code accordingly (e.g., lang="es" for Spanish).

</code>: The page title </h4> <div class="highlight js-code-highlight"> <pre class="highlight html"><code><span class="nt"><title></span>Hello world!<span class="nt">

Every HTML document must have a title tag. This text appears in browser tabs, bookmarks, and search engine results. Make your titles descriptive yet concise, ideally under 60 characters. A good title both informs users about your page content and includes relevant keywords for SEO.

: Configuring viewport for responsive design

 name="viewport" content="viewport-fit=cover, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0">

In today's mobile-first world, this meta tag is non-negotiable. It tells mobile browsers how to control the page's dimensions and scaling. Let's break down what each parameter does:

  • viewport-fit=cover: Ensures content extends to the edge of the display (especially important for notched phones)
  • width=device-width: Sets the width of the page to follow the screen width of the device
  • initial-scale=1.0: Sets the initial zoom level when the page is first loaded
  • minimum-scale=1.0: Prevents users from zooming out too much
  • maximum-scale=5.0: Allows users to zoom in up to 5x (limiting this completely would harm accessibility)

Without this tag, mobile devices will render pages at a typical desktop screen width and then scale them down, resulting in tiny, unreadable text and forcing users to zoom and pan.

, , : Essential meta information

 name="description" content="">
 name="keywords" content="">
 name="author" content="">

These meta tags provide important information about your page:

  • description: A concise summary of your page content (ideally 150-160 characters). This often appears in search engine results below your title.
  • keywords: Relevant keywords for your page content. While less important for Google these days, other search engines and crawlers may still use this information.
  • author: The name of the individual or organization that created the page.

While these tags don't directly affect page rendering, they're valuable for SEO and content categorization.

: Avoiding duplicate indexation

 rel="canonical" href="" />

This tag helps search engines avoid indexing the same content multiple times when it's accessible via different URLs. For example, if your page is accessible via multiple URLs (like example.com/page and example.com/page/index.html), the canonical tag tells search engines which URL is the "official" version to index, preventing duplicate indexation which can harm your search rankings.

Fill in the href attribute with the primary URL you want search engines to associate with this content.

CSS loading strategies: Critical inline CSS vs. external stylesheets


 rel="preload" href="main.css" as="style" onload="this.onload=null;this.rel='stylesheet'">

This strategy for CSS loading optimizes performance in two ways:

  1. Inline critical CSS: By embedding essential styles directly in the HTML document, you avoid making an additional network request that would block rendering. This makes critical styles load blazingly fast and prevents the Flash of Unstyled Content (FOUC) that can occur when styles load after content.
  2. Asynchronous loading for non-critical CSS: The preload technique with the onload handler allows the main stylesheet to load without blocking rendering. This means your page can start displaying while the rest of the styles are still loading, creating a better user experience. The noscript tag provides a fallback for users with JavaScript disabled.

Alternatively, if your site doesn't have significant styling needs above the fold, you can use a simpler approach:

 href="main.css" rel="stylesheet" />

This is more straightforward but can slow initial rendering as the browser must download and parse the CSS before displaying content.