It seems like there are a few different techniques out there, so I was hoping to get a "definitive" answer on this...

On a website, it's common practice to create a logo that links to the homepage. I want to do the same, while best optimizing for search engines, screen readers, IE 6+, and browsers who have disabled CSS and/or images.

Example One: Doesn't use an h1 tag. Not as good for SEO, right?

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

Example Two: Found this somewhere. The CSS seems a little 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;
}

Example Three: Same HTML, different approach using text-indent. This is the "Phark" approach to image replacement.

<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. Displays when images and/or css is turned off.

<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 */
}

What method is the best for this sort of thing? Please provide html and css in your answer.

推荐答案

You're missing the option:

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

title in href and img to h1 is very, very important!

Css相关问答推荐

视频嵌入在Bootstrap 5轮播中没有响应扩展

CSS动画与不正确的时间比率

MUI Reaction移除焦点上的轮廓边框

为什么我的容器的弹性包裹会导致我的自动填充网格添加额外的空轨道行?

使用高图的背景大小渐变

如何按列垂直对齐项目,但不在列内水平对齐

如何在保持默认外观的同时更改 javaFX 中按钮的突出显示 colored颜色

如何在 TypeScript 文件中导入 CSS 模块?

如果父项的不透明度小于 1,则背景过滤器不适用

控制表格单元格之间的间距

如何保持包装的弹性项目与前一行元素的宽度相同?

什么是 ::content/::slotted 伪元素,它是如何工作的?

Chrome 呈现 colored颜色 的方式与 Safari 和 Firefox 不同

Bootstrap 4:导航内的多级下拉菜单

为什么浏览器会为 CSS 属性创建供应商前缀?

css 中的 clearfix 类有什么作用?

CSS3可以转换字体大小吗?

CSS3单向过渡?

可见性:隐藏 vs 显示:无 vs 不透明度:0

如何使用纯 CSS 创建工具提示尾部?