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?
Asked
Active
Viewed 1.6e+01k times
16
-
3Maybe 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 Answers
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
-
I think [this answer on a similar question](https://stackoverflow.com/a/165146/2636454) provides a better example of this in action. – GDP2 Sep 08 '19 at 21:14
-
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.
$('#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:

Andhi Irawan
- 456
- 8
- 15

Chris
- 968
- 16
- 27