作者:阿宏 2005-4-15 20:45:18
原文:What is the Best Way to Mark up the Title of a Document?
说明:本文是 《Web Standards Solutions: The Markup and Style Handbook》一书中的一章。书号:ISBN:1590593812。我们会陆续翻译此书中有价值的章节。
原作者:Dan Cederholm
翻译:阿宏
一个文档标题,最好的定义方法是什么?要回答这个问题,先设想我们要在一个页面上定义文章的标题,通常我们有三个方法来实现这个简单目的:
方法一: 有意义吗?
文章标题
虽然在某些情况下会是一个方便的标签,但它并不能表达出标题的完整含义。采用这个方法的一个好处是,我们可以对它附加一个CSS规则,分配其一个heading class,使其文字象标题一样显示。
.heading {
font-size: 24px;
font-weight: bold;
color: blue;
}
ok,现在所有的标题都用heading class标记成了大号的粗体字体,并且为蓝色。太棒了!但是这样做对吗?如果有人用一个不支持CSS的浏览器来观看,会怎样呢?
举个例子,如果我们设置的这个外部样式表的规则不被老版本的浏览器支持,会怎样?又或者有视觉障碍的人用屏幕阅读器来阅读这个页面,又会怎样?一个访问者通过这些途径所看到(或听到)的应该和这个页面上正常的文本没有任何区别。
尽管class="heading"为这个标签增加了一点意义,但仍然只是一个普通的标签,可以被大多数浏览器的缺省样式所修改掉。
搜索引擎检索这个页面时会略过标签,就好象它不存在一样,不会对其可能包含的关键字给于一点额外的重视。在后面我们会更多的谈到搜索引擎和标题的关系。
最后,由于标签是一个内嵌元素,我们可能需要把它嵌套在一个额外的块级元素中,比如 标签或 方法二: 和组合 文章标题 不象方法A,标签能在可视化的浏览器中把文字显示成粗体——甚至在不支持CSS的浏览器中。但是和标签一样,搜索引擎也不会因为有一些东西在段落中被加粗了而给予更高的优先。 难以设计样式 用普通的 和的组合,也带来了另一个缺憾——无法把这个标题设计成不同于其他段落的样式。我们可能想用一个特别的样式来突出标题,来使页面内容更清晰更具结构,但是用这个方法只能使其显示成粗体。 方法三:样式加实质 这也是聪明的定义方法,你会发现它很简单。不再需要额外的标签,你可以说,这仅仅比另外两个方法节省了一点点的字节,可以忽略不计,但节省一点是一点。 轻松定制样式 因为我们使用 标签更适合使用在整个页面,所以我们可以用各种各样的CSS方法来样式化。 更重要的是,尽管完全不用样式,一个标题标签也能明显的表示出一个标题!可视化的浏览器把 Screen readers, PDAs, cell phones, and visual and non-visual browsers will all understand what to do when they encounter a title tag, handle it correctly, and treat it more seriously than ordinary text on the page. With the tag, browsers that don't support CSS won't treat it specially. Annoying default styles In the past, designers might have avoided using title tags entirely due to the ugly browser defaults. Alternatively, avoid using However, it is important to emphasize that we can easily change these title tags using CSS - for example, an < ;h1> does not have to be a huge standard that takes up more than half of the screen. Later on, I’ll demonstrate how simple it is to style title tags with CSS, hopefully helping to alleviate some of your overwhelming fears. Search engine friendly This is a huge benefit. Search engines love title tags. On the other hand, a tag or a plain bold paragraph tag means something less. Marking your titles appropriately from Search engine bots will give special attention to title tags – this is where you might put some keywords. Just like So with just a little bit of effort, you can increase the likelihood that people will find your site based on the content of your page. Sounds good, doesn't it? About the order of headings As mentioned earlier, designers may use
使用一个段落标签,将会给我们带来块级的显示,会把文本变成粗体。但是用这个方法标记一个重要的标题时,我们面对的是同样无意义的结果。
文章标题
恩,多么好的标题定义。大多数的网页设计者对它都很熟悉。其实适当的使用它们,一直到
,代表了标题的六个级别,从最重要的(
)到最次要的(
)。他们本身就是块级的,不需要增加其它元素来使其单独成行。简单,有效——就是好工具。
标签是唯一的,而或
显示成更大的粗体。一个非样式化的页面将以被期望的那样显示文档结构,用适当的标题标签来传达意思。
or
due to the huge size of the default values, and instead use a higher numerical title tag to achieve a smaller size.
to
requires only a little effort on your part, but makes your pages easier for search engines to crawl, so people can ultimately find them.
In the example, this particular heading is the most important on the page because it is the title of the document. Therefore, we use the most important title tag, . Following the W3C specification, some people think that skipping several heading levels is a bad practice. For example, imagine we are on the following page:
Article Title
Our next title (if it is not repeated with another ) should be
, then
, etc. You probably shouldn't skip one level after
and go straight to
. I tend to agree with the above point of view, and maintain the continuity of levels along the text to construct a layout structure. This way, it's easier to add titles and styles to an already existing page, and you'll reduce errors caused by using extra numbers.
to label the most important title on a page, simply because its default font size is not as obnoxiously large as
. But remember, structure first, design later. We can always use CSS to style headings to any text size we like.