小编典典

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

all

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

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

示例一: 不使用 h1 标签。对搜索引擎优化没有那么好,对吧?

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


阅读 116

收藏
2022-05-18

共1个答案

小编典典

您缺少以下选项:

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

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

2022-05-18