HTML 文档之 Head 最佳实践

本文介绍了HTML元信息的最佳实践,包括语言声明、字符编码、视口设置、DNS预读取等,有助于提升网页性能和用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

语言

html 标签中通过 lang 属性进行明确的语言声明,将会有助于翻译,英文、简体中文和繁体中文网页所属性值如下:

<html lang="en"> <html lang="zh-Hans"> <html lang="zh-Hant">

编码

请在 <head> 中第一行统一使用 utf-8 编码声明。

<meta charset="utf-8">

Base 元素

尽量不使用 <base> 元素,绝对地址和 URL 对于开发者和用户来说都更好。

<!-- Bad -->
<base href="/blog/"> <link href="hello-world" rel="canonical"> <!-- Good --> <link href="/blog/hello-world" rel="canonical">

Viewport

设置 viewport 的宽度为设备宽度,默认缩放比为 1(允许用户缩放),设置 viewport-fit=cover 以兼容 iPhone X 全面屏“刘海”的显示。

<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">

DNS 预读取

DNS 预读取是一项使浏览器主动去执行域名解析的功能,其范围包括当前文档内的所有链接,这能够减少用户点击链接时的延迟。

<meta http-equiv="x-dns-prefetch-control" content="on"> <link rel="dns-prefetch" href="//www.google-analytics.com"> <link rel="dns-prefetch" href="//fonts.googleapis.com">

预加载

指示浏览器预先请求当前页面所需要的关键性资源。

<link rel="preload" href="/css/main.css" as="style"> <link rel="preload" href="/js/main.js" as="script">

预请求

指示浏览器预先请求用户即将浏览页面所需要的关键性资源。

<link rel="prefetch" href="/img/css-sprite.png"> <link rel="prefetch" href="/fonts/icons.woff2">

渲染偏好

对于国内各种双核浏览器,通过设置 renderer 头要求它们使用 webkit 内核;对于 IE 浏览器,通过设置 IE=edge 要求它使用最新的版本;对于百度搜索强制转码的流氓做法,通过 no-transform 禁止其自动转码;指示浏览器对类似电话、邮箱或地址的内容不要自作聪明的瞎识别(移动端)。

<meta name="renderer" content="webkit"> <!-- 用在360中 --> <meta name="force-rendering" content="webkit"> <!-- 用在其它 --> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- 针对 IE 浏览器 --> <meta http-equiv="Cache-Control" content="no-transform"> <!-- 针对百度搜索 --> <meta name="format-detection" content="telephone=no,email=no,adress=no">

文档信息

HTML 文档的一些元数据,包括:作者、描述、关键词和生成工具;设置 robotsindex,follow 指示搜索引擎爬虫该页面需要进入索引,并且页面内的所有链接都要继续跟踪;设置 referrerorigin-when-cross-origin 指示浏览器在进行跨域跳转时,其 referrer 值将统一为域名,而非具体的 URL 地址。

<meta name="author" content="米老朱"> <meta name="description" content="米老朱的个人博客"> <meta name="keywords" content="米老朱,极客,博客,WEB,开发,产品设计"> <meta name="generator" content="Hugo 0.32"> <meta name="robots" content="index,follow"> <meta name="referrer" content="origin-when-cross-origin">

Icons

虽说所有浏览器都还支持过时的 favicon.ico 格式,但在 HTML5 时代,我们应该使用更好的 PNG icon with sizes 方案。同时为了兼容老旧浏览器,我们可以将生成好的 favicon.ico 文件放在网站的根目录下面,通常浏览器会自动请求并加载它,并不需要额外通过 link 标签引入。

<meta name="theme-color" content="#db5945"> <!-- 主题颜色 --> <meta name="application-name" content="米老朱的博客"> <!-- 应用名称 --> <meta name="apple-mobile-web-app-capable" content="yes"> <!-- 隐藏状态栏 --> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <!-- 状态栏颜色 --> <meta name="apple-mobile-web-app-title" content="米老朱的博客"> <!-- iOS 下的应用名称 --> <meta name="msapplication-tooltip" content="米老朱的博客"> <!-- Hover 的提示信息 --> <meta name="msapplication-TileColor" content="#db5945"> <!-- 磁贴背景颜色 --> <meta name="msapplication-TileImage" content="/img/logo-144.png"> <!-- 磁贴图标 --> <meta name="msapplication-config" content="/browserconfig.xml"> <!-- 磁贴配置文件 --> <link rel="icon" type="image/png" href="/img/logo-16.png" sizes="16x16"> <!-- Browser Favicon --> <link rel="icon" type="image/png" href="/img/logo-32.png" sizes="32x32"> <!-- Taskbar Shortcut --> <link rel="icon" type="image/png" href="/img/logo-96.png" sizes="96x96"> <!-- Desktop Shortcut --> <link rel="icon" type="image/png" href="/img/logo-128.png" sizes="128x128"> <!-- Chrome Web Store --> <link rel="icon" type="image/png" href="/img/logo-196.png" sizes="196x196"> <!-- Chrome for Android Home Screen --> <link rel="icon" type="image/png" href="/img/logo-228.png" sizes=

转载于:https://www.cnblogs.com/liuhao-web/p/8706554.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值