CSS text replace with image, need hyperlink

ぃ、小莉子 提交于 2019-12-04 19:43:16
espenhogbakk

There are many ways to do this, this is the way that I prefer, it works well, and is easy to implement.

<div id="header">
    <h1><a href="/" title="Homepage">Homepage</a></h1>
</div>

Then i do this css, this is also know as the "Leafy/Langridge image replacement" method

#header h1 a {
    display: block;
    padding: 22px 0 0 0;
    overflow: hidden;
    background-image: url(../images/sidebar/heading.png);
    background-repeat: no-repeat;
    height: 0px !important;
    height /**/:22px;
}

The only thing you should have to edit is the height, and the padding-top. In this example it is 22px, this should be equal to your image-height.

Why are you mucking about with negative indents - just use the alt attribute of the img tag?

<h1 title="Homepage><a href="#"><img src="images/logo.png" alt="My logo"/></a></h1>
Davy Landman

@Partrik Hägne: You should't use display:none, because some screen readers will ignore that...

You can see a list of Nine Techniques for CSS Image Replacement on http://css-tricks.com, which describes the cons and pros for each solution.

What you can do is remove the indent. And use a span to hide instead:

<h1 title="Homepage"><a href="#"><span>My logo</span></a></h1>

#header h1 span
{
  display: none;
}

You might have to set the width and height of the A-tag also since nothing fills using this trick.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!