简体   繁体   English

背景图片作为链接

[英]Background-Image as link

I am trying to make the background image I have in CSS turn into a link which I can reference.我正在尝试将 CSS 中的背景图像变成我可以参考的链接。 I have the following in CSS:我在 CSS 中有以下内容:

#wrapper {
height: 100%;
padding: 66px;
}


#ad_11 {
background-image: url(images/index1.png);
display: block;
text-indent:-9999px;
background-repeat: no-repeat;
background-position: left top;
padding-top:475px;
padding-bottom:0px;
margin: 20px; 
width: 519px; 
float: left;
}

#ad_12 {
background-image: url(images/index2.png);
display: block;
text-indent:-9999px;
background-repeat: no-repeat;
background-position: right top;
padding-top:475px;
padding-bottom:0px;
margin: 20px; 
width: 510px; 
float: left;
}

Then, I have the following HTML:然后,我有以下 HTML:

<div id="wrapper">
<div id="ad_11"></div>
<div id="ad_12"></div>
<br style="clear: both;" /> 
</div>

The question is how do I turn this background image into a link.问题是我如何把这个背景图片变成一个链接。 Is this possible at all?这可能吗?

Thanks.谢谢。

The short answer is no, it's not possible to make background images into links.简短的回答是否定的,不可能将背景图像制作成链接。

As a rule of thumb I'd say that if you need an image to be a link, then the image is content, and should be added using the img tag.根据经验,我会说,如果您需要图像作为链接,那么图像就是内容,应该使用img标签添加。

If the image is being used to replace some text then one thing you can do is put the background image on the anchor and style it so it's the size you want and hide the text using text-indent .如果图像被用于替换某些文本,那么您可以做的一件事是将背景图像放在锚点上并设置样式,使其成为您想要的大小并使用text-indent隐藏文本。

Here's a quick sample of how to do that: http://jsfiddle.net/tS27T/以下是如何执行此操作的快速示例: http://jsfiddle.net/tS27T/

a{
    background: url('image_url.jpg') 0 0 no-repeat;
    display:block;
    width:400px;
    height:400px;
    text-indent:-9999px;
}

UPDATE更新

To answer what I think your new question is i've put together this demo: http://jsfiddle.net/nChaH/为了回答我认为你的新问题,我整理了这个演示: http://jsfiddle.net/nChaH/

Strictly speaking this is not possible, however, see this link for a workaround: http://xavisys.com/css-trick-turning-a-background-image-into-a-clickable-link/严格来说这是不可能的,但是,请参阅此链接以获取解决方法: http://xavisys.com/css-trick-turning-a-background-image-into-a-clickable-link/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM