如何正确使用h1标签以图片表示你的博客标题

Posted in WordPress / 软硬一本 @ May 15th, 2008 by underone |

当然,需要说明的是这是通用法则(我理解的),不仅限于WordPress的主题。

G7v1之后(现在看起来其实这个主题很粗糙),我不再使用图片形式表现blog的标题,也就是我们通常使用的“<h1>我的博客</h1>”,因为那是错的,来看一下错误的写法(所谓错误是概念的错误,不是逻辑错误):

<a title="我的博客" href="http://www.myblog.com"> <img src="http://www.myblog.com/image/logo.gif" alt="" /></a>

这是很通常的写法,就是在需要插入图片的地方用<img>标签插入图片,然后用<a>标签加上链接。这个方法对搜索引擎十分不友好,如果<h1>作为博客标题也就是全站唯一的名称标签的话,搜索引擎抓取的是张图,而不是文字,对吧。
于是之后,我发布的theme都没有使用图片形式作为博客标题,但是依然有人会问我想和我自己使用的theme一样使用图片作为标题,该怎么做。这里用G7v5举例说明合理的方式:

<h1><a href="<?php echo get_option('home'); ?>/" title="<?php bloginfo('name'); ?>"><?php bloginfo('name'); ?></a></h1>

这个h1在div #menu中,看起来就是很简单的插入了博客标题,再加上链接,再来看css是怎么写的:

#menu h1 a {
 height: 30px;
 width: 165px;
 float: left;
 text-indent: -9999px;
 background-image: url(images/logo.gif);
 background-repeat: no-repeat;
 display: block;
}

很好理解吧?把h1作为一个块来显示(display: block;),指定长宽(和图片一样大小),然后指定h1的背景图片,也就是将我们需要的图片作为h1这个标签的背景。而h1标签中插入的,仍然是作为字符形式出现的博客标题,然后用text-indent: -9999px;将文字甩到屏幕看不到的地方,9999px应该是足够了,谁的屏幕也没那么大吧:)

于是乎,搜索引擎抓到的h1仍然是文字,看起来却是图片显示,对吧。我觉得这样的方式是合理的。

Popularity: 36% [?]

延展阅读

37 Responses to “如何正确使用h1标签以图片表示你的博客标题”

  1. 以烟为伍  Reply to this comment

    不错,学习了!

  2. 漩涡眸  Reply to this comment

    正解!
    且全文只能存在一个H1

  3. Lifespy  Reply to this comment

    Good!

  4. 虫儿飞  Reply to this comment

    Nice!

  5. Black-Xstar  Reply to this comment

    原来是这样,好深奥的说。

  6. Beach  Reply to this comment

    对,用文字计较好,不过Image几个Alt也不错的!

  7. bssn  Reply to this comment

    se估计很生气,呵呵。

  8. underone  Reply to this comment

    se?

  9. edger  Reply to this comment

    第二快代码中
    和有点小错

    '<'与'?'之间不应有个空格。

  10. 88ali  Reply to this comment

    老大,我已经成功,www.88ali.com

  11. underone  Reply to this comment

    @edger
    是错了啊。。奇怪。。粘贴是对的,一生成就出空格。。。莫非coolcode出错了。。

  12. underone  Reply to this comment

    知道了。。。wp-admin的
    Writing Settings--Formatting
    WordPress should correct invalidly nested XHTML automatically
    要关掉。。

  13. edger  Reply to this comment

    @underone 汗~

  14. 痴情的猪  Reply to this comment

    我习惯使用1像素

  15. bssn  Reply to this comment

    好像和这篇文章方式一样,呵呵。
    http://www.headsetoptions.org/2008/05/12/seo-friendly-images-to-replace-text-in-navigation-menus/

  16. justk  Reply to this comment

    学习了

  17. underone  Reply to this comment

    @bssn
    就我这英文。。。。。

  18. 二手科学家  Reply to this comment

    终于得知此法,困扰许久,太谢谢了

  19. cheeselife  Reply to this comment

    一直不会用编辑wordpress 你有msn吗,可以直接指教一下吗?

  20. underone  Reply to this comment

    什么叫编辑。。。wordpress.....

  21. Cmb  Reply to this comment

    呵呵,关于text-indent,
    我还喜欢用在那种切换图片的按钮上,a:hover+bg-position触发~

  22. 北海旅游网  Reply to this comment

    请问你以前那个两栏的风格还有吗?

  23. underone  Reply to this comment

    V2V3不都是2栏的么?

  24. 阿可  Reply to this comment

    不错,谢谢了,意外发现,这样也有图片的alt提示和链接,不知道是哪定义的http://cofan.cn/blog

  25. 阿可  Reply to this comment

    知道了,是超链接的title,呵呵

  26. 不懂倔强  Reply to this comment

    请问是直接填图片url,是不是还有什么符号?....我试了几次都没搞好

  27. 不懂倔强  Reply to this comment

    哦~搞定了..~so thx

  28. underone  Reply to this comment

    我的MSN:oooooooo@263.net

  29. funyfan  Reply to this comment

    css隐藏文字,对搜索引擎不友好的说,会被认为是作弊,当然这样的用户体验不错了。善意提醒下

  30. underone  Reply to this comment

    啊?是这样?
    搜索引擎如何知道文字偏出10px和9999px有什么区别?

  31. resun  Reply to this comment

    搜索引擎有时候也会搜索CSS文件的。
    我觉得可以有两种方式,一个是给图片加alt说明属性;第二个文字图片并存,如下:
    html:
    underone

    css:

    h1{width:90px;height:27px;overflow:hidden;position:relative;}
    h1 img{position:absolute;top:0;left:0;border:none;background:#fff;}

    这样没有隐藏文字,只是用图片把文字给覆盖了,应该就没事了吧。不过没有试验,对搜索引擎友不友好也不知道。

    另:
    我很喜欢你的这个博客模板风格,但我用的是bo-blog,所以直接在论坛里下了别人做的G7模板,也不知道模板有没有经过你的同意,希望不要介意:)

  32. underone  Reply to this comment

    bob么,应该有朋友移植的,好像还不止一个……

  33. chiny  Reply to this comment

    好文,留个记号!

  34. 则卷一家 » Blog Archive » wordpress修改日志  Reply to this comment

    [...] 如何正确使用h1标签以图片表示你的博客标题 [...]

  35. 榨菜  Reply to this comment

    丸子 我还是不太明白你的方法 能不能教教我? 很着急啊

  36. underone  Reply to this comment

    是,哪里不明白呢?

  37. 中農  Reply to this comment

    看到你的回复就过来看了,看完,点头,修改了下颜色搞定。
    加你的链接了,希望能加上我哈,谢谢!

Leave a Reply ↓