HTML Head
What is the Head Section?
The HTML <head> element is a container for metadata (data about data) and is placed between the <html> tag and the <body> tag. The head section contains information about the document that is not displayed on the page but is essential for browsers and search engines.
Basic Head Structure
Here's the basic structure of the head section:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page Title</title>
</head>
<body>
<h1>Page Content</h1>
</body>
</html>
Elements in the Head Section
Common elements that can appear in the <head> section:
1. Title Element
The <title> element is required and defines the page title shown in the browser tab:
<head>
<title>My Web Page</title>
</head>
2. Meta Charset
The charset meta tag specifies the character encoding. UTF-8 is the recommended encoding:
<head>
<meta charset="UTF-8">
</head>
3. Viewport Meta Tag
The viewport meta tag controls how the page is displayed on mobile devices:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
4. Meta Description
The description meta tag provides a summary of the page content for search engines:
<head>
<meta name="description" content="Learn HTML with our comprehensive tutorial guide.">
</head>
5. Meta Keywords
The keywords meta tag (deprecated) was used for SEO, but is no longer used by major search engines:
<!-- Deprecated - Don't use -->
<meta name="keywords" content="HTML, CSS, JavaScript">
6. Meta Author
The author meta tag specifies the author of the document:
<head>
<meta name="author" content="John Doe">
</head>
7. Link Element
The <link> element links to external resources like CSS files, favicons, etc.:
<head>
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.ico">
</head>
8. Style Element
The <style> element contains internal CSS:
<head>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
</style>
</head>
9. Script Element
The <script> element contains or references JavaScript:
<head>
<script src="script.js"></script>
<script>
console.log('Script in head');
</script>
</head>
10. Base Element
The <base> element specifies the base URL for all relative URLs in the document:
<head>
<base href="https://example.com/">
</head>
<body>
<img src="images/logo.png"> <!-- Resolves to https://example.com/images/logo.png -->
</body>
Complete Head Example
Here's a complete example of a well-structured head section:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Character encoding -->
<meta charset="UTF-8">
<!-- Viewport for responsive design -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Page title -->
<title>HTML Tutorial - Learn Web Development</title>
<!-- Meta description for SEO -->
<meta name="description" content="Learn HTML with our comprehensive tutorial guide covering all HTML fundamentals.">
<!-- Meta author -->
<meta name="author" content="The World of Computing According to Dave">
<!-- External CSS -->
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/tutorial.css">
<!-- Favicon -->
<link rel="icon" href="favicon.ico" type="image/x-icon">
<!-- Internal CSS (optional) -->
<style>
/* Internal styles here */
</style>
</head>
<body>
<h1>Page Content</h1>
</body>
</html>
Head Element Order
While order isn't strictly required, here's a recommended order for head elements:
- Charset meta tag
- Viewport meta tag
- Title element
- Meta description
- Other meta tags
- Link elements (CSS, favicon, etc.)
- Style element (if needed)
- Script elements (if needed in head)
Best Practices
- Always include charset: UTF-8 is the recommended encoding
- Include viewport meta tag: Essential for responsive design
- Use unique titles: Each page should have a unique, descriptive title
- Provide meta description: Helpful for SEO and search result snippets
- Link external CSS before body: Place CSS links in the head
- Consider script placement: Scripts are often better at the end of body
- Organize head elements: Keep head section organized and readable