Skip to content

HTML 头部

什么是 HTML 头部

HTML 头部是 <head> 元素的内容,包含了页面的元数据(meta data)。这些信息不会直接显示在页面上,但对浏览器、搜索引擎和其他程序很重要。

head 元素的结构

html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <!-- 字符编码 -->
    <meta charset="UTF-8">
    
    <!-- 视口设置 -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <!-- 页面标题 -->
    <title>页面标题</title>
    
    <!-- 页面描述 -->
    <meta name="description" content="页面描述内容">
    
    <!-- 关键词 -->
    <meta name="keywords" content="关键词1, 关键词2, 关键词3">
    
    <!-- 作者信息 -->
    <meta name="author" content="作者姓名">
    
    <!-- 样式表链接 -->
    <link rel="stylesheet" href="styles.css">
    
    <!-- 页面图标 -->
    <link rel="icon" href="favicon.ico">
</head>
<body>
    <!-- 页面内容 -->
</body>
</html>

title 元素

<title> 元素定义页面的标题,显示在浏览器标签页上。

基本用法

html
<title>我的个人博客</title>
<title>产品介绍 - 公司名称</title>
<title>联系我们 | 服务热线 400-123-4567</title>

title 的重要性

  1. 浏览器标签显示
  2. 搜索引擎结果
  3. 书签标题
  4. 社交媒体分享

title 最佳实践

html
<!-- 好的标题:简洁、描述性、包含关键词 -->
<title>HTML教程 - 从入门到精通 | 前端开发指南</title>

<!-- 不好的标题:太长或无意义 -->
<title>这是一个非常非常长的标题,包含了太多不必要的信息,用户和搜索引擎都不喜欢</title>
<title>页面</title>

meta 元素

<meta> 元素提供页面的元数据信息。

1. 字符编码

html
<!-- UTF-8 编码(推荐) -->
<meta charset="UTF-8">

<!-- 其他编码格式 -->
<meta charset="GBK">
<meta charset="ISO-8859-1">

2. 视口设置(响应式设计)

html
<!-- 基本响应式设置 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<!-- 详细设置 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

viewport 属性说明:

  • width=device-width: 宽度等于设备宽度
  • initial-scale=1.0: 初始缩放比例
  • maximum-scale=1.0: 最大缩放比例
  • minimum-scale=1.0: 最小缩放比例
  • user-scalable=no: 禁止用户缩放

3. SEO 相关元数据

html
<!-- 页面描述(搜索引擎摘要) -->
<meta name="description" content="本站提供专业的HTML教程,从基础到高级,适合初学者和有经验的开发者。">

<!-- 关键词(现在重要性降低) -->
<meta name="keywords" content="HTML, CSS, JavaScript, 前端开发, 网页制作">

<!-- 作者信息 -->
<meta name="author" content="张三">

<!-- 版权信息 -->
<meta name="copyright" content="© 2024 我的网站">

<!-- 页面语言 -->
<meta name="language" content="zh-CN">

<!-- 页面主题色 -->
<meta name="theme-color" content="#007acc">

4. 页面控制元数据

html
<!-- 页面刷新 -->
<meta http-equiv="refresh" content="30">

<!-- 页面重定向 -->
<meta http-equiv="refresh" content="5; url=https://www.example.com">

<!-- 缓存控制 -->
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">

<!-- 兼容性设置 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">

5. 社交媒体元数据 (Open Graph)

html
<!-- Facebook/Open Graph -->
<meta property="og:title" content="页面标题">
<meta property="og:description" content="页面描述">
<meta property="og:image" content="https://example.com/image.jpg">
<meta property="og:url" content="https://example.com/page">
<meta property="og:type" content="website">
<meta property="og:site_name" content="网站名称">

<!-- Twitter Card -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="页面标题">
<meta name="twitter:description" content="页面描述">
<meta name="twitter:image" content="https://example.com/image.jpg">
<meta name="twitter:site" content="@username">

<link> 元素用于链接外部资源。

1. CSS 样式表

html
<!-- 外部样式表 -->
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="https://cdn.example.com/bootstrap.css">

<!-- 媒体查询样式表 -->
<link rel="stylesheet" href="print.css" media="print">
<link rel="stylesheet" href="mobile.css" media="screen and (max-width: 600px)">

<!-- 预加载关键样式 -->
<link rel="preload" href="critical.css" as="style" onload="this.onload=null;this.rel='stylesheet'">

2. 网站图标

html
<!-- 标准网站图标 -->
<link rel="icon" href="favicon.ico">
<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16">

<!-- Apple 设备图标 -->
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon-180x180.png">

<!-- Android Chrome 图标 -->
<link rel="manifest" href="site.webmanifest">

3. 字体引入

html
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap" rel="stylesheet">

<!-- 自定义字体 -->
<link rel="preload" href="fonts/custom-font.woff2" as="font" type="font/woff2" crossorigin>

4. 资源预加载

html
<!-- DNS 预解析 -->
<link rel="dns-prefetch" href="//example.com">

<!-- 预连接 -->
<link rel="preconnect" href="https://fonts.googleapis.com">

<!-- 预加载资源 -->
<link rel="preload" href="hero-image.jpg" as="image">
<link rel="preload" href="script.js" as="script">

<!-- 预获取资源 -->
<link rel="prefetch" href="next-page.html">

5. 页面关系

html
<!-- 规范链接(避免重复内容) -->
<link rel="canonical" href="https://example.com/page">

<!-- 上一页/下一页 -->
<link rel="prev" href="page1.html">
<link rel="next" href="page3.html">

<!-- 多语言版本 -->
<link rel="alternate" hreflang="en" href="https://example.com/en/page">
<link rel="alternate" hreflang="zh-CN" href="https://example.com/zh/page">

<!-- RSS 订阅 -->
<link rel="alternate" type="application/rss+xml" href="feed.xml" title="RSS Feed">

script 元素在头部

虽然 <script> 通常放在页面底部,但有些情况需要在头部引入:

html
<head>
    <!-- 关键JavaScript(如polyfills) -->
    <script src="modernizr.js"></script>
    
    <!-- 异步加载脚本 -->
    <script async src="analytics.js"></script>
    
    <!-- 延迟加载脚本 -->
    <script defer src="main.js"></script>
    
    <!-- 内联关键脚本 -->
    <script>
        // 关键配置代码
        window.CONFIG = {
            apiUrl: 'https://api.example.com'
        };
    </script>
</head>

style 元素

内部样式表,用于页面特定的样式:

html
<head>
    <style>
        /* 关键CSS(首屏渲染) */
        .hero {
            height: 100vh;
            background: linear-gradient(45deg, #007acc, #00d4ff);
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        /* 字体加载优化 */
        @font-face {
            font-family: 'CustomFont';
            src: url('font.woff2') format('woff2');
            font-display: swap;
        }
    </style>
</head>

base 元素

设置页面中所有相对 URL 的基础 URL:

html
<head>
    <base href="https://www.example.com/">
    <base target="_blank">
</head>
<body>
    <!-- 这个链接实际指向 https://www.example.com/about.html -->
    <a href="about.html">关于我们</a>
    
    <!-- 这个图片实际指向 https://www.example.com/images/logo.png -->
    <img src="images/logo.png" alt="Logo">
</body>

完整的头部示例

1. 标准网站头部

html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <!-- 基本设置 -->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    
    <!-- SEO 信息 -->
    <title>专业前端开发教程 - 从零基础到高级 | DevTutorial</title>
    <meta name="description" content="提供HTML、CSS、JavaScript等前端技术的详细教程,适合初学者和进阶开发者学习。">
    <meta name="keywords" content="前端开发,HTML教程,CSS教程,JavaScript,网页制作">
    <meta name="author" content="DevTutorial Team">
    
    <!-- 社交媒体 -->
    <meta property="og:title" content="专业前端开发教程">
    <meta property="og:description" content="从零基础到高级的前端开发完整教程">
    <meta property="og:image" content="https://devtutorial.com/og-image.jpg">
    <meta property="og:url" content="https://devtutorial.com">
    <meta property="og:type" content="website">
    
    <!-- 网站图标 -->
    <link rel="icon" href="/favicon.ico">
    <link rel="apple-touch-icon" href="/apple-touch-icon.png">
    
    <!-- 样式表 -->
    <link rel="stylesheet" href="/css/styles.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap" rel="stylesheet">
    
    <!-- 性能优化 -->
    <link rel="dns-prefetch" href="//cdn.example.com">
    <link rel="preload" href="/images/hero-bg.jpg" as="image">
    
    <!-- 分析脚本 -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'GA_MEASUREMENT_ID');
    </script>
</head>

2. 移动应用头部

html
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    
    <title>移动应用名称</title>
    
    <!-- PWA 配置 -->
    <link rel="manifest" href="/manifest.json">
    <meta name="theme-color" content="#000000">
    
    <!-- iOS 特殊设置 -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="apple-mobile-web-app-title" content="应用名称">
    
    <!-- 启动屏幕 -->
    <link rel="apple-touch-startup-image" href="/launch-640x1136.png" media="(device-width: 320px) and (device-height: 568px)">
    
    <!-- 防止电话号码自动识别 -->
    <meta name="format-detection" content="telephone=no">
</head>

3. 电商网站头部

html
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <title>商品名称 - 在线购买 | 电商网站</title>
    <meta name="description" content="高质量商品,优惠价格,快速配送。立即购买享受更多优惠!">
    
    <!-- 结构化数据 -->
    <script type="application/ld+json">
    {
        "@context": "https://schema.org/",
        "@type": "Product",
        "name": "商品名称",
        "image": "https://example.com/product-image.jpg",
        "description": "商品详细描述",
        "brand": {
            "@type": "Brand",
            "name": "品牌名称"
        },
        "offers": {
            "@type": "Offer",
            "url": "https://example.com/product",
            "priceCurrency": "CNY",
            "price": "199.00",
            "availability": "https://schema.org/InStock"
        }
    }
    </script>
    
    <!-- 安全设置 -->
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline' https://trusted-cdn.com">
</head>

性能优化技巧

1. 关键资源预加载

html
<head>
    <!-- 预加载关键CSS -->
    <link rel="preload" href="critical.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
    <noscript><link rel="stylesheet" href="critical.css"></noscript>
    
    <!-- 预加载关键字体 -->
    <link rel="preload" href="font.woff2" as="font" type="font/woff2" crossorigin>
    
    <!-- 预加载首屏图片 -->
    <link rel="preload" href="hero-image.jpg" as="image">
</head>

2. 内联关键CSS

html
<head>
    <style>
        /* 首屏关键样式内联,避免渲染阻塞 */
        .header { height: 60px; background: #fff; }
        .hero { min-height: 100vh; background: #f0f0f0; }
    </style>
    
    <!-- 非关键CSS异步加载 -->
    <link rel="preload" href="styles.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
</head>

常见错误

1. 字符编码设置错误

html
<!-- 错误:字符编码设置太晚 -->
<head>
    <title>网站标题</title>
    <meta charset="UTF-8">  <!-- 应该在最前面 -->
</head>

<!-- 正确:字符编码在最前面 -->
<head>
    <meta charset="UTF-8">
    <title>网站标题</title>
</head>

2. 缺少视口设置

html
<!-- 错误:没有视口设置,在移动设备上显示不正常 -->
<head>
    <meta charset="UTF-8">
    <title>网站标题</title>
</head>

<!-- 正确:包含视口设置 -->
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网站标题</title>
</head>

3. SEO 信息不完整

html
<!-- 错误:缺少重要的SEO信息 -->
<head>
    <title>我的网站</title>
</head>

<!-- 正确:包含完整的SEO信息 -->
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>具体的页面标题 - 网站名称</title>
    <meta name="description" content="页面的详细描述,包含关键词">
    <meta name="keywords" content="相关关键词">
</head>

实践练习

创建一个完整的网站头部:

html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <!-- 基础设置 -->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    
    <!-- 页面信息 -->
    <title>HTML头部元素完整指南 - 前端开发教程</title>
    <meta name="description" content="详细介绍HTML头部元素的使用方法,包括meta标签、link标签等,帮助开发者构建专业的网页头部。">
    <meta name="keywords" content="HTML,head标签,meta标签,网页头部,前端开发">
    <meta name="author" content="前端开发团队">
    
    <!-- 社交媒体分享 -->
    <meta property="og:title" content="HTML头部元素完整指南">
    <meta property="og:description" content="学习如何正确使用HTML头部元素">
    <meta property="og:image" content="https://example.com/head-guide-cover.jpg">
    <meta property="og:url" content="https://example.com/html-head-guide">
    <meta property="og:type" content="article">
    
    <!-- 网站图标 -->
    <link rel="icon" href="/favicon.ico" sizes="any">
    <link rel="icon" href="/favicon.svg" type="image/svg+xml">
    <link rel="apple-touch-icon" href="/apple-touch-icon.png">
    <link rel="manifest" href="/manifest.json">
    
    <!-- 样式和字体 -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&display=swap" rel="stylesheet">
    
    <!-- 内联关键CSS -->
    <style>
        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            line-height: 1.6;
            color: #333;
            margin: 0;
            padding: 20px;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
        }
        
        h1 {
            color: #007acc;
            border-bottom: 2px solid #007acc;
            padding-bottom: 10px;
        }
    </style>
    
    <!-- 外部样式表 -->
    <link rel="stylesheet" href="styles.css">
    
    <!-- 性能优化 -->
    <link rel="dns-prefetch" href="//cdn.example.com">
    <link rel="preload" href="hero-image.jpg" as="image">
    
    <!-- 结构化数据 -->
    <script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "Article",
        "headline": "HTML头部元素完整指南",
        "author": {
            "@type": "Organization",
            "name": "前端开发团队"
        },
        "datePublished": "2024-02-03",
        "description": "详细介绍HTML头部元素的使用方法"
    }
    </script>
</head>
<body>
    <div class="container">
        <header>
            <h1>HTML 头部元素实践</h1>
            <p>这个页面展示了HTML头部的正确设置方法。</p>
        </header>
        
        <main>
            <section>
                <h2>本页面头部包含的元素:</h2>
                <ul>
                    <li>✓ 字符编码设置</li>
                    <li>✓ 视口配置(响应式)</li>
                    <li>✓ SEO优化信息</li>
                    <li>✓ 社交媒体分享标签</li>
                    <li>✓ 网站图标配置</li>
                    <li>✓ 字体和样式加载</li>
                    <li>✓ 性能优化设置</li>
                    <li>✓ 结构化数据</li>
                </ul>
            </section>
            
            <section>
                <h2>检查方法</h2>
                <p>你可以通过以下方式检查头部设置:</p>
                <ol>
                    <li>使用浏览器开发者工具查看源代码</li>
                    <li>使用SEO检测工具测试优化效果</li>
                    <li>在社交媒体平台测试分享效果</li>
                    <li>使用性能测试工具检查加载速度</li>
                </ol>
            </section>
        </main>
    </div>
</body>
</html>

小结

  • <head> 元素包含页面的元数据,不直接显示
  • <title> 定义页面标题,对SEO很重要
  • <meta> 标签提供各种元数据信息
  • <link> 用于引入外部资源和定义页面关系
  • 正确设置字符编码和视口很关键
  • 社交媒体标签提升分享效果
  • 性能优化从头部开始
  • 结构化数据帮助搜索引擎理解内容

下一章我们将学习 HTML 与 CSS 的结合使用。