似乎有几种不同的技术,所以我希望在这个问题上得到一个"明确的"答案……

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

Example One:不使用h1标签.对搜索引擎优化没那么好,对吧?

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

Example Two:人在某处找到了这个.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;
}

Example Three:个相同的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;
}

Example Four: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.

推荐答案

你错过了这个选项:

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

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

Css相关问答推荐

下一个js app样式刷新页面后不工作页面

转换间隙值时基于百分比的弹性元素&跳转

在carousel组件上应用css转换时出现问题

显示Reaction组件上的问题.使用FlexBox时的意外大小

设置ScaleImageButton的样式

Prettier/VS Code 在 CSS 中的 !important 之前添加一个空格,导致它在 WordPress 中崩溃

使用CSS Grid是否可以跳过下一列?

弹性布局溢出时的换行符

如何更改 JqGrid 中的pager 背景 colored颜色 ?

为什么我的 CSS 转换在 React 18 中不起作用

在revealjs/Quarto中定义一类反背景的幻灯片

适合剩余高度

如何使单选按钮看起来像切换按钮

twitter-bootstrap:当鼠标悬停在 -tag 中的 btn-group 上时,如何go 掉带下划线的按​​钮文本?

ID 在整个页面中必须是唯一的吗?

通过javascript删除或禁用浏览器的焦点边框

跨域 iframe 调整大小

CSS 外边框

从 textarea 中删除所有样式(边框、发光)

通过css将图像添加到文本的左侧