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

在一个网站上,创建一个链接到主页的标志是很常见的做法。我想做同样的事情,同时对搜索引擎、屏幕阅读器、IE 6+和禁用CSS和/或图像的浏览器进行最佳优化。

例一:不使用h1标记。对搜索引擎优化不太好,对吧?

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

例二:在某个地方找到了这个。CSS看起来有点俗气。

<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,使用文本缩进的不同方法。这就是图像替换的“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;
}

例四:Leahy-Langridge-Jefferies方法。当图像和/或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。


当前回答

我不知道,但这是我们用过的格式。

<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它,但我不认为它加载更快。

其他回答

你错过了以下选项:

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

标题在href和img到h1是非常非常重要的!

W3C是怎么做的?

看看https://www.w3.org/就知道了。 图像的z指数为:1,文本在这里但在后面,因为z指数为:0耦合到位置: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;
}

在阅读了上述解决方案后,我使用了CSS网格解决方案。

创建一个包含h1文本和图像的div。 在相同的单元格中定位两个项目,并使它们都处于相对位置。 使用旧的zeldman.com技术来隐藏文本,使用文本缩进、空白和溢出属性。

<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>
.title-stack {
    display: grid;
}
.title-stack__title, .title-stack__logo {
    grid-area: 1/1/1/1;
    position: relative;
}
.title-stack__title {
    z-index: 0;
    text-indent: 100%;
    white-space: nowrap;
    overflow: hidden;
}
<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});
}

如果你不想在<h1>中包装你的图像时应用浏览器样式,你可以使用属性role="heading"和aria-level="1"来获得与<h1>相同的可访问性和SEO改进。

例如,就画外音而言,这个

<标题> < a href = " / " > 堆栈溢出 < / > < / h1 >

<div role="heading" aria-level="1"> < a href = " / " > <img src="https://via.placeholder.com/150" alt="Stack Overflow" /> < / > < / div >