The HTML Head Element
An essential part of an HTML document is the<head> element, which contains metadata about the document. Some vital information, such as the document’s title and character encoding are stored in the <head> element. It’s also where you can include links to external resources such as CSS stylesheets and JavaScript files.
Recommended Minimum Head
More often than not, this sort of metadata can grow in complexity with time. However, there are a few important things that should never be omitted. Here’s the bare minimum you should include in your<head> element:
Essential Meta Tags Explained
-
charset: The
charsetmeta tag tells the browser what character encoding to use when rendering the document. UTF-8 is the standard encoding that supports all characters and symbols. -
viewport: The
viewportmeta tag tells the browser how to render the page on mobile devices. This ensures your site is responsive and displays correctly on all screen sizes. -
title: The
titleelement is used by search engines to display the page’s title in search results. It should be descriptive, unique, and under 60 characters. -
description: The
descriptionmeta tag is used by search engines to display a short description of the page in search results. Keep it under 155 characters for optimal display.
Favicons and Icons
Over the years, there have been many different and often conflicting guidelines about favicons. Nowadays, you can get away with a very minimal set of meta tags and tailor them to your needs as you go:rel="icon" meta tags with different sizes attributes.
Icon Types
- Standard icon: Used by most modern browsers
- Apple touch icon: Used when users add your site to their iOS home screen
- Mask icon: Used by Safari for pinned tabs (SVG format with specified color)
SEO and Metadata Links
Some<link> elements are important for SEO and metadata purposes. Here’s a list of really important ones to include:
Link Elements Explained
-
canonical: The
canonicallink element tells search engines which URL is the canonical version of the page. This helps prevent duplicate content issues and ensures that the correct page is indexed. -
sitemap: The
sitemaplink element tells search engines where to find the sitemap for the website. Sitemaps are XML files that contain a list of all the pages on the website and their metadata. They are used by search engines to index the website. -
alternate (RSS): The
alternatelink element tells search engines where to find the RSS feed for the website. RSS feeds contain a list of the most recent posts and are used by search engines and RSS readers. -
search: The
searchlink element is used by browsers to display a search box in the browser’s address bar, allowing users to search the website directly.
Social Media Tags
For better sharing on social media platforms, include Open Graph and Twitter Card tags:Resource Hints
Improve performance by preloading or prefetching resources:Resource Hint Types
- preload: Fetch critical resources early in the page load
- prefetch: Fetch resources that might be needed for future navigation
- dns-prefetch: Resolve DNS for external domains ahead of time
- preconnect: Establish early connections to important third-party origins
Complete Head Example
Here’s a comprehensive example combining all the essential elements:Best Practices
- Always include the essential meta tags: charset, viewport, title, and description
- Keep titles under 60 characters for optimal display in search results
- Keep descriptions under 155 characters to avoid truncation
- Use absolute URLs for canonical links and social media tags
- Optimize favicon size - a single 192x192 PNG covers most use cases
- Preload critical resources to improve initial page load performance
- Include social media tags for better sharing experience
- Use semantic, descriptive titles that accurately represent the page content
Next Steps
Now that you understand the HTML head basics, explore:- How to optimize images with lazy loading and proper alt text
- Security best practices for external links
- Advanced performance optimization techniques
- Accessibility guidelines for better user experience