I want to declare a link without any content, instead I want to use a background image for it. But I can see the background only when I put something between <a></a>
tags. How can I solve it?
views:
101answers:
5
A:
To use a background image, give it a style of display: block;
or display: inline-block;
depending what you're after.
Example HTML:
<a href="#" class="ImgLink"></a>
CSS:
a.ImgLink {
display: block;
background: #FFFFFF url(img.jpg);
width: 200px;
height: 100px;
}
/* Add a hover if you want */
a.ImgLink:hover {
background: #FFFFFF url(imgHover.jpg);
}
Nick Craver
2010-05-02 12:13:45
thank you so much,it works.but my table is tearing apart with that! i want something to take place over my original image(this background is different from background of my link,this is the original image that i want to put my link over it). how can i do it?
armin etemadi
2010-05-02 12:48:56
@armin - You want the link to replace the background of the cell it's in? And if so are you using any javascript framework?
Nick Craver
2010-05-02 12:52:29
+2
A:
Make the link a block-level element, and give it a width and height:
a.somelink {
display: block;
width: 100px;
height: 20px;
background-image: url(someimage.png)
}
Or just use an <img />
inside the <a>
instead of using a background-image
.
Douwe Maan
2010-05-02 12:14:38
thank you so much,it works.but my table is tearing apart with that!i want something to take place over my original image(this background is different from background of my link,this is the original image that i want to put my link over it).how can i do it?
armin etemadi
2010-05-02 12:22:46
@armin that kind of info should be in your question, and should be there in the first place. Please update your question and give a full overview of what you want to do.
Pekka
2010-05-02 21:25:08
+1
A:
A link should always have text, whether it is direct text content or the alt
tag of an image. You can use a negative text-indent
style to hide the text from view, replacing it with the image. Example:
<a href="page.html" id="important-link">check out my important stuff</a>
#important-link {
background: transparent url(../images/important-stuff.png) no-repeat top left;
display: block; /* needed as <a> tag is inline by default */
height: 100px; /* whatever image width is */
text-indent: -9999px; /* moves the text off the screen */
width: 100px; /* whatever image height is */
}
This is a common technique for image replacement where specific fonts are needed, while preserving accessibility (mostly, CSS+no images is the only caveat) and SEO benefits from the text.
roryf
2010-05-02 12:23:58
thank you so much,it works.but my table is tearing apart with that! i want something to take place over my original image(this background is different from background of my link,this is the original image that i want to put my link over it). how can i do it?
armin etemadi
2010-05-02 12:48:14
A:
What about this:
<a href="#> </a>
then remove the possible underline with the following css:
a.somelink {
display: block;
text-decoration:none;
background-image: url(someimage.png)
}
PlanetMaster
2010-05-02 21:23:07