Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an image to an anchor tag using CSS?

Tags:

I want to show an image with the link on the menubar. My code is as below:

  <a href="#" class="login" title="Login"></a> 

The login class in css is as below:

   .login{background: url(../img/user.png) no-repeat 6px center;}  

But, I am not able to view the image in the browser. If I tried like

   <a href="#" class="login" title="Login">Login</a> 

then image appears in the background. But I want to use only image and not the text. how can I do that?

like image 946
Bhushan Firake Avatar asked Dec 08 '12 17:12

Bhushan Firake


People also ask

How do I add an image to an anchor in CSS?

You'll have to set dimensions on the a tag, and set it to display: block; . Of course replace dimensions with the correct ones. If you add the image directly to the a tag like you have at the bottom of your answer, the anchor seems to overflow underneath the image a little bit.

How do I add an image to an anchor tag?

To use image as a link in HTML, use the <img> tag as well as the <a> tag with the href attribute. The <img> tag is for using an image in a web page and the <a> tag is for adding a link. Under the image tag src attribute, add the URL of the image.

Can you put an image in a div?

You can also set an image as the background of an HTML element rather than the entire web page. For example, you could place HTML elements inside a div, then target the div with the CSS properties we used above.


2 Answers

You'll have to set dimensions on the a tag, and set it to display: block;.

.login {   background: url(../img/user.png) no-repeat 6px center;   width: 100px;   height: 100px;   display: block; } 

Of course replace dimensions with the correct ones.

Alternatively you could put the image directly into the a tag like so:

<a href="#" class="login" title="Login"><img src="../img/user.png" /></a>

like image 131
Jan Hančič Avatar answered Oct 15 '22 01:10

Jan Hančič


You can use the following:

<a href="#" class="login" title="Login"><img src="../img/user.png" /></a> 

EDIT: I forgot to mention this would mean you have to remove the background image from your CSS.

like image 37
neilvillareal Avatar answered Oct 15 '22 01:10

neilvillareal