3.2    <title>: Heading of the HTML Page:HTML页面的标题

In every HTML document, you should use a title that gets displayed in the header of the web browser. 在每个HTML文档中,您应该使用一个显示在web浏览器标题中的标题。You can write such a title between the <title> and </title> tags within the head element. 您可以在head元素中的<title></title>标签之间编写这样的标题。If no title is used, what appears here depends on the web browser. Often, you’ll then find a title such as Untitled Document or Untitled, for example. 如果不使用标题,则此处显示的内容取决于网络浏览器。通常,您会发现一个标题,例如“无标题文档”或“无标题”。You can use only one title element in total for an HTML document.HTML文档总共只能使用一个title元素。

In Figure 3.2, you can see the title element used in the following example being rendered:在图3.2中,您可以看到以下示例中使用的title元素正在呈现:

<!doctype html>
<html lang="en">
<head>
<title>The Heading of the HTML Page
</title>
<meta charset="UTF-8">
</head>
...

Listing 3.1     /examples/chapter003/3_2/index.html

The Title Is Usually Displayed in the Header Bar and/or Tab of the Web Browser

Figure 3.2     The Title Is Usually Displayed in the Header Bar and/or Tab of the Web Browser标题通常显示在Web浏览器的标题栏和/或选项卡中

Invalid HTMLHTML无效

If you omit the title element, an error message displays when you check the validity of the HTML code (e.g., at http://validator.w3.org). 如果省略title元素,则在检查HTML代码的有效性时会显示错误消息(例如,在http://validator.w3.org)。This means a title element must exist in every HTML document for it to be valid HTML. 这意味着每个HTML文档中都必须存在title元素,它才是有效的HTML。In addition, no other HTML elements are allowed in between <title> and </title>.此外,在<title></title>之间不允许有其他HTML元素。

Besides the display in the header bar or tabs of the web browser, there are two much more important reasons to use the title element: first, this title is used as a name suggestion by the web browser when setting bookmarks (favorites), and second—and even more importantly—the title has a high significance with the search engines on the web. 除了在web浏览器的标题栏或选项卡中显示外,使用title元素还有两个更重要的原因:第一,此标题在设置书签(集合夹)时被web浏览器用作名称建议;第二,更重要的是,标题在web上的搜索引擎中具有很高的意义。As you can see in Figure 3.3, the title is often the first thing your visitors see when your website is listed in a search engine. 如图3.3所示,当您的网站在搜索引擎中列出时,标题通常是访问者看到的第一件事。You can create such a search result yourself for testing purposes with a SERP snippet generator (e.g., www.serpsimulator.com). 您可以使用SERP代码片段生成器(例如www.serpsimulator.com)自己创建这样的搜索结果以进行测试。It often occurs that the title is also a clickable link to your website. In addition, the title is important for the hit list and for the rank of the page in the hit index of the search engine.通常情况下,标题也是您网站的可点击链接。此外,标题对于搜索引擎的点击列表和页面在点击索引中的排名也很重要。

For Search Engines, the Importance of the <title> Element Shouldn’t Be Ignored

Figure 3.3     For Search Engines, the Importance of the <title> Element Shouldn’t Be Ignored对于搜索引擎来说,<title>元素的重要性不容忽视

Titles for Search Engines搜索引擎标题

The topic of search engine optimization (SEO) can’t be described in a few paragraphs and encompasses many subareas. 搜索引擎优化(SEO)的主题不能用几段话来描述,它包含了许多子领域。It isn’t possible either to make a general statement about when exactly something is “optimal.” 也不可能对什么时候是“最佳”做出一般性的陈述Even the SEO experts often disagree on this. Nevertheless, it’s safe to say that the title is very important in search engines. 即使是SEO专家也经常对此持不同意见。然而,可以肯定地说,标题在搜索引擎中非常重要。It’s often recommended to use one or two keywords, followed by a slogan and perhaps the web address (e.g., Keyword 1, Keyword 2—A short heading—www.domain.com). 通常建议使用一两个关键字,后面跟着一个口号,也许还有网址(例如,关键字1,关键字2——一个简短的标题——www.domain.com)。In practice, for example, the following title would make sense: Smartphones, Cell Phones—buy cheap smartphones—www.domain.com. 例如,在实践中,以下标题是有意义的:智能手机,手机——买便宜的智能手机——www.domain.com。In addition, the title shouldn’t be too long, otherwise it will be truncated when listed in the search engine. The common recommendation ranges between 60 and 70 characters.此外,标题不应该太长,否则在搜索引擎中列出时会被截断。常见的推荐范围在60到70个字符之间。