16

Can I add an <a href=""> link to an HTML element using CSS. As I have an image which I want to add a link to a particular page. Can any one help me in doing this?

feetwet
  • 3,248
  • 7
  • 46
  • 84
amit
  • 189
  • 1
  • 1
  • 11
  • 3
    Maybe because you already can add content, pictures, and other stuff via ":before" and ":after" to the page. – Julix Jan 15 '17 at 05:00

3 Answers3

16

No. Its not possible to add link through css. But you can use jquery

$('.case').each(function() {
  var link = $(this).html();
  $(this).contents().wrap('<a href="example.com/script.php?id="></a>');
});

Here the demo: http://jsfiddle.net/r5uWX/1/

Selvamani
  • 7,434
  • 4
  • 32
  • 43
3

You cannot simply add a link using CSS. CSS is used for styling.

You can style your using CSS.

If you want to give a link dynamically to then I will advice you to use jQuery or Javascript.

You can accomplish that very easily using jQuery.

I have done a sample for you. You can refer that.

URL : http://jsfiddle.net/zyk9w/

$('#link').attr('href','http://www.google.com');

This single line will do the trick.

Rohith Gopi
  • 536
  • 1
  • 5
  • 22
-1

You don't need CSS for this.

     <img src="abc"/>

now with link:

     <a href="#myLink"><img src="abc"/></a>

Or with jquery, later on, you can use the wrap property, see these questions answer:

how to add a link to an image using jquery?

Andhi Irawan
  • 456
  • 8
  • 15
Chris
  • 968
  • 16
  • 27