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

在一个网站上,创建一个链接到主页的标志是很常见的做法。我想做同样的事情,同时对搜索引擎、屏幕阅读器、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。


当前回答

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

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

推荐:http://www.zeldman.com/2012/03/01/replacing - - 9999 px -攻击-新-图像- replacement/

其他回答

SEO原因:

<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>
<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的争论感兴趣。这是关于是将h1元素用于页面标题还是用于logo的争论。

就我个人而言,我同意你的第一个建议,大致如下:

<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>

当然,这取决于你的设计是否使用页面标题,但这是我在这个问题上的立场。

没有人提到的一点是,h1属性应该特定于每个页面,使用网站标志将有效地在网站的每个页面上复制h1。

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

<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;
}