Forum Moderators: not2easy

Message Too Old, No Replies

Image and link issue

using display:block

         

hurafloyd

11:50 am on Mar 31, 2011 (gmt 0)

10+ Year Member



Good day mates,

I'm 2 months new to CSS :) and I am having quite an issue at the moment, I am trying to set an image as a link with CSS, but the problem is that it does not take all the size the image has but just a portion of it. Just to make it more clear, I'm not using the image as a background, but as the link itself.

I dont want to use XHTML or PHP to determine the link with the image since I want to set it as the main template of the website.

Just to give you more data, the image I use has 20x60 pixels in size (being 20 height x 60 width pixels).

The CSS code for the button (and link) in question looks as the following:

/* This would be the container */

.carrito {
float:right;
margin-left:30px;
margin-top:5px;
margin-right:5px;
width:60px;
height:20px;
border:#000 solid 1px;
}

/* This would be the parameters for the link */

.carrito a:link {
color:transparent;
background-image:url(img/carritoa.jpg);
}

.carrito a:visited {
color:transparent;
background-image:url(img/carritoa.jpg);
}

.carrito a:hover {
color:transparent;
background-image:url(img/carritob.jpg);
}

.carrito a:active {
color:transparent;
background-image:url(img/carritoa.jpg);
}

And the HTML just has a div container as the following:

<div class="carrito">
<a href="#">Carrito</a>
</div>

It came to my mind that may be the browser (Firefox) had a predetermined size set or something like that for images in links, so I thought also I could set the height and the size of the image used in the parameters of the pseudoclasses, but to no avail.

I would appreciate your help with this issue, may be I am missing something that a pro knows :) since I am still a beginner.

If you need any other data, let me know and I'll reply asap.

Thanks!

birdbrain

3:42 pm on Mar 31, 2011 (gmt 0)



Hi there hurafloyd,

try it like this...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">

<title>link with background-image</title>

<style type="text/css">
.carrito {
float:right;
border:1px solid #000;
margin:5px 5px 0 30px;
}
.carrito a,.carrito a:visited,.carrito a:active {
display:block;
width:60px;
height:20px;
background-image:url(img/carritoa.jpg);
}
.carrito a:hover {
background-image:url(img/carritob.jpg);
}
</style>

</head>
<body>

<div class="carrito">
<a href="#">Carrito</a>
</div>

</body>
</html>

birdbrain

rocknbil

4:15 pm on Mar 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, the key point being display:block is missing on the a.

This one will be extremely helpful to you: imagine loading only one image for your link. How? Instead of three images, "stack" them into a single image, like this:

the "link" part
---------------
the "visited" part
---------------
the "hover" part

(you can extend this to include active with a fourth row)

So now carrito.jpg is 60px tall. It is displayed through a 60 x 20 pixel "window" in the a that initially reveals only the top left portion. You use the background-position problem to change the various states.

.carrito a {
display:block;
width:60px;
height:20px;
background: transparent url(img/carrito.jpg) top left no-repeat;
}

.carrito a:visited {
background-position: 0 20px; /* x first, then y */
}

.carrito a:hover {
background-position:bottom left;
}

Combine that with B.B.'s solution and you reduce your images as well.

hurafloyd

9:11 am on Apr 1, 2011 (gmt 0)

10+ Year Member



Thank you very much birdbrain and rocknbil, you solved this issue that held me (psychologically) from continuing the template.

It is strange, since I remember studying the display property, but I dont remember using it for this, I'll keep it in mind from now on :D.

Right now I have another issue, but I want to think more about it and try to find the solution by myself, if not I'll not learn anything properly if I keep asking :).

Thanks again!

birdbrain

9:16 am on Apr 1, 2011 (gmt 0)



No problem, you're very welcome. ;)