Home  >  Article  >  Web Front-end  >  The best way to define a title_CSS/HTML

The best way to define a title_CSS/HTML

WBOY
WBOYOriginal
2016-05-16 12:10:542061browse

作者:阿宏 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"为这个标签增加了一点意义,但仍然只是一个普通的标签,可以被大多数浏览器的缺省样式所修改掉。

搜索引擎检索这个页面时会略过标签,就好象它不存在一样,不会对其可能包含的关键字给于一点额外的重视。在后面我们会更多的谈到搜索引擎和标题的关系。

最后,由于标签是一个内嵌元素,我们可能需要把它嵌套在一个额外的块级元素中,比如

标签或

标签,为的是使它能够形成单独的行,这会进一步被非必要的代码弄乱你的标签。而这些额外增加的标签却是必须的,这样才能使不支持CSS的浏览器显示出没有差别的文本。

方法二:

组合

文章标题


使用一个段落标签,将会给我们带来块级的显示,会把文本变成粗体。但是用这个方法标记一个重要的标题时,我们面对的是同样无意义的结果。

不象方法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

or

due to the huge size of the default values, and instead use a higher numerical title tag to achieve a smaller size.

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

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.

Search engine bots will give special attention to title tags – this is where you might put some keywords. Just like and <meta> are retrieved, they will search down the page along the title tag. If you don't use these tags, the keywords contained within them won't be considered valuable and will be ignored. </p> <p>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? </p> <p>About the order of headings<br>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, <h1>. 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: </p> <p><h1>Article Title</h1> <br>Our next title (if it is not repeated with another <h1>) should be <h2>, then <h3>, etc. You probably shouldn't skip one level after <h1> and go straight to <h3>. 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. </p> <p>As mentioned earlier, designers may use <h4> to label the most important title on a page, simply because its default font size is not as obnoxiously large as <h1>. But remember, structure first, design later. We can always use CSS to style headings to any text size we like. <br></p></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="CSS solves the problem of vertical centering of unknown height_CSS/HTML" href="http://m.php.cn/faq/1247.html">CSS solves the problem of vertical centering of unknown height_CSS/HTML</a></span><span>Next article:<a class="dBlack" title="CSS solves the problem of vertical centering of unknown height_CSS/HTML" href="http://m.php.cn/faq/1250.html">CSS solves the problem of vertical centering of unknown height_CSS/HTML</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="http://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ul class="nphpXgwzList"><li><b></b><a href="http://m.php.cn/faq/347776.html" title="Summary of six implementation methods of JSDOMReady event" class="aBlack">Summary of six implementation methods of JSDOMReady event</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/348838.html" title="Examples of CSS implementation of various column layouts on the page" class="aBlack">Examples of CSS implementation of various column layouts on the page</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/349106.html" title="Pure CSS3 full-screen responsive slideshow effects" class="aBlack">Pure CSS3 full-screen responsive slideshow effects</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/349108.html" title="Pure CSS3 cool colorful 3D button style" class="aBlack">Pure CSS3 cool colorful 3D button style</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/349109.html" title="Stunning CSS3 hexagonal menu animation effects" class="aBlack">Stunning CSS3 hexagonal menu animation effects</a><div class="clear"></div></li></ul></div></div><div class="nphpFoot"><div class="nphpFootBg"><ul class="nphpFootMenu"><li><a href="http://m.php.cn/"><b class="icon1"></b><p>Home</p></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><p>Course</p></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><p>Q&A</p></a></li><li><a href="http://m.php.cn/login"><b class="icon5"></b><p>My</p></a></li><div class="clear"></div></ul></div></div><div class="nphpYouBox" style="display: none;"><div class="nphpYouBg"><div class="nphpYouTitle"><span onclick="$('.nphpYouBox').hide()"></span><a href="http://m.php.cn/"></a><div class="clear"></div></div><ul class="nphpYouList"><li><a href="http://m.php.cn/"><b class="icon1"></b><span>Home</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><span>Course</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/article.html"><b class="icon3"></b><span>Article</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><span>Q&A</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/dic.html"><b class="icon6"></b><span>Dictionary</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course/type/99.html"><b class="icon7"></b><span>Manual</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/xiazai/"><b class="icon8"></b><span>Download</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/faq/zt" title="Topic"><b class="icon12"></b><span>Topic</span><div class="clear"></div></a></li><div class="clear"></div></ul></div></div><div class="nphpDing" style="display: none;"><div class="nphpDinglogo"><a href="http://m.php.cn/"></a></div><div class="nphpNavIn1"><div class="swiper-container nphpNavSwiper1"><div class="swiper-wrapper"><div class="swiper-slide"><a href="http://m.php.cn/" >Home</a></div><div class="swiper-slide"><a href="http://m.php.cn/article.html" class="hover">Article</a></div><div class="swiper-slide"><a href="http://m.php.cn/wenda.html" >Q&A</a></div><div class="swiper-slide"><a href="http://m.php.cn/course.html" >Course</a></div><div class="swiper-slide"><a href="http://m.php.cn/faq/zt" >Topic</a></div><div class="swiper-slide"><a href="http://m.php.cn/xiazai" >Download</a></div><div class="swiper-slide"><a href="http://m.php.cn/game" >Game</a></div><div class="swiper-slide"><a href="http://m.php.cn/dic.html" >Dictionary</a></div><div class="clear"></div></div></div><div class="langadivs" ><a href="javascript:;" class="bg4 bglanguage"></a><div class="langadiv" ><a onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan " href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a><a onclick="javascript:;" class="language course-right-orders chooselan chooselanguage" href="javascript:;"><span>English</span><span>(EN)</span></a><a onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan " href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a><a onclick="javascript:setlang('ja');" class="language course-right-orders chooselan " href="javascript:;"><span>日本語</span><span>(JA)</span></a><a onclick="javascript:setlang('ko');" class="language course-right-orders chooselan " href="javascript:;"><span>한국어</span><span>(KO)</span></a><a onclick="javascript:setlang('ms');" class="language course-right-orders chooselan " href="javascript:;"><span>Melayu</span><span>(MS)</span></a><a onclick="javascript:setlang('fr');" class="language course-right-orders chooselan " href="javascript:;"><span>Français</span><span>(FR)</span></a><a onclick="javascript:setlang('de');" class="language course-right-orders chooselan " href="javascript:;"><span>Deutsch</span><span>(DE)</span></a></div></div><script> var swiper = new Swiper('.nphpNavSwiper1', { slidesPerView : 'auto', observer: true,//修改swiper自己或子元素时,自动初始化swiper observeParents: true,//修改swiper的父元素时,自动初始化swiper }); </script></div></div><!--顶部导航 end--><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>