用徽标图像替换H1文本:SEO和可访问性的最佳方法?

时间:2009-03-20 04:57:46

标签: css xhtml image seo

似乎有一些不同的技术,所以我希望得到一个“明确”的答案......

在网站上,通常会创建一个链接到主页的徽标。我想做同样的事情,同时最好地优化搜索引擎,屏幕阅读器,IE 6+以及禁用CSS和/或图像的浏览器。

示例一:不使用h1标记。对SEO不太好吧?

<div id="logo">
    <a href="">
        <img src="logo.png" alt="Stack Overflow" />
    </a>
</div>

示例二:在某个地方找到了这个。 CSS看起来有点像hacky。

<h1 id="logo">
    <a href="">Stack Overflow</a>
</h1>
/* css */
#logo {
    padding: 70px 0 0 0;
    overflow: hidden;
    background-image: url("logo.png");
    background-repeat: no-repeat;
    height: 0px !important;
    height /**/:70px;
}

示例三:相同的HTML,使用text-indent的不同方法。这是图像替换的“Phark”方法。

<h1 id="logo">
    <a href="">Stack Overflow</a>
</h1>
/* css */
#logo {
    background: transparent url("logo.png") no-repeat scroll 0% 0%;
    width: 250px;
    height: 70px;
    text-indent: -3333px;
    border: 0;
    margin: 0;
}

#logo a {
    display: block;
    width: 280px; /* larger than actual image? */
    height: 120px;
    text-decoration: none;
    border: 0;
}

示例四: The Leahy-Langridge-Jefferies method。关闭图像和/或css时显示。

<h1 id="logo" class="logo">
    <a href="">Stack Overflow</a>
</h1>
/* css */
h1.logo {
    margin-top: 15px; /* for this particular site, set this as you like */
    position: relative; /* allows child element to be placed positioned wrt this one */
    overflow:hidden; /* don’t let content leak beyond the header - not needed as height of anchor will cover whole header */
    padding: 0; /* needed to counter the reset/default styles */
}

h1.logo a {
    position: absolute; /* defaults to top:0, left:0 and so these can be left out */
    height: 0; /* hiding text, prevent it peaking out */
    width: 100%; /* 686px; fill the parent element */
    background-position: left top;
    background-repeat: no-repeat;
}

h1#logo {
    height: 60px; /* height of replacement image */
}

h1#logo a {
    padding-top: 60px; /* height of the replacement image */
    background-image: url("logo.png"); /* the replacement image */
}

对于这类事情,最好的方法是什么?请在答案中提供html和css。

16 个答案:

答案 0 :(得分:210)

你错过了选项:

<h1>
  <a href="http://stackoverflow.com">
    <img src="logo.png" alt="Stack Overflow" />
  </a>
</h1>
href中的

标题和h1中的img非常非常重要!

答案 1 :(得分:17)

我这样做主要与上面的一样,但出于可访问性原因,我需要支持在浏览器中禁用图像的可能性。因此,我将<span>绝对定位到<span>的完整宽度和高度并使用<a>将其置于上方,而不是将文本从页面上的链接缩进。堆叠顺序中的链接文本。

价格只有一个空z-index,但我愿意在那里找到与<span>一样重要的东西。

<h1>
<h1 id="logo">
  <a href="">Stack Overflow<span></span></a>
</h1>

答案 2 :(得分:11)

如果可访问性原因很重要,那么请使用第一个变体(当客户希望看到没有样式的图像时)

<div id="logo">
    <a href="">
        <img src="logo.png" alt="Stack Overflow" />
    </a>
</div>

无需符合虚构的SEO要求,因为上面的HTML代码具有正确的结构,只有您应该决定这适合您的访问者。

此外,您可以使用HTML代码较少的变体

<h1 id="logo">
  <a href=""><span>Stack Overflow</span></a>
</h1>
/* position code, it may be absolute position or normal - depends on other parts of your site */
#logo {
  ...
}

#logo a {
   display:block;
   width: actual_image_width;
   height: actual_image_height;
   background: url(image.png) no-repeat left top;
}

/* for accessibility reasons - without styles variant*/
#logo a span {display: none}

请注意,我已删除所有其他CSS样式和黑客,因为它们与任务不对应。它们可能仅在特定情况下有用。

答案 3 :(得分:3)

我认为你对H1 debate感兴趣。关于是否将h1元素用于页面标题或徽标是一个争论。

就我个人而言,我会按照你的第一个建议去做,就像这样:

<div id="header">
    <a href="http://example.com/"><img src="images/logo.png" id="site-logo" alt="MyCorp" /></a>
</div>

<!-- or alternatively (with css in a stylesheet ofc-->
<div id="header">
    <div id="logo" style="background: url('logo.png'); display: block; 
        float: left; width: 100px; height: 50px;">
        <a href="#" style="display: block; height: 50px; width: 100px;">
            <span style="visibility: hidden;">Homepage</span>
        </a>
    </div>
    <!-- with css in a stylesheet: -->
    <div id="logo"><a href="#"><span>Homepage</span></a></div>
</div>


<div id="body">
    <h1>About Us</h1>
    <p>MyCorp has been dealing in narcotics for over nine-thousand years...</p>
</div>

当然这取决于您的设计是否使用页面标题,但这是我对此问题的立场。

答案 4 :(得分:3)

这里迟到了,但无法抗拒。

你的问题是半缺陷的。让我解释一下:

关于图像替换的问题的前半部分是一个有效的问题,我的观点是,对于徽标,一个简单的图像; alt属性;和CSS定位就足够了。

问题的后半部分,关于&#34; SEO值&#34;标识的H1是决定使用哪些元素用于不同类型内容的错误方法。

徽标不是主要标题,甚至根本不是标题,并且使用H1元素标记网站每个页面上的徽标会对您的排名造成(略微)弊大于利。在语义上,标题(H1 - H6)恰好适用于:内容的标题和副标题。

在HTML5中,每页允许多个标题,但徽标不值得其中一个标题。你的徽标,可能是一个模糊的绿色小部件和一些文字是在图片的一侧的图像中有一个原因 - 它是一种&#34;标记&#34;,而不是一个分层元素构建你的内容。第一个(是否使用更多取决于您的标题层次结构)您网站每个页面的H1应标题其主题。索引页面的主要主要标题可能是纽约市模糊绿色窗口小部件的最佳来源&#39;。另一页上的主要标题可能是“我们的模糊小部件的运送详细信息”。在另一页上,它可能是关于伯特的模糊小部件公司。&#39;。你明白了。

旁注:尽管听起来令人难以置信,但请不要查看Google拥有的网络资源的来源,以获取正确标记的示例。这是一个完整的帖子。

获得最多&#34; SEO值&#34;了解HTML及其元素,看一下HTML5 specs,并根据(HTML)语义做出标记决策,并在搜索引擎之前为用户提供价值,并且您的SEO将获得更好的成功。< / p>

答案 5 :(得分:2)

您错过了<a>元素中的标题。

<h1 id="logo">
  <a href="#" title="..."><span>Stack Overflow</span></a>
</h1>

我建议将标题放在<a>元素中,因为客户想知道该图像的含义是什么。因为您已为text-indent的测试设置<h1>,所以该前端用户可以在徽标悬停时获取主徽标的信息。

答案 6 :(得分:2)

<h1>
  <a href="http://stackoverflow.com">
  Stack Overflow<img src="logo.png" alt="Stack Overflow" />
  </a>
</h1>

这是SEO的好选择,因为SEO给H1标签高优先级,h1标签内应该是你的站点名称。如果您在搜索引擎优化中搜索网站名称,则使用此方法也会显示您的网站徽标。

您要隐藏网站名称或文字请使用负值的文字缩进。前

h1 a {
 text-indent: -99999px;
}

答案 7 :(得分:1)

没有人触及的一点是,h1属性应该特定于每个页面,并且使用站点徽标将在站点的每个页面上有效地复制H1。

我喜欢为每个页面使用z索引隐藏h1,因为最好的搜索引擎优化h1通常不是最适合销售或美学价值的。

答案 8 :(得分:0)

我认为第一个示例绰绰有余,因为如果图像被禁用,将显示替代文字。这也将帮助搜索引擎了解您的网站。

更新:看起来我错了。请检查此article

答案 9 :(得分:0)

我不知道,但这是使用的格式...

<h1>
    <span id="site-logo" title="xxx" href="#" target="_self">
        <img src="http://www.xxx.com/images/xxx.png" alt="xxx" width="xxx" height="xxx" />
        <a style="display:none">
            <strong>xxx</strong>
        </a>
    </span>
</h1>

简单,就我所见,它没有对我的网站造成任何伤害。你可以css它,但我没有看到它加载更快。

答案 10 :(得分:0)

新的(Keller)方法应该可以提高-9999px方法的速度:

.hide-text {
text-indent: 100%;
white-space: nowrap;
overflow: hidden;
}

此处推荐:http://www.zeldman.com/2012/03/01/replacing-the-9999px-hack-new-image-replacement/

答案 11 :(得分:0)

W3C的表现如何?

只需看看https://www.w3.org/。 图片中有z-index:1,由于z-index:0position: absolute;

耦合,因此文字在此处但在后面

原始HTML:

<h1 class="logo">
    <a tabindex="2" accesskey="1" href="/">
        <img src="/2008/site/images/logo-w3c-mobile-lg" width="90" height="53" alt="W3C">
    </a>
    <span class="alt-logo">W3C</span>
</h1>

原始CSS:

#w3c_mast h1 a {
    display: block;
    float: left;
    background: url(../images/logo-w3c-screen-lg) no-repeat top left;
    width: 100%;
    height: 107px;
    position: relative;
    z-index: 1;
}
.alt-logo {
    display: block;
    position: absolute;
    left: 20px;
    z-index: 0;
    background-color: #fff;
}

答案 12 :(得分:0)

通读以上解决方案后,我使用了CSS Grid解决方案。

  1. 创建一个包含h1文本和图像的div。
  2. 将两个项目放在同一个单元格中,并使它们都相对定位。
  3. 使用旧的zeldman.com technique通过文本缩进,空格和溢出属性来隐藏文本。
logstash.bat -f logstash.config
<div class="title-stack">
    <h1 class="title-stack__title">StackOverflow</h1>
    <a href="#" class="title-stack__logo">
        <img src="/images/stack-overflow.png" alt="Stack Overflow">
    </a>
</div>

答案 13 :(得分:-1)

<div class="logo">
    <h1><a href="index.html"><span>Insert Website Name</span></a></h1>
    <p>Insert Slogan Here</p>
</div>
#header .logo h1 {
    background: red; /* replace with image of logo */
    display:block;
    height:40px; /* image height */
    width:220px; /* image width */
}

#header .logo h1 a {
    display:block;
    height:40px; /* image height */
    width:220px; /* image width */
}

#header .logo h1 a span {
    display:none;
}

答案 14 :(得分:-1)

<h1><a href="/" title="Some title">Name</a></h1>
h1 a{
  width: {logo width};
  height: {logo height};
  display:block;
  text-indent:-9999px;
  background:url({ logo url});
}

答案 15 :(得分:-1)

出于搜索引擎优化的原因:

<div itemscope itemtype="https://schema.org/Organization">
 <p id="logo"><a href="/"><span itemprop="Brand">Your business</span> <span class="icon fa-stg"></span> - <span itemprop="makesOffer">sell staff</span></a></p>
 </div>
   <h1>Your awesome title</h1>